1

I have been using the old itext jar (com.lowagie) to generate some pdfs from image files. But when I upgraded to the itextpdf jar (5.5.0) the page size ends up being set to A4 (even though I call doc.setPageSize(rectangleOfCustomSize)). When I look at the pdfs side by side the new code has the image file flowing off of the page (top and right). The old page has a size of 15.81x23.32 while the new one is 8.26x11.69.

How do I change my code to fix this issue? I need to always have custom pages sizes (never standard).

Here are some code snippets.

Document document = new Document();
...

// Set image scale
image = com.itextpdf.text.Image.getInstance(file.toString());
...
int xDPI = image.getDpiX();
int yDPI = image.getDpiY();

if (xDPI != 72 || yDPI != 72) {
    image.scaleAbsolute(image.getWidth() * 72f / xDPI, image.getHeight() * 72f / yDPI);
}
...
Rectangle size = new Rectangle(image.getPlainWidth(), image.getPlainHeight());
document.setPageSize(size);

Here is a screen capture of the 2 side by side. Old and New files

Mark.ewd
  • 694
  • 1
  • 8
  • 22
  • 1
    When you use `setPageSize()`, the new page size is only active after the next `newPage()`. See the example in the answer to this question: http://stackoverflow.com/questions/23117200/itext-create-document-with-unequal-page-sizes – Bruno Lowagie Dec 20 '14 at 08:28
  • Thanks. I'll have to refactor our code. This code has worked for years with the old library. Our code calls newPage() after adding the image and text. I'll try calling newPage() right after setting the page size. – Mark.ewd Dec 22 '14 at 14:43
  • I moved the newPage() call so that it was directly after setPageSize and the output is correct. Thanks. – Mark.ewd Dec 22 '14 at 18:48

1 Answers1

0

We noticed that setting the page size for the current page, while still on the current page, could result in some awkward situations.

Suppose that you had a page that was initialized as a LETTER page, and you added some content. Then suddenly halfway the page you could change the size to, let's say POSTCARD size (which is much smaller than LETTER). In that case, iText had no clue what to do because the text that was already added didn't match the new size anymore.

Hence, we decided that any change of the page size would only go into effect after a newPage() was triggered. As a result, you may have to introduce small changes to old code when upgrading to a newer version of iText.

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