120196
120196

Reputation: 283

iTextsharp - Repeat a html table on top of every page

I plan to create an invoice by using iTextsharp, Inside my invoice, it consist of 3 parts which are

  1. table on top of page ( include all vendor information)
  2. gridview (item of purchase)
  3. signature part (only in last page of invoice)

So far I complete the gridview part using Pdfptable + splitlate

       PdfPTable table = new PdfPTable(gv.Columns.Count);
       table.AddCell(new PdfPCell(new Phrase(cellText, fontH1)));
       ...
       ...
        //create PDF document
        Document pdfDoc = new Document(PageSize.A4, -30, -30, 15f, 15f);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        pdfDoc.Add(table);
        pdfDoc.Close();
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;" + "filename=GridViewExport.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Write(pdfDoc);
        Response.End();

But I have no idea how to insert table on every page. I plan to use html table because it quite many thing need to control, like difference vendor, addrress, show/hide or cancelled image. Please help.

Upvotes: 0

Views: 1766

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

Your question has been asked before. See How to add HTML headers and footers to a page? in the official documentation, or take a look at How to add HTML headers and footers to a page? on StackOverflow.

The answer by Roman Sidorov is wrong, because Roman assumes that you are triggering NewPage() from your code. That isn't always true. You add a table to the Document, and that table spans multiple pages. This means that iText triggers the NewPage() function internally.

You can add content to every page that is created, by using page events. The OnEndPage() event is triggered right before a NewPage() action is performed. This is when you add extra content to the current page. The OnStartPage() event is triggered right after a NewPage() action is performed. It is forbidden to add content in the OnStartPage() event. See iTextSharp - Header and Footer for all pages

This is an example of a page event implementation in Java:

public class HeaderFooter extends PdfPageEventHelper {
    protected ElementList header;
    protected ElementList footer;
    public HeaderFooter() throws IOException {
        header = XMLWorkerHelper.parseToElementList(HEADER, null);
        footer = XMLWorkerHelper.parseToElementList(FOOTER, null);
    }
    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        try {
            ColumnText ct = new ColumnText(writer.getDirectContent());
            ct.setSimpleColumn(new Rectangle(36, 832, 559, 810));
            for (Element e : header) {
                ct.addElement(e);
            }
            ct.go();
            ct.setSimpleColumn(new Rectangle(36, 10, 559, 32));
            for (Element e : footer) {
                ct.addElement(e);
            }
            ct.go();
        } catch (DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }
}

You can easily port it to C#. I used this answer because it was the literal answer to a literal question. However: why would you define your header (or footer) in HTML. That doesn't make sense, does it?

Why don't you just create a PdfPTable and add that to every page in a page event. That is explained in the answer to the question How to add a table as a header? There are many other examples in the page events section of the official documentation.

Upvotes: 1

Related Questions