I converted some image files to PDF using the following code. I expected image’s aspect ration will be maintained, but it somehow did not do it.
The image quality in PDF document seems to have smaller resolution comparing to the original document. Is there any way to maintain the image quality? I am adding the image sample and PDF sample here.
I am not sure whether my code is wrong or this is something to happen all the time with PDF conversion.
Thanks/Kevin
Document doc = new Document();
Page page = doc.getPages().add();
FileInputStream fs = new FileInputStream(data.getCachedFileName());
BmpImage img = new BmpImage(fs);
page.getPageInfo().getMargin().setBottom(0);
page.getPageInfo().getMargin().setTop(0);
page.getPageInfo().getMargin().setLeft(0);
page.getPageInfo().getMargin().setRight(0);
int maxWidth = img.getWidth();
int maxHeight = img.getHeight();
int defaultDPI = 200;
double scaledWidth = maxWidth * defaultDPI / 300;
double scaledHeight = maxHeight * defaultDPI / 300;
log.info("maxWidth: " + maxWidth);
log.info("maxHeight: " + maxHeight);
log.info("scaledWidth: " + scaledWidth);
log.info("scaledHeight: " + scaledHeight);
page.setCropBox(new Rectangle(0, 0, scaledWidth, scaledHeight));
Image image1 = new Image();
page.getParagraphs().add(image1);
FileInputStream fsInput = new FileInputStream(data.getCachedFileName());
image1.setImageStream(fsInput);
doc.save(outputPdfLocation());
img.close();<a class="attachment" href="/uploads/default/44718">129846442.pdf</a> (550.8 KB)