Hi, I'm trying to construct a single-page pdf with a single picture filling all the space.
I want it to have specific dpi and specific page size in mm. Say, 300 dpi, 112.86x169.33mm
As far as I understood, com.lowagie.text.Rectangle(float urx, float ury) width and height are set in "points". And I can't specify dpi for the com.lowagie.text.Document, since it's vector object.
I saw com.lowagie.text.PageSize which has different pages available, but not sure how so specify my own.
I searched through examples but it wasn't very helpful regarding this issue.
How do I set dpi and size in mm?
The meaning of the Rectangle width and height depend on where that rectangle is going to be used. As you mention PageSize, though, I assume you mean Rectangle instances used to set the page size property of a Document, either in the constructor or the accordingly named setter.
Here the rectangle dimensions are set in _default user space units_. These units are defined by the UserUnit entry of the page object in question which is specified as
_(Optional; PDF 1.6)_ A positive number that shall give the size of default user space units, in multiples of 1⁄72 inch. The range of supported values shall be implementation-dependent.
Default value: 1.0 (user space unit is 1⁄72 inch).
(ISO 32000-1, Table 30 – Entries in a page object)
As iText / OpenPdf by default don't explicitly set this entry, the page size rectangle dimensions usually have to be given in 1⁄72 inch. As an inch equals 25.4 mm, you have to multiply your mm values by (72/25.4).
For example for your 112.86mm × 169.33mm rectangle:
float width = 112.86f * 72f / 25.4f;
float height = 169.33f * 72f / 25.4f;
Rectangle rect = new Rectangle(width, height);
Document doc = new Document(rect, 0, 0, 0, 0);
PdfWriter.getInstance(doc, YOUR_OUTPUT_STREAM);
doc.open();
Now you want to add a bitmap image to this page filling it completely. Thus, you have to instantiate an Image, scale it to page size, and add it to the document:
Image image = Image.getInstance(YOUR_IMAGE_LOCATION);
image.scaleAbsolute(width, height);
doc.add(image);
Finally you have to close the document.
doc.close();
Thanks, it worked like a charm!
Most helpful comment
The meaning of the
Rectanglewidth and height depend on where that rectangle is going to be used. As you mentionPageSize, though, I assume you meanRectangleinstances used to set the page size property of aDocument, either in the constructor or the accordingly named setter.Here the rectangle dimensions are set in _default user space units_. These units are defined by the UserUnit entry of the page object in question which is specified as
(ISO 32000-1, Table 30 – Entries in a page object)
As iText / OpenPdf by default don't explicitly set this entry, the page size rectangle dimensions usually have to be given in 1⁄72 inch. As an inch equals 25.4 mm, you have to multiply your mm values by (72/25.4).
For example for your 112.86mm × 169.33mm rectangle:
Now you want to add a bitmap image to this page filling it completely. Thus, you have to instantiate an
Image, scale it to page size, and add it to the document:Finally you have to close the document.