Support for DPI settings in PDFOptions (Java)

Hello,

How can I create a standard size PDF (8.5x11 inches) from a 300 DPI image?
I have tried the following, but I ended up with an about 35x46 inches PDF.

String imageFilename = "/tmp/standardSize.tif";
    String pdfFilename = imageFilename + ".pdf";
    TiffImage asposeImage = (TiffImage) com.aspose.imaging.Image.load(imageFilename);

    System.out.println(MessageFormat.format("Size W:{0}  H:{1} Dpi {2}x{3}",
            asposeImage.getWidth(),
            asposeImage.getHeight(),
            asposeImage.getVerticalResolution(),
            asposeImage.getHorizontalResolution() ));

    int DefaultPagePpi = 72;
    int DefaultPageWidth = 612;
    int DefaultPageHeight = 792;

    com.aspose.imaging.imageoptions.VectorRasterizationOptions emfRasterization = new com.aspose.imaging.imageoptions.EmfRasterizationOptions();
    emfRasterization.setPageWidth(DefaultPageWidth);
    emfRasterization.setPageHeight(DefaultPageHeight);
    PdfOptions pdfOptions = new PdfOptions();
    pdfOptions.setResolutionSettings(new ResolutionSetting(DefaultPagePpi, DefaultPagePpi));
    pdfOptions.setVectorRasterizationOptions(emfRasterization);
    asposeImage.save(pdfFilename, pdfOptions);

This is the input file

NOTE: The tif above was a standard size pdf page saved to a 300 DPI tif, so it is pretty common to
run into images with this size and DPI

@russ.nichols,

Can you please share source tiff file so that we may further investigate to help you out. Also please share generated result with us.

The source file was attached to the post

I put it in a zip file, maybe it is easier to download

https:// www.dropbox.com/s/2qbateyiruskm84/standardSize.zip?dl=0

@russ.nichols,

I have observed your requirements and regret to share that at present you may not be able to set the DPI settings for exported PDF. An issue with ID IMAGINGJAVA-1304 has been created in our issue tracking system to provide the requested support. However, you may use the following sample code to get the desired size PDF on your end.

String imageFilename = "standardSize.tif";

            using (TiffImage asposeImage = (TiffImage)Image.Load(imageFilename))
            {
                Console.WriteLine(string.Format("Size W:{0} H:{1} Dpi {2}x{3}",
                        asposeImage.Width,
                        asposeImage.Height,
                        asposeImage.VerticalResolution,
                        asposeImage.HorizontalResolution));
                asposeImage.Resize(612, 792);
                int DefaultPagePpiX = 72;
                int DefaultPagePpiY = 72;
                int DefaultPageWidth = 612;
                int DefaultPageHeight = 792;

                VectorRasterizationOptions emfRasterization = new EmfRasterizationOptions();

                emfRasterization.PageWidth=DefaultPageWidth;
                emfRasterization.PageHeight=DefaultPageHeight;
                PdfOptions pdfOptions = new PdfOptions();
                pdfOptions.VectorRasterizationOptions = emfRasterization;
                pdfOptions.ResolutionSettings=new ResolutionSetting(DefaultPagePpiX, DefaultPagePpiY);

                asposeImage.Save("standardSize0.pdf", pdfOptions);
            }

standardSize0.pdf (189.7 KB)

The resize operation works and allows to obtain a PDF of the desired dimension.,
but the quality of the resulting PDF is low.

You can compare the result with what the open source
library we currently use and are trying to migrate from produce:

Is there a way to get a better result?

This is lower priority, but the resulting PDF is also larger

@russ.nichols,

As I shared with you earlier that we have created an issue with ID IMAGINGJAVA-1304 to address the issue of setting DPI that can in improve the quality. This issue will get resolved as soon as the concerned issue will be fixed. For the time being, I also suggest you to consider using Aspose.Pdf on your end for converting TIFF to PDF.

 // Instantiate Document Object
    Document doc = new Document();
    // Add a page to pages collection of document
    Page page = doc.getPages().add();
    // Load the source image file to Stream object
    InputStream fs = new FileInputStream(dataDir + "standardSize.tif");
    // Set margins so image will fit
    page.getPageInfo().setMargin(new MarginInfo(0, 0, 0, 0));
    // Create an image object
    com.aspose.pdf.Image image1 = new com.aspose.pdf.Image();
    // Add the image into paragraphs collection of the section
    page.getParagraphs().add(image1);
    // Set the image file stream
    image1.setImageStream(fs);
    // Save resultant PDF file
    doc.save(dataDir + "TIFF2PDF_19.4.pdf");

The issues you have found earlier (filed as IMAGINGJAVA-1304) have been fixed in this update.