-1

How to set PDF document Page Size to 3 by 5 inches (itextpdf).

When I look at the PageSize API, I don't see option for 3 by 5 inches.

Thanks!

user2371505
  • 97
  • 1
  • 2
  • 10
  • 1
    see https://stackoverflow.com/questions/39031462/set-8-1-2-x-12-itext-page-size – Zach Metcalf Aug 09 '18 at 15:57
  • Please clarify if you are using the old (no longer supported) iText 5 or one of the iText 7 versions that has been released in the last couple of years. If you are using iText 5, then your question is a duplicate as indicated in the previous comment, but you should upgrade to iText 7 before you start doing any real work. No new functionality is being added to iText 5, so if you'll need support for PDF 2.0 or SVG or ... at some point, you'll have to rewrite all your code from scratch. – Bruno Lowagie Aug 09 '18 at 18:07
  • If you chose the future-proof approach and decided to go with iText 7, then the answer is in the FAQ. [How to create a document with unequal page sizes?](https://developers.itextpdf.com/content/best-itext-questions-stackoverview/getting-started/itext7-how-create-document-unequal-page-sizes) As measurement in PDF is done in user units, and as 1 inch corresponds with 72 user units, you need to create a `Rectangle` like this: `Rectangle(216, 360)`. – Bruno Lowagie Aug 09 '18 at 18:10

1 Answers1

1

First, you have to create a low-level document instance like this:

PdfDocument pdf = new PdfDocument(new PdfWriter(""));

Then you need to create a rectangle that measures 3 by 5 inches. As the measurement unit in PDF is the user unit, and as 1 inch corresponds with 72 user units, the rectangle will be 3 x 72 user units wide and 5 by 72 user units high;

Rectangle rectangle3x5 = new Rectangle(216, 360);

Now you can use your own PageSize instance:

PageSize pagesize3x5 = new PageSize(rectangle3x5);

You can use this PageSize instance to create a new high-level document instance:

Document document = new Document(pdf, pagesize3x5);

You can now add objects such as Paragraph and Table to the document instance.

If you don't need a high-level document instance, you can add a page to the low-level document instance like this:

PdfPage page = pdf.addNewPage(pagesize3x5);

Once you have this page, you can use it to create a PdfCanvas instance to which you can add content using low-level methods.

If this doesn't answer your question because you are using an old version of iText, please upgrade to iText 7 because iText 5 is no longer supported. New functionality such as support for PDF 2.0, SVG, etc. will not be added to iText 5, only to iText 7.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165