2011-03-23 242 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