How to give repeating title on all pages - iText p

2019-09-08 04:35发布

问题:

I have a table for title and a table for content. My content table is too long and doesn't fit in a single page. So I want to repeat my title table after each page. How can I achieve this? Please help.

Here is my code

File tempFile = File.createTempFile("progress", "tmp");
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(tempFile));            
document.setMargins(6, 6, 36, 36);
document.setMarginMirroring(true);
document.open();
PdfPTable titleTable = new PdfPTable(new float[] { 18f });
// Image img = Image.getInstance(uploadFolder + "/logo.jpg");
Font titleFont = new Font(getBaseFont(), 12f, Font.BOLD);
Font tHeadFont = new Font(getBaseFont(), 10f, Font.BOLD);
Font contentFont = new Font(getBaseFont(), 10f);
// img.scalePercent(50);
titleTable.setWidthPercentage(100);
titleTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
titleTable.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
// titleTable.addCell(new Paragraph(new Chunk(img, 5, -5)));
document.add(gutter());
titleTable.addCell(new Paragraph("Title", titleFont));         
document.add(titleTable);
document.add(gutter());
document.add(gutter());

PdfPTable comTable = new PdfPTable(new float[] { 6f, 4f, 2f, 3f, 6f, 3f });
PdfPCell cell1 = new PdfPCell(new Paragraph("Module", tHeadFont));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell1.setPaddingBottom(4);
comTable.addCell(cell1);
PdfPCell cell2 = new PdfPCell(new Paragraph("Name", tHeadFont));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell2.setPaddingBottom(4);
comTable.addCell(cell2);
PdfPCell cell3 = new PdfPCell(new Paragraph("Serial", tHeadFont));
cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell3.setPaddingBottom(4);
comTable.addCell(cell3);
PdfPCell cell4 = new PdfPCell(new Paragraph("Lesson", tHeadFont));
cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell4.setPaddingBottom(4);
comTable.addCell(cell4);
PdfPCell cell5 = new PdfPCell(new Paragraph("Topic", tHeadFont));
cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell5.setPaddingBottom(4);
comTable.addCell(cell5);
PdfPCell cell6 = new PdfPCell(new Paragraph("No.of Lessons", tHeadFont));
cell6.setHorizontalAlignment(Element.ALIGN_CENTER);
cell6.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell6.setPaddingBottom(4);
comTable.addCell(cell6);

There are some iterations below for displaying content which is dynamic so I can't tell how many pages will be there. I want titleTable to be repeated on all pages.

回答1:

You have different options.

Approach #1: define a header row for a table

Instead of using two tables, you could use a single table and define one or more header rows. This is explained in the answer to the following questions:

  • How to write a table header if part of a table is forwarded to the next page?
  • How can I convert a CSV file to a table with a repeating header row?
  • ...

You can also get some inspiration from the following examples:

  • Reporting examples
  • Splitting tables
  • Repeating rows
  • ...

Approach #2: define a header for the page

As you may have discovered when browsing other questions on StackOverflow, you can use page events to create a header. Less than a day ago, I explained the basic principle here: Header and Footer in ITextSharp

For more examples, see:

  • How to generate a report with dynamic header in PDF using itextsharp?
  • How to add a text to the left and to the right in a header?
  • How to add text as a header or footer?
  • ...

But what you really want is the answer to the questions:

  • How to add a table as a header?
  • How to create a table with 2 rows that can be used as a footer?

In short, you need a page event like this:

public class FooterTable extends PdfPageEventHelper {
    protected PdfPTable footer;
    public FooterTable(PdfPTable footer) {
        this.footer = footer;
    }
    public void onEndPage(PdfWriter writer, Document document) {
        footer.writeSelectedRows(0, -1, 36, 64, writer.getDirectContent());
    }
}

However, instead of FooterTable, you should call this class HeaderTable, and instead of using hard coded coordinates, you should use something like this:

float x = document.left(36);
float y = document.top(10);
footer.writeSelectedRows(0, -1, x, y, writer.getDirectContent());

When defining the x and the y, I used the document object as a read-only object to get the left coordinate with a margin of 36 and the top coordinate with a margin of 50. It is important to realize that a margin of 10 may be insufficient space for the table: the header table might run into the content table. If that is the case, you need to calculate the height of the header table and change the top margin of the document so that the header table and content table don't overlap.

I have one question for you: we redesigned the iText web site and we released it on Thanksgiving. We now notice that we don't get as many visits as we used to. Given the fact that all the information you needed can be found on the online documentation and given the fact that you still needed to ask the question, we'd like to know what is wrong with the web site. Is there something we can do to improve the content? To find the answers to your questions, I used the search box and tags such as header. What could be the reason that drives people away from our web site? Do we have too much content now?



标签: java itext