To summarize, I\'m using Itextsharp to import specific pages from a PDF, possibly rotate, resize or otherwise alter that page, and exporting it into a new PDF. To this end I\'m
I've created a small sample that implements what mkl suggests: ScaleRotate.
PdfReader reader = new PdfReader(src);
int n = reader.getNumberOfPages();
PdfDictionary page;
for (int p = 1; p <= n; p++) {
page = reader.getPageN(p);
if (page.getAsNumber(PdfName.USERUNIT) == null)
page.put(PdfName.USERUNIT, new PdfNumber(2.5f));
page.remove(PdfName.ROTATE);
}
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
stamper.close();
reader.close();
Given an original PDF pages.pdf with rotated pages, cropped pages and annotations. We scale and rotate some pages, resulting in pages_altered.pdf.
We introduce a UserUnit of 2.5 for all pages that aren't scaled yet. If you change the UserUnit to 0.5, you'll see that it won't have any effect in Adobe Reader. The ISO standard for PDF says that the range that can be used for the user unit is implementation-independent. Version 1.7 of the PDF specification originally written by Adobe says: "Acrobat 7.0 supports a maximum UserUnit value of 75,000." Nothing is said about the minimum value, but experience tells us that the minimum value supported by Adobe Reader is 1, meaning you can't scale down.
As for the rotation, you can change the rotation of a page by changing the /Rotate
key in the page dictionary. In the example, I removed the key, changing all pages shown in landscape (of which the value for /Rotate
is 90) into portrait (the default value for /Rotate
is 0). You'll notice that this doesn't have any effect on page 4. Page 4 isn't rotated. It looks like a page in landscape because the dimensions of the page are created in such a way that the width is greater than the height.
Summarized: it's a piece of cake to rotate pages in an existing PDF, so is scaling the pages up to a bigger size. If you want to downscale pages, you can only use PdfWriter
(which throws away all annotations) and you need to copy the annotations separately after transforming all the /Rect
values of these annotations. This is a huge task. It took one of our customers several weeks to achieve this correctly. Be prepared to spend an equal amount of time if that's what you want.
DISCLAIMER: the UserUnit value isn't supported by all viewers. Implementations may vary depending on the viewer that is used. The feature was introduced in PDF 1.6, meaning that the functionality won't work in any viewer supporting only older PDF versions.