Reputation: 49
I'm trying to use iText7 to create a PDF with questions that are stored in images and respective numbers. The numbers and the images must stay together on the same page, so I used a table object to keep them together; however, the images run off the edge of the page.
Table table = new Table(UnitValue.CreatePercentArray(8)).UseAllAvailableWidth();
Cell cellQuestionNumber = new Cell().Add(questionNum).SetBorder(Border.NO_BORDER);
Cell cellImage = new Cell().Add(img).SetBorder(Border.NO_BORDER);
table.AddCell(cellQuestionNumber);
table.AddCell(cellImage);
document.Add(table);
I've seen some people use .SetAutoScaleWidth(true)
on the image, but when I do that the image just shrinks to an incredibly unreadable size.
. .
Any tips?
Upvotes: 1
Views: 308
Reputation: 12312
There is no need in using tables here - you can add the elements you want to keep in one page into a Div
and then use setKeepTogether(true)
property to prevent splitting that Div
over two pages.
To make sure an image does not occupy more than the width of the page you can set its maximum width property to 100%.
Here is a code example:
Image image = new Image(ImageDataFactory.create("C:\\Users\\Alexey\\Desktop\\Capture.PNG"));
Div div = new Div();
div.add(new Paragraph("#1.)"));
div.add(image.setMaxWidth(UnitValue.createPercentValue(100)));
div.setKeepTogether(true);
document.add(div);
Upvotes: 2