re-sizing scanned documents as PDF file - iText


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);;
			PdfContentByte cb = writer.getDirectContent();
			for(int i=1; i<=pdfReader.getNumberOfPages(); i++) {
				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);
		} catch (Exception e) {
			return pdfIn;
		return baos.toByteArray();

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rectangle originalSize =  pdfReader.getPageSize(1);

Open in new window

Do you really mean 1? I ask as it's in the loop (and shouldn't be)
dkim18Author Commented:
it didn't make any difference if I take out that line out of the loop.
dkim18Author Commented:
iText and JavaScript API for Acrobat worked.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dkim18Author Commented:
I provided solution for my own question.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.