如何使用iText5在PDF中的多个页面上显示大尺寸的图像? [英] How to show an image with large dimensions across multiple pages in a PDF using iText5?
问题描述
我有一个大尺寸的图形图像,我需要在PDF文件中显示。
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?
推荐答案
请查看 TiledImage 示例。它采用原始大小的图像,并将其拼贴4页: tiled_image.pdf
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();
为了确保每个页面都大到页面的四分之一,我定义了这个矩形: / p>
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);
我在创建文档时使用此矩形
实例和我使用不同的坐标添加相同的图像4次:
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 ;-)
这篇关于如何使用iText5在PDF中的多个页面上显示大尺寸的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!