In an application, user can upload any pdf file and which has dimensions of 8.46" x 10.97". As per our application dimensions should be 8.5" x 11". Question is, How to re-size the existing pdf page size to set 8.5" x 11"? I have to fix by code, not manually or commend line or external software. Please let me know which java supporting jar (free version) providing functionality to achieve this or through simple java fix also fine.
-
What do you mean by *free version*? E.g. iText can be used freely as long as you respect the AGPL license. Can you respect it? If not, please clarify your licensing restraints. – mkl Jan 27 '14 at 07:48
-
That been said, all you have to do is manipulate the media box (and the crop box if present) values of each document page. This can easily be done using e.g. iText or PDFBox but other libraries, too. Thus, which library shall be used? – mkl Jan 27 '14 at 07:51
-
free means without buy. Please give code if it can be possible through iText or PDFBox for how to resize the existing PDF page. – Subbu Jan 27 '14 at 09:07
-
2"Free means without buy" - **no** it certainly does **not** mean that. For instance, there is shareware, try-before-you-pay, and (of course) The Pirate Bay. – Jongware Jan 27 '14 at 11:30
2 Answers
Using iText you can do something like this:
float width = 8.5f * 72;
float height = 11f * 72;
float tolerance = 1f;
PdfReader reader = new PdfReader("source.pdf");
for (int i = 1; i <= reader.getNumberOfPages(); i++)
{
Rectangle cropBox = reader.getCropBox(i);
float widthToAdd = width - cropBox.getWidth();
float heightToAdd = height - cropBox.getHeight();
if (Math.abs(widthToAdd) > tolerance || Math.abs(heightToAdd) > tolerance)
{
float[] newBoxValues = new float[] {
cropBox.getLeft() - widthToAdd / 2,
cropBox.getBottom() - heightToAdd / 2,
cropBox.getRight() + widthToAdd / 2,
cropBox.getTop() + heightToAdd / 2
};
PdfArray newBox = new PdfArray(newBoxValues);
PdfDictionary pageDict = reader.getPageN(i);
pageDict.put(PdfName.CROPBOX, newBox);
pageDict.put(PdfName.MEDIABOX, newBox);
}
}
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("target.pdf"));
stamper.close();
I introduced the tolerance because you likely don't want to change pages whose size is just a tiny fraction off.
Furthermore you might want to also count in the UserUnit value of the page even though it hardly ever is used.
Any general purpose PDF library is likely to allow something like this, either by providing an explicit method for resizing or by allowing direct access as used here.
Concerning your requirement free version you clarified in a comment free means without buy; you can use iText without buying some license as long as you comply with the AGPL.

- 90,588
- 15
- 125
- 265
-
-
Its working fine only if using PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("C:\\TestingDoc\\target.pdf")); I should not generate new output file. Issue is resizing logic is not working in the byte[] content like PdfReader reader = new PdfReader(attachment.getFullDocContent()); getFullDocContent which will return byte[]. – Subbu Feb 05 '14 at 14:37
-
Instead of the `FileOutputStream` you can also use a `ByteArrayOutputStream` from which you can get a `byte[]` without any file use. In future questions please don't forget to mention important requirements. – mkl Feb 05 '14 at 15:39
-
Its not working: OutputStream out = new ByteArrayOutputStream(); PdfStamper stamper = new PdfStamper(reader, out); byte barray[] = ((ByteArrayOutputStream)out).toByteArray()); – Subbu Feb 06 '14 at 15:37
-
You don't do `byte barray[] = ((ByteArrayOutputStream)out).toByteArray());` directly after `PdfStamper stamper = new PdfStamper(reader, out);`, do you? At that moment obviously the result cannot yet be in `out` because the stamper has not yet been worked with... – mkl Feb 06 '14 at 20:05
-
See after stamper call only trying to get byteArray from out. is there any time specification has to wait for stamper conversion? – Subbu Feb 07 '14 at 06:59
-
As mentioned in comment to your [follow-up question](http://stackoverflow.com/questions/21608598/how-to-get-byte-array-from-itext-pdfreader) I tested the code with a `ByteArrayOutputStream` in place of the `FileOutputStream`, and it worked all right. Thus, your issue is due to something you have not mentioned. Maybe some variable scoping or exception ignoring problem or something similar. – mkl Feb 07 '14 at 13:39
Perhaps appending the existing file to a new document with the given specs using PDFBox as stated in this question (iTextPdf now offers a commercial license)