In itext, I have a table in my pdf. When the table gets full for a page the entries continues on the next page but it overlaps with my pdf page header. How do I avoid that?
问题:
回答1:
Please take a look at the TableHeader example.
In this example, I create a document with some "Hello World" content:
public void createPdf(String filename) throws IOException, DocumentException {
TableHeader.HeaderTable event = new TableHeader.HeaderTable();
// step 1
Document document = new Document(PageSize.A4, 36, 36, 20 + event.getTableHeight(), 36);
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
writer.setPageEvent(event);
// step 3
document.open();
// step 4
for (int i = 0; i < 50; i++)
document.add(new Paragraph("Hello World!"));
document.newPage();
document.add(new Paragraph("Hello World!"));
document.newPage();
document.add(new Paragraph("Hello World!"));
// step 5
document.close();
}
As you can see, I also define a TableHeader
event. I use this event
as a page event, but I also need this event when I define the Document
. I use the following value as top margin:
20 + event.getTableHeight()
What does this mean? Let's take a look at the implementation of this event:
public class HeaderTable extends PdfPageEventHelper {
protected PdfPTable table;
protected float tableHeight;
public HeaderTable() {
table = new PdfPTable(1);
table.setTotalWidth(523);
table.setLockedWidth(true);
table.addCell("Header row 1");
table.addCell("Header row 2");
table.addCell("Header row 3");
tableHeight = table.getTotalHeight();
}
public float getTableHeight() {
return tableHeight;
}
public void onEndPage(PdfWriter writer, Document document) {
table.writeSelectedRows(0, -1,
document.left(),
document.top() + ((document.topMargin() + tableHeight) / 2),
writer.getDirectContent());
}
}
When I create the event, a PdfPTable
is constructed. I store this table
as a member variable, along with the height of this table: tableHeight
.
I use this tableHeight
when I define the top margin, so that I am 100% sure that the table will fit the margin. I add an additional 20 user units because I don't want the table to stick to the top border of the page:
20 + event.getTableHeight()
When I add the table in the onEndPage()
method, I use the following coordinates:
x = document.left()
y = document.top() + ((document.topMargin() + tableHeight) / 2),
The value of document.top()
is the value of the top of the page minus the top margin. I add some extra space, more specifically the difference of the top margin and the table height divided by two, added to the table height:
tableHeight + ((document.topMargin() - tableHeight) / 2)
This formula can be simplified to:
((document.topMargin() + tableHeight) / 2)
As you can see, all of this is simple Math, the kind of Math you are taught in primary school.
The resulting PDF looks like this:
This proves that your allegation that "it doesn't work" is wrong. Please understand that it is not polite to say "it doesn't work" after people have explained in great detail how to do something. By showing that it does work, people reading this could interpret your allegation as a lie (and that is bad for your karma).