dkim18
asked on
re-sizing scanned documents as PDF file - iText
Experts,
I am working on re-sizing scanned documents as PDF file and the following code prints regular PDF in LETTER size fine, but not scanned documents that are often their size like slightly off (8.501" x 11.09"). Any suggestions?
thanks,
I am working on re-sizing scanned documents as PDF file and the following code prints regular PDF in LETTER size fine, but not scanned documents that are often their size like slightly off (8.501" x 11.09"). Any suggestions?
private byte[] resizePDFToLetter(byte[] pdfIn) throws VisitNoteGenException {
if(pdfIn == null) {
throw new VisitNoteGenException("Cannot resize PDF - input PDF is null");
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
PdfReader pdfReader = new PdfReader(pdfIn);
Document doc = new Document(PageSize.LETTER);
PdfWriter writer = PdfWriter.getInstance(doc, baos);
doc.open();
PdfContentByte cb = writer.getDirectContent();
for(int i=1; i<=pdfReader.getNumberOfPages(); i++) {
doc.newPage();
PdfImportedPage page = writer.getImportedPage(pdfReader, i);
Rectangle originalSize = pdfReader.getPageSize(1);
float scaleX = PageSize.LETTER.getWidth() / originalSize.getWidth();
float scaleY = PageSize.LETTER.getHeight() / originalSize.getHeight();
cb.addTemplate(page, scaleX, 0, 0, scaleY, 0, 0);
}
doc.close();
} catch (Exception e) {
e.printStackTrace();
return pdfIn;
}
return baos.toByteArray();
}
thanks,
ASKER
it didn't make any difference if I take out that line out of the loop.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I provided solution for my own question.