2011-03-10 26 views
2

我正在寫下面的代碼,以創建一個帶有表格的PDF文件它。iText:創建的表(com.lowagie.text)只佔用整個頁面的80%,如何使它利用整個頁面

 Document document = new Document(PageSize.A4, 20, 20, 20, 80); 
     Font myfont = new Font(FontFactory.getFont(FontFactory.COURIER_BOLD, 13, Font.NORMAL)); 
     PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(request.getRealPath("/") + "SAMPLE.pdf")); 

     document.open(); 
     Table table = new Table(2); 
     Cell c2 = new Cell(); 

     int[] widths = {8, 150}; //Tried different values, but no change 
     table.setBorder(Rectangle.BOX); 
     table.setAlignment(Element.ALIGN_LEFT); 
     table.setSpacing(0); 
     table.setPadding(0); 
     table.setTableFitsPage(true); //Tried with 'false', even removed it, but no change 
     table.setWidths(widths); 

     c2 = new Cell(new Paragraph("1.  ", myfont)); 
     c2.setBorder(Rectangle.NO_BORDER); 
     table.addCell(c2); 
     c2 = new Cell(new Paragraph("TEST DATA  ", myfont)); 
     c2.setBorder(Rectangle.NO_BORDER); 
     table.addCell(c2); 

     c2 = new Cell(new Paragraph("2.  ", myfont)); 
     c2.setBorder(Rectangle.NO_BORDER); 
     table.addCell(c2); 
     c2 = new Cell(new Paragraph("TEST DATA", myfont)); 
     c2.setBorder(Rectangle.NO_BORDER); 
     table.addCell(c2); 

     c2 = new Cell(new Paragraph("3.  ", myfont)); 
     c2.setBorder(Rectangle.NO_BORDER); 
     table.addCell(c2); 
     c2 = new Cell(new Paragraph("TEST DATA", myfont)); 
     c2.setBorder(Rectangle.NO_BORDER); 
     table.addCell(c2); 

     document.add(table); 
     document.close(); 

但創建的文件包含一個佔據頁面大約80-85%的表格。我希望它能夠利用整個頁面。

我試着對代碼進行一些調整,如將table.setTableFitsPage(true);更改爲table.setTableFitsPage(false);,甚至嘗試刪除它。 也隨着指定的寬度而改變。但在所有情況下,它只是給了我一份只佔80-85%頁面表格的文件。

是否有東西我缺少要添加到我的代碼或有一個屬性,阻止表佔100%的頁面。

它會在內容很大時產生問題,因爲我最終得到的表格長度過長,頁面上的空格仍未被佔用。

附上這裏生成的實際PDF文件的屏幕截圖!

Screen-shot of the PDF generated

+0

您正在使用哪個版本的iText? – Tommi 2011-03-10 07:47:01

+0

我正在使用** itext-2.1.5.jar ** – 2011-03-10 08:42:11

+0

在這種情況下,我首先建議升級到最新版本(目前爲5.​​0.6)。請前往http://itextpdf.com/download.php下載。與您使用的版本相比,最新版本將包含許多改進和錯誤修復,此外,您更有可能從類似網站獲得支持(因爲有更多人使用最新版本而非舊版本圖書館)。 – Tommi 2011-03-10 10:16:51

回答

1

你應該重寫你的表代碼來使用,而不是PdfPTable。你可以找到一些examples of its use online。 iText in Action 2nd ed的整個第4章是關於表格的,PdfPTables是精確的。

很多示例代碼。請享用。

+0

嗯,我曾試着在發佈這個問題之前使用PdfPTable,但即使那樣幫助,稍後隨便我看看** PdfPTable ** [這裏](http://itextpdf.com/examples/iia.php?id=93)的例子之一,在那裏我看到,當我們添加屬性'table.setWidthPercentage(100f);'到表中使用100%頁面的代碼,之後,只是給了'表'一個嘗試,並將其添加到我的代碼'table.setWidth(100f)',並給了寬度相對於100(即所有值的總和加起來爲100)並且做了這個訣竅。 – 2011-03-10 22:54:14

+0

正如你一直與iText api密切相關,我想問**爲什麼從iText 5.xx版本中刪除了表及其他特性?**我認爲表格結構是iText和表格的重要組成部分通常在創建PDfs時使用.. – 2011-03-10 23:02:58

+0

CLASS'Table'已被刪除。「PdfPTable」更加靈活/強大。改用它。而且不需要通用的「表」,因爲我們現在只輸出爲PDF。不,我們不需要一般的其他任何東西,但大多數實體並沒有在.pdf包中的某個地方放置一個優越的替代品。表做了。 – 2011-03-10 23:12:03

0

改爲使用PdfPTable類,並且可以在整個表上設置列寬。使其成爲橫跨整個頁面可以通過構建

float delta = document.getPageSize().getWidth()/numberOfColumns; 
// add delta numberOfColumn times 
PdfPTable table = new PdfPTable(new float[] {delta, delta, delta, delta }); 
table.setWidthPercentage(100f); 

傳入構造寬度確定多少空間給定列獲得相對於整個表格的寬度來完成。

+0

謝謝......但我認爲這也是馬克也提出的建議,沒有必要重複......反正......我已經檢查過了,我也通過使用簡單的**表**解決了這個問題本身....添加了我在上面的評論中如何做到這一點....請檢查它... – 2011-03-11 13:29:05