Reputation: 3776
I am generating PDF report using iTextPDF for Selenium WebDriver scripts developed in TestNG.
The report would contain text block (String) and images. Images always contain a text block before it.
The issue I am facing is that while creating the document, the text block and image blocks are getting displayed in the wrong order of occurrence in the test case. I believe this is because the image to be inserted has size greater than the PDF page.
Consider a scenario where the order of occurrence in the test is as follows
Text Block1
Image1
Text Block2
Text Block3
Image2
'Text Block4'
But the PDF shows as
Text Block1
Image1
Text Block2
Text Block3
Text Block4
Image2
My code is not wrong. I have triple checked it.
No, I cannot post the code because it is huge (>500 lines) and is in my company system.
I want to know if we can create a PDF page and then change its size dynamically when I encounter that the image to be inserted is large.
Upvotes: 1
Views: 4201
Reputation: 77528
Your code is not wrong. When an image doesn't fit and there's text that follows the image, adding the image is postponed. You can change this behavior by using the following line:
writer.setStrictImageSequence(true);
In this case writer
is your PdfWriter
instance.
This solves one problem: the sequence of the text and images will now be correct. However, due to the image size, you will end up with plenty of white space in your document because images that don't fit will trigger a new page.
You could try to solve this by changing the page size. This involves using the setPageSize()
method as explained in my answer to this question: iText create document with unequal page sizes
If you want to match page sizes to image sizes, take a look at my answer to this question: Add multiple images into a single pdf file with iText using java
The Image
class extends Rectangle
and we can use an Image
object as a parameter when we create a Document
instance, or we can use an Image
object when we change the page size:
document.setPageSize(img);
document.newPage();
Important: when you change the page size, the new size will only go into effect on the next page. You can't change the size of the current page (it has already been initialized and changing it after initialization might screw up the content that was already added).
Also: it isn't sufficient for you to change the page size to the size of the image because you're also adding text. You could use ColumnText
in simulation mode to find out how much space you need for the text, and then use ColumnText
once more to add the text for real after you've created a page with a size that accommodates for the text and the image.
See Can I tell iText how to clip text to fit in a cell and look for the getYLine()
method.
I wonder if it wouldn't be easier for you to scale down the images so that they fit the page... Of course: if the size of the images can vary, you'd have the risk that large images would become illegible.
P.S. All the answers I refer to are also available in the free ebook The Best iText Questions on StackOverflow. I bundled hundreds of my answers in this book so that I could easily search for already answered questions when answering a new question.
Upvotes: 4