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!
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!
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.