2011-03-23 342 views
0

我正在测试iText以生成由平铺格式的8个图像组成的PDF。我使用JFreeChart创建一个图形,然后由iText将其转换为图像。 PDF生成的很好,但是当我打开输出文件时,左侧,右侧和底部仍有大约一英寸的空白。我想打印时利用合法大小页面上的所有空间。使用iText消除PDF中的所有空白空间

我知道在PDF中没有“边距”的概念,它不是可编辑的格式。图像必须在没有空白的情况下创建。文档构造函数中的额外参数实际上做了什么?

我认为通过向Document对象(LEGAL和1f params)提供必要的参数可以消除空白,并且我的表将占用打印页面上的所有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(); 
+0

你能告诉我怎样才能去除顶部空白当我在grails中使用jFreeChart生成条形图? – VVB 2015-02-24 07:10:22

回答

0

好的,您在Document构造函数中将页边距设置为1。 1点是1/72英寸。您可能应该使用0f,但这并不能解释1英寸的边距。微小的白色条子?当然...但不是你所描述的。

该问题几乎可以肯定源于您将Image包装在Paragraph中,而Paragraph又包裹在PdfPTable中。

我建议你缩放图像相匹配的页面大小,然后直接添加图像文件,而不是在一个表中包装它:

Image img1 = Image.getInstance(path); 
img1.scaleAbsoluteHeight(PageSize.LEGAL.getHeight()); 
img1.scaleAbsoluteWidth(PageSize.LEGAL.getWidth()); 

// you might need this, you might not. 
img1.setAbsolutePosition(0, 0); 

// and add it directly. 
document.add(img1); 
+0

感谢您的建议。现在一切都很美好。立即取出桌面有助于减少左/右空白,并且我可以利用整个可打印区域的宽度。将图像包装在段落中不允许平铺效果使用全部14“的长度,因此将其改为直接将图像添加到文档的建议允许我使用整个长度。 – Ken 2011-03-24 13:31:18

+0

请问您可以告诉我如何在grails中使用jFreeChart生成条形图时删除顶部空白空间? – VVB 2015-02-24 07:10:41

+0

不知道,对不起。 – 2015-03-04 00:21:01