Recently we found Paul Madary's blog post about digital signatures in a Univeral Application (UWP) with iText 7, and we wanted to share it. Paul gracefully agreed to let us do that, and as a bonus we upgraded the code to be usable out-of-the-box with iText 7.1.3. The only change needed is the method SignDocumentSignature.

A big thank you to our Q3 top contributors!

There are so many people that contribute information to us in order to help improve our code, products and projects. We want to make sure that our top contributors are being recognized for the help they give and that they know how much we appreciate them.

Top menu

Breadcrumb

How to rotate and scale pages in an existing PDF?

I need some example code of using the PdfCopy class to resize a page. There is a PdfCopy.SetPagesize() function, but I have basically no idea how to properly scale the source page when it needs to be resized.

31st May 2016

admin-marketing

To this end I'm using the PdfWriter class. The problem I'm running into is that when using the writer class, it doesn't appear to be including the visible annotations from the source page (for instance: the comment are missing).

I am looking for one of two solutions:

Preferably I'd like to continue using the PdfWriter class, but need it to copy/duplicate any visible annotations from the source pages and include them in the output, or

I need some example code of using the PdfCopy class to resize a page. There is a PdfCopy.SetPagesize() function (which doesn't work, which I suspect means I'm doing it wrong), but I have basically no idea how to properly scale the source page when it needs to be resized.

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 need to copy the annotations separately after transforming all the /Rect values of these annotations. This is not a trivial 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.

Click this link if you want to see how to answer this question in iText 5.

Why is the page size of a PDF always the same, no matter if it's landscape or portrait?

Ready to use iText?

As always, if you have any technical questions, you can contact support with your valid support subscription or head over to one of our community support pages on Stack Overflow to see if your question has already been answered for our AGPL users.