问题描述
我正在使用PdfWriter
创建PDF文档.我要在PDF文档中添加PdfPTable
.该表具有标题行,然后具有实际数据行.如果表很大,那么其中的一部分将结转到新页面.我希望此页面也具有表标题行.但是,我只在表数据在新页面上时才需要此标题行.
I am using PdfWriter
to create a PDF document. I am adding a PdfPTable
to the PDF document. This table has header row and then actual data rows. If the table is big, then part of it gets carried forward to new page. I want this page to have table header row as well. However, I want this header row only when the table data goes on new page.
推荐答案
这是创建带有标题行的表的方法:
This is how you create a table with a header row:
// table with 2 columns:
PdfPTable table = new PdfPTable(2);
// header row:
table.addCell("Key");
table.addCell("Value");
table.setHeaderRows(1);
// many data rows:
for (int i = 1; i < 51; i++) {
table.addCell("key: " + i);
table.addCell("value: " + i);
}
document.add(table);
在这种情况下,该表需要多个页面.当您将setHeaderRows()
与1
用作参数时,将重复第一行:
In this case, the table needs more than one page. As you used setHeaderRows()
with 1
as parameter, the first row will be repeated:
如果您不希望标题出现在首页上,则必须添加一行:table.setSkipFirstHeader(true);
If you don't want the header to be present on the first page, you have to add a single line: table.setSkipFirstHeader(true);
// table with 2 columns:
PdfPTable table = new PdfPTable(2);
// header row:
table.addCell("Key");
table.addCell("Value");
table.setHeaderRows(1);
table.setSkipFirstHeader(true);
// many data rows:
for (int i = 1; i < 51; i++) {
table.addCell("key: " + i);
table.addCell("value: " + i);
}
document.add(table);
现在表格如下:
这篇关于iText PDFWriter-如果少量表行转到新页,则写入表头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!