Difference in print output based on a PNG

Hi,

I have a PNG file written on to a Output stream. When I convert the same to a PDF, I see difference in the margins .
Example, note the difference between the border and the word “Quotation” in PNG output and PDF output.
I have tried playing around with margin and page size, but not successful. Can you please let me know if I am missing something.
Code Im using

           ByteArrayOutputStream baos = new ByteArrayOutputStream();
           ImageIO.write(pageImg, "PNG", baos);
           baos.flush();
           
           //write the image to Aspose PDF Document
           Page page1 = doc.getPages().add();
           if(printOriginalSize)
           {
              page1.getPageInfo().setHeight(pageShort);
              page1.getPageInfo().setWidth(pageLong);
           }
           else
           {
              page1.getPageInfo().setHeight(imHeight);
              page1.getPageInfo().setWidth(imWidth);
           }
           
           com.aspose.pdf.Image image1 = new com.aspose.pdf.Image();
           ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
          image1.setFixHeight(pageLong);
          image1.setFixWidth(pageShort);

           MarginInfo mg = new MarginInfo();
           mg.setLeft(this.marginLeft+100);
           mg.setRight(this.marginRight+100);
           mg.setTop(this.marginTop+100);
           mg.setBottom(this.marginBottom+100);
           image1.setMargin(mg);
           image1.setImageStream(bais);

           page1.getParagraphs().add(image1);
          doc.save()

Attached both PNG and PDF output

EDIT:
If I use Aspose.Imaging, I get a proper output in PDF also.
But the problem is I have multiple images and I need to create multiple pages in one single PDF document, so I am not sure if Aspose.Imaging would be a good fit for the case.

           ByteArrayOutputStream baos = new ByteArrayOutputStream();
           ImageIO.write(pageImg, "PNG", baos);
           baos.flush();
           ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
          //Using Imaging
           PngImage pngImage = new PngImage(bais);
           com.aspose.imaging.imageoptions.PdfOptions exportOptions = new 
       com.aspose.imaging.imageoptions.PdfOptions();
           exportOptions.setPdfDocumentInfo(new PdfDocumentInfo());
           pngImage.save(os,exportOptions);

sample.7z (275.1 KB)

@sushma1509

Have you tried setting the page margins as well? By default, new page has 72 points of margin for each side in a PDF. You can set it to 0 in order to get the page dimensions similar to the image.

page.getPageInfo().setMargin(new MarginInfo(0,0,0,0));

Also, can you please share the outputs that you have obtained in your environment using Aspose.PDF as well as Aspose.Imaging?

Hi @asad.ali
I am attaching the zip file having 3 documents
Actual PNG - Input file
Using_AsposePDF - Converted using Aspose PDF (adding new pages to PDF Doc and setting image stream)
Using AsposeImaging - Converted using Aspsose Imaging (Using PNGImage)
input.zip (16.5 KB)

@sushma1509

The archive contains only a .doc file. Can you please make sure that you attached correct files? Also, have you tried the above suggestion that we shared earlier?

@asad.ali
Sorry my bad. I have attached the correct folder.
Yes, I did try setting margin… But it produced the same result.
One more difference is the canvas size (the white background). PNG has a smaller one, where as PDFs (both from Aspose Imaging and Aspose PDF) have wider canvas
Files.zip (350.3 KB)

Edit : I removed some other code for setting height and width and added margin setting as suggested. Now margins are proper. But the white canvas is still wider. Uploaded another folder
canvasWider.7z (2.2 MB)

aspose_1_old is expected output
aspose_1 is what I get.

@sushma1509

With using the below code and 24.1 version of the API, we received attached output PDF that looks fine:

Document doc = new Document();
Page page = doc.getPages().add();
com.aspose.pdf.Image image = new Image();
image.setFile(dataDir + "ActualPNG.png");

// Set margins so the image will fit
page.getPageInfo().getMargin().setBottom(0);
page.getPageInfo().getMargin().setTop(0);
page.getPageInfo().getMargin().setLeft(0);
page.getPageInfo().getMargin().setRight(0);

// Load the source image file
BufferedImage bimage = ImageIO.read(new File(dataDir + "ActualPNG.png"));

// Get the width and height of the image
int width = bimage.getWidth();
int height = bimage.getHeight();

page.getPageInfo().setWidth(bimage.getWidth());
page.getPageInfo().setHeight(bimage.getHeight());

page.getParagraphs().add(image);
doc.save(dataDir + "output.pdf");

output.pdf (347.8 KB)

Please check it and let us know if you notice any issues in it.

@asad.ali
I have received the same output. But I am concerned on the white background taking the entire browser window width when opened. In PNG file, it is not so.
Also, in aspose_old file, it is not so. White background is not taking entire window witdh

Also, the size of the PDF is too huge. The actual PNG file is only 95KB, where as the converted PDF is 417KB
I tried the below optimization:
com.aspose.pdf.optimization.OptimizationOptions optimizationOptions = new
com.aspose.pdf.optimization.OptimizationOptions();

     optimizationOptions.getImageCompressionOptions().setResizeImages(true);        
     optimizationOptions.getImageCompressionOptions().setMaxResolution(72);

     int quality = 50;
     optimizationOptions.getImageCompressionOptions().setCompressImages(true);
     optimizationOptions.getImageCompressionOptions().setImageQuality(50);
     optimizationOptions.setRemoveUnusedObjects(true);
     optimizationOptions.setRemoveUnusedStreams(true);
     optimizationOptions.setUnembedFonts(true);
     doc.optimizeResources(optimizationOptions);
     doc.save("C:/temp/sample.pdf");
     //doc.save(os,SaveFormat.Pdf);
     doc.close();

Page dimensions are also 3048 (potrait) … Actual is 1511

Still no effect. Can you please help in reducing the size of the PDF?

@sushma1509

We will log an investigation ticket to further investigate both issues related to file size and the white background. However, can you please share some screenshots with the differences between expected and current output for our better understanding?

@asad.ali
Thank you.

I have uploaded the folder which has original PNG file.
The file named aspose_old (generated through other library) has size of 73KB
The file named aspose_new (generated using Aspose PDF) has size of 417 KB.

Files.zip (616.5 KB)

@sushma1509

We were able to notice the output PDF size and when we optimized it in our environment using below code snippet, the size only reduced to 172KB.

Document doc = new Document(dataDir + "aspose_new.pdf");

com.aspose.pdf.optimization.OptimizationOptions opt = new com.aspose.pdf.optimization.OptimizationOptions();
opt.setRemoveUnusedObjects(true);
opt.setAllowReusePageContent(true);
opt.setRemoveUnusedStreams(true);
opt.setLinkDuplcateStreams(true);
opt.setUnembedFonts(true);

com.aspose.pdf.optimization.ImageCompressionOptions imageCompressionOptions = opt.getImageCompressionOptions();
imageCompressionOptions.setCompressImages(true);
imageCompressionOptions.setImageQuality(10);
imageCompressionOptions.setResizeImages(true);
imageCompressionOptions.setMaxResolution(100);

doc.optimizeResources(opt);

// Save the output file
doc.save(dataDir + "out_optimized.pdf");

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFJAVA-43585

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Thanks @asad.ali

@sushma1509

Please feel free to share the details about other issue that you discussed and which was related to the dimensions and background. Please share the screenshots of the differences between actual and expected outputs by highlighting the differences for our better understanding.