Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

c# - How To Stop iText7 Image Cutoff When In A Table Cell

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. Here's the image without adding autoscale. Here's the image with autoscale.

Any tips?

question from:https://stackoverflow.com/questions/65836788/how-to-stop-itext7-image-cutoff-when-in-a-table-cell

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...