How to show an image with large dimensions across multiple pages in a PDF using iText5?

空扰寡人 提交于 2019-12-11 03:16:33

问题


I have a graph image with large dimensions which I need to display in a PDF file.

I can't scale to fit the image as this would make text on the node illegible.

How could I split the image into multiple pages while retaining its original dimensions?


回答1:


Please take a look at the TiledImage example. It takes an image at its original size and it tiles it over 4 pages: tiled_image.pdf

To make this work, I first asked the image for its size:

Image image = Image.getInstance(IMAGE);
float width = image.getScaledWidth();
float height = image.getScaledHeight();

To make sure each page is as big as one fourth of the page, I define this rectangle:

Rectangle page = new Rectangle(width / 2, height / 2);

I use this rectangle when creating the Document instance and I add the same image 4 times using different coordinates:

Document document = new Document(page);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfContentByte canvas = writer.getDirectContentUnder();
canvas.addImage(image, width, 0, 0, height, 0, -height / 2);
document.newPage();
canvas.addImage(image, width, 0, 0, height, 0, 0);
document.newPage();
canvas.addImage(image, width, 0, 0, height, -width / 2, - height / 2);
document.newPage();
canvas.addImage(image, width, 0, 0, height, -width / 2, 0);
document.close();

Now I have distributed the image over different pages, which is exactly what you are trying to achieve ;-)



来源:https://stackoverflow.com/questions/26859473/how-to-show-an-image-with-large-dimensions-across-multiple-pages-in-a-pdf-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!