How to resize a PdfPTable to fit the page?
我正在生成一个文档,就像下面的代码一样,当然除了表中的内容不同之外。我需要做的是确保这个表在大小上永远不会超过一个页面,不管单元格中的内容是多少。有办法吗?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | import com.itextpdf.text.Phrase; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.PageSize; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import java.awt.Desktop; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; </p> <p> public void createTemplate() throws DocumentException, FileNotFoundException, IOException{ String TARGET = System.getProperty("user.home")+"\temp.pdf"; Document document = new Document(PageSize.A4); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(TARGET)); document.open(); </p> <wyn> PdfPTable table = new PdfPTable(7); for (int i = 0; i < 105; i++) { Phrase p = new Phrase("some text"); PdfPCell cell = new PdfPCell(); cell.addElement(p); table.addCell(cell); } table.setTotalWidth(PageSize.A4.getWidth()-10); table.setLockedWidth(true); PdfContentByte canvas = writer.getDirectContent(); PdfTemplate template = canvas.createTemplate(table.getTotalWidth(),table.getTotalHeight()); table.writeSelectedRows(0, -1, 0, PageSize.A4.getHeight(), template); Image img = Image.getInstance(template); img.scaleToFit(PageSize.A4.getWidth(), PageSize.A4.getHeight()); img.setAbsolutePosition(0, PageSize.A4.getHeight()); document.add(img); document.close(); Desktop desktop = Desktop.getDesktop(); File file = new File(TARGET); desktop.open(file); } |
编辑:@bruno lowagie。用图片包装模板的提示对我来说是正确的,我根据修改了代码,但是现在我得到的只是一个空的PDF。我是在做错事,还是这是一个错误的方法?
如果您希望一个表适合一个页面,那么您应该在考虑页面大小之前创建该表,并像TableHeight示例中那样询问该表的高度。注意,除非定义表的宽度,否则
1 2 | table.setTotalWidth(width); table.setLockedWidth(true); |
现在您可以创建一个大小为
如果不需要自定义页面大小,则需要使用表的大小创建一个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | public static void main(String[] args) throws DocumentException, FileNotFoundException, IOException { String TARGET ="temp.pdf"; Document document = new Document(PageSize.A4); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(TARGET)); document.open(); PdfPTable table = new PdfPTable(7); for (int i = 0; i < 700; i++) { Phrase p = new Phrase("some text"); PdfPCell cell = new PdfPCell(); cell.addElement(p); table.addCell(cell); } table.setTotalWidth(PageSize.A4.getWidth()); table.setLockedWidth(true); PdfContentByte canvas = writer.getDirectContent(); PdfTemplate template = canvas.createTemplate(table.getTotalWidth(), table.getTotalHeight()); table.writeSelectedRows(0, -1, 0, table.getTotalHeight(), template); Image img = Image.getInstance(template); img.scaleToFit(PageSize.A4.getWidth(), PageSize.A4.getHeight()); img.setAbsolutePosition(0, (PageSize.A4.getHeight() - table.getTotalHeight()) / 2); document.add(img); document.close(); } |