Free Support Forum - aspose.com

Image quality when inserting PNG and generating PDF

Hi Aspose

I have a issue regarding the quality of png-images inserted in my documents through DocumentBuilder.insertImage().

We have images that we want to insert in a word template using mail-merge.
Since the images represent graphs, we specifically use png (PNG24) to remain lossless. For safety sake, I now remove the alpha channel before insertion to make sure that this wasn’t the problem.

When we generate a pdf from the resulting word document (se code below), the resulting PDF shows the images with very visible compression artifacts instead of the crisp lossless version.

I have included 2 zoomed snippets from my picture - one from the original png, and one from the generated pdf.

Do you know if this is Aspose that saves the images in a sub-optimal form? And is there anything I could do to improve the quality of the images?

While investigating the issue last week, I temporarily tried saving the document to HTML instead, and noticed that this specific image was included in both png and jpg version in the resulting files. It seems to me, that maybe Aspose chooses to insert the jpg version in the resulting pdf as well?

I hope you can help me along…

Regards Jan

public File generatePDF(Document doc) throws Exception{
File tmpFile = File.createTempFile(“tmp”, “.pdf”);
PdfOptions options = new PdfOptions();
options.setJpegQuality(1);
options.setHeadingsOutlineLevels(0);
options.setExpandedOutlineLevels(0);
options.setTrueTypeFontsFolder(getClass().getResource("/Fonts").getFile());
doc.saveToPdf(0, doc.getPageCount(),tmpFile.getAbsolutePath(), options);
return tmpFile;
}


Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Could you please attach your input document, the image and the output PDF here for testing? I will check the problem on my side and provide you more information.

Best regards,

Hi Andrey

I have produced a simplified test-version of our document, containing only this one mergefield. (The original doc is about 12 pages and very complex with lots of {IF…} fields that need special code to run.

In real use, our graphs are provided by an external webservice, but we have a static example that we use for local unit-testing, which I have provided here.
Currently the images are 96dpi which make the problem much more visible, but I have tested using a 300dpi image as well, and the compression artifacts are still visible as seen in the examples from my original post.

If you need anything else than the 3 files provided, please let me know.

Regards, Jan


Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information. Please try specifying JpegQuality to 100. See the following piece of code:

PdfOptions options = new PdfOptions();

options.setJpegQuality(100);

Best regards,