使用 iText 消除 PDF 中的所有空白
我正在测试 iText 生成一个由 8 个平铺格式图像组成的 PDF。我使用 JFreeChart 创建一个图表,然后由 iText 将其转换为图像。 PDF 生成得很好,但是当我打开输出文件时,左侧、右侧和底部仍然有大约一英寸的空白。我想在打印时利用合法尺寸页面上的所有空间。
我知道 PDF 中没有“边距”的概念,并且它不是可编辑的格式。创建的图像必须没有空白。 Document 构造函数中的额外参数实际上是做什么的?
我认为通过向 Document 对象提供必要的参数(LEGAL 和 1f 参数)可以消除空白,并且我的表格将占据打印页面上的所有 8.5x14,但没有运气。
有什么建议吗?预先感谢
原始代码:
// Setup document
Document doc = new Document(PageSize.LEGAL, 1f, 1f, 1f, 1f);
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("c:\\temp\\image_in_chunk.pdf"));
doc.open();
//create the chart, save to file system, and create an iText Image object
ChartUtilities.saveChartAsPNG(new File("C:\\temp\\img.png"), createChart(createDataset()), 240, 240);
Image img1 = Image.getInstance("C:\\temp\\img.png");
PdfPCell cell1 = null;
Paragraph paragraph = new Paragraph();
paragraph.add(new Chunk(img1, 0, 0, true));
PdfPTable table = new PdfPTable(2);
for (int i = 0; i < 8; i++)
{
cell1 = new PdfPCell(paragraph);
table.addCell(cell1);
}
doc.add(table);
doc.close();
更正和工作代码(当然创建您自己的 JFreeChart 作为 img1。我不能发布示例图像输出不是会员):
// Setup document
Document doc = new Document(PageSize.LEGAL, 0f, 0f, 0f, 0f);
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("c:\\temp\\image_in_chunk.pdf"));
doc.open();
//create the chart, save to file system, and create an iText Image object
ChartUtilities.saveChartAsPNG(new File("C:\\temp\\img.png"), createChart(createDataset()), 305, 250);
Image img1 = Image.getInstance("C:\\temp\\img.png");
// Create pdf document
for (int i = 0; i < 8; i++)
{
doc.add(new Chunk(img1, 0, 0, true));
}
doc.close();
I am testing iText to generate a PDF that is composed of 8 images in a tiled format. I use JFreeChart to create a graph, which in turn is converted into an image by iText. The PDF generates fine, but when I open the output file, there is still about an inch of white space on the left, right and bottom. I want to utilize all space on a legal size page when printed.
I know there is no concept of "margins" in PDF and its not an editable format. The image must be created without white space. What do the extra params in the Document constructor actually do then?
I thought by supplying the necessary parameters to the Document object (LEGAL and the 1f params) would eliminate the white space and my table would take up all 8.5x14 on the printed page, but no luck.
Any suggestions? Thanks in advance
Original Code:
// Setup document
Document doc = new Document(PageSize.LEGAL, 1f, 1f, 1f, 1f);
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("c:\\temp\\image_in_chunk.pdf"));
doc.open();
//create the chart, save to file system, and create an iText Image object
ChartUtilities.saveChartAsPNG(new File("C:\\temp\\img.png"), createChart(createDataset()), 240, 240);
Image img1 = Image.getInstance("C:\\temp\\img.png");
PdfPCell cell1 = null;
Paragraph paragraph = new Paragraph();
paragraph.add(new Chunk(img1, 0, 0, true));
PdfPTable table = new PdfPTable(2);
for (int i = 0; i < 8; i++)
{
cell1 = new PdfPCell(paragraph);
table.addCell(cell1);
}
doc.add(table);
doc.close();
Corrected and working Code (Of course create your own JFreeChart as img1. I cannot post a sample image output not being a member):
// Setup document
Document doc = new Document(PageSize.LEGAL, 0f, 0f, 0f, 0f);
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("c:\\temp\\image_in_chunk.pdf"));
doc.open();
//create the chart, save to file system, and create an iText Image object
ChartUtilities.saveChartAsPNG(new File("C:\\temp\\img.png"), createChart(createDataset()), 305, 250);
Image img1 = Image.getInstance("C:\\temp\\img.png");
// Create pdf document
for (int i = 0; i < 8; i++)
{
doc.add(new Chunk(img1, 0, 0, true));
}
doc.close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,您在
Document
构造函数中将页边距设置为 1 磅。 1 磅是 1/72 英寸。您可能应该使用0f
来代替,但这并不能解释 1 英寸的边距。白色的小条子?当然……但不是你所描述的那样。该问题几乎肯定源于您将
Image
包装在Paragraph
中,而该Paragraph
又包装在PdfPTable
中。我建议您缩放图像以匹配页面大小,然后将图像直接添加到文档中,而不是将其包装在表格中:
Okay, you're setting the page margins to 1 point in your
Document
constructor. 1 point is 1/72 of an inch. You should probably use0f
instead, but that doesn't explain a 1 inch margin. Tiny white sliver? Sure... but not what you're describing.The problem almost certainly stems from you wrapping the
Image
in aParagraph
which is in turn wrapped in aPdfPTable
.I suggest you scale the image to match the page size, then add the image directly to the document rather than wrapping it in a table: