【发布时间】:2011-03-11 19:42:52
【问题描述】:
我正在编写以下代码以创建一个包含表格的 PDF 文件。
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 文件的屏幕截图!
【问题讨论】:
-
您使用的是哪个版本的 iText?
-
我正在使用 itext-2.1.5.jar
-
在这种情况下,我首先建议升级到最新版本(目前为 5.0.6)。前往itextpdf.com/download.php 进行下载。与您使用的版本相比,最新版本将包含许多改进和错误修复,此外,您更有可能从此类网站获得支持(因为使用最新版本的人比使用非常旧版本的人更多)图书馆)。
-
嗯,支持表格的最后一个版本是 2.17,之后 itext 直接跳转到 5.0.x 版本。 5.0.0 版本删除表格和其他与表格相关的功能...在itext (History) of 5.0.0 处查看它,您会发现它在最后几行中提到....这就是我工作的原因在 2.1.5 版本中......