2011-09-27 45 views
3

我想將我的文檔中的表格垂直對齊到頁面的底部。iText文檔垂直對齊

我已經將表的垂直對齊設置爲BOTTOM,但這只是使單元格與表本身的底部對齊。

如何使文檔本身垂直對齊底部?

感謝

回答

8

搜索的很多天以後。我的解決辦法是纏上了我的表外表1個細胞。將單元格設置爲頁面高度減去兩個邊距並將垂直對齊設置爲底部。將您想要的所有內容添加到此表中。

全例如,爲了簡潔省略錯誤代碼

public void run() { 
    try { 
     Document document = new Document(PageSize.LETTER, 10.0f, 10.0f, 36.0f, 36.0f); 
     PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("test.pdf")); 
     document.open(); 

     PdfPTable outerTable = new PdfPTable(1); 
     outerTable.setWidthPercentage(100.0f); 

     PdfPCell cell = new PdfPCell(); 
     cell.setMinimumHeight(document.getPageSize().getHeight() - 36.0f - 36.0f); 
     cell.setVerticalAlignment(Element.ALIGN_BOTTOM); 
     cell.addElement(createTable()); 

     outerTable.addCell(cell); 
     document.add(outerTable); 
     document.newPage(); 
     document.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 

public PdfPTable leftTable() { 
    PdfPTable table = new PdfPTable(1); 
    for (int i = 0; i < 50; i++) { 
     table.addCell("Cell: " + i); 
    } 
    return table; 
} 

public PdfPTable middleTable() { 
    PdfPTable table = new PdfPTable(1); 
    for (int i = 0; i < 10; i++) { 
     table.addCell("Cell: " + i); 
    } 
    return table; 
} 

public PdfPTable rightTable() { 
    PdfPTable table = new PdfPTable(1); 
    for (int i = 0; i < 100; i++) { 
     table.addCell("Cell: " + i); 
    } 
    return table; 
} 

public PdfPTable createTable() { 
    PdfPTable table = new PdfPTable(3); 

    table.getDefaultCell().setVerticalAlignment(Element.ALIGN_BOTTOM); 
    table.addCell(leftTable()); 
    table.addCell(middleTable()); 
    table.addCell(rightTable()); 

    return table; 
}