1

I'm using iTextSharp to add footer to existing pdf files on each page. I have code which is working, but I can't make it to skip first page (I don't want footer on fist page), this is my code:

public class PdfHelper : PdfPageEventHelper
{

    public string HeaderPhrase { get; set; }


    public PdfHelper (string headerPhrase)
    {
        HeaderPhrase = headerPhrase;
    } 

    public PdfHelper ()
    {

    }

    public override void OnEndPage (PdfWriter writer, Document document)
    {
        PdfPTable table = new PdfPTable(1);

        table.TotalWidth = document.PageSize.Width - document.LeftMargin - document.RightMargin; 
        PdfPTable table2 = new PdfPTable(2);
        table.DefaultCell.Border = Rectangle.NO_BORDER;
        table2.DefaultCell.Border = Rectangle.NO_BORDER;


        var p = new Phrase(HeaderPhrase);
        p.Font = new Font(Font.FontFamily.HELVETICA, 20);
        PdfPCell cell2 = new PdfPCell(p);
        cell2.Colspan = 2;
        cell2.Border = Rectangle.NO_BORDER;
        cell2.HorizontalAlignment = Element.ALIGN_RIGHT;
        table2.AddCell(cell2);

        PdfPCell cell = new PdfPCell(table2);
        cell.Border = Rectangle.NO_BORDER;
        table.AddCell(cell);



        table.WriteSelectedRows(0, -1, document.LeftMargin, document.Bottom, writer.DirectContent);
    }
}

Here is my code for generating pdf:

  public void Success()
    {

        var guid = Guid.NewGuid();

        var outPath = "~/Content/pdfs/Ebook - " + guid + ".pdf";
        var mappedPathIn = System.Web.Hosting.HostingEnvironment.MapPath("~/Content/pdfs/ebook - 1.pdf");
        var mappedPathOut = System.Web.Hosting.HostingEnvironment.MapPath(outPath);
        var pdfReader = new PdfReader(mappedPathIn);
        var size = pdfReader.GetPageSizeWithRotation(1);
        var document = new Document(size);

        var fs = new FileStream(mappedPathOut, FileMode.Create, FileAccess.Write);
        var writer = PdfWriter.GetInstance(document, fs);

        document.Open();

        PdfContentByte cb = writer.DirectContent;

        document.NewPage();
        PdfImportedPage tempPage = writer.GetImportedPage(pdfReader, 1);
        cb.AddTemplate(tempPage, 0, 0);

        writer.PageEvent =
           new PdfHelper("This is a test");

        for (int pageNumber = 2; pageNumber <= pdfReader.NumberOfPages; pageNumber++)
        {

            document.NewPage();
            PdfImportedPage page = writer.GetImportedPage(pdfReader, pageNumber);
            cb.AddTemplate(page, 0, 0);


        }

        document.Close();

   }

So what I actually tired is, loop from page 2 and before loop add first page, and then set PageEvent:

        PdfContentByte cb = writer.DirectContent;  

        document.NewPage();
        PdfImportedPage tempPage = writer.GetImportedPage(pdfReader, 1);
        cb.AddTemplate(tempPage, 0, 0);

        writer.PageEvent =
        new PdfHelper("This is a test");

But this adds footer for he first page also.

PhillyNJ
  • 3,859
  • 4
  • 38
  • 64
hyperN
  • 2,674
  • 9
  • 54
  • 92
  • 2
    First, it is almost always preferable to use `OnEndPage` instead of `OnStartPage`. Second, since you are performing a two-pass, unless you have a need for using a `PdfTemplate` you can accomplish this with just a simple `PdfStamper`. See [this answer](http://stackoverflow.com/a/9845722/231316), specifically the third block of code. – Chris Haas Jun 24 '15 at 17:51
  • Thanks for pointing that out, I was actually using OnEndPage, and I was testing the output wit OnStartPage, so I mistakenly C/P test code instead of right one, I'll edit question. – hyperN Jun 24 '15 at 17:54
  • @ChrisHaas Thank you for your answer it helped me. However, I have one more question. Can I centrer text at the bottom of the page. If I use X,Y coordinates as suggested in solution, sometimes my text will go out of page (and I don't know how many chars will text contain because it is dynamically created). With my solution text was always centred. Thanks in advance ! – hyperN Jun 24 '15 at 18:08
  • 1
    Do you at least know a maximum of how many lines it will contain? If so, you could use a `ColumnText` that's full page width and `max_lines * font_height` tall – Chris Haas Jun 24 '15 at 20:31
  • 1
    If you don't know how many lines, you should still be able to use the `WriteSelectedRows` method but with the two-pass stamper (I think, you'll need to try). You can get a `DirectContentByte` by accessing calling `GetOverContent()` on the `PdfPStamper`. – Chris Haas Jun 24 '15 at 20:35

0 Answers0