0

I'm producing PDF reports using the iTextSharp library. I have a paragraph as title with a line underneath. When I add an PdfPageEventHelper class as PageEventListener to my writer which handles the background and page numbering on each page, the line disappears. I think it's about the alignment of the background and the line so the line is behind the background but I can't seem to arrange it in front.

Document code:

using (Document doc = new Document())
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-Disposition", "attachment; filename=IVIPS_report.pdf");

            doc.SetPageSize(PageSize.A4.Rotate());
            doc.SetMargins(36f, 36f, 36f, 70f);

            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            bckgr = Image.GetInstance(Request.MapPath(PDF_BACKGROUND_LANDSCAPE_PATH));
            bckgr.Alignment = Image.UNDERLYING;
            bckgr.SetAbsolutePosition(0, 0);
            bckgr.ScaleToFit(doc.PageSize);

            PdfWriter writer = PdfWriter.GetInstance(doc, Response.OutputStream);
            MyPdfPageEventHelpPageNo PdfPageEvent = new MyPdfPageEventHelpPageNo();
            writer.PageEvent = PdfPageEvent;
            doc.Open();


            Font title = new Font(Font.FontFamily.HELVETICA, 16f, Font.BOLD, BaseColor.DARK_GRAY);
            Paragraph p = new Paragraph("Overzicht chauffeurs", title);
            p.Alignment = Element.ALIGN_LEFT;
            LineSeparator line = new LineSeparator(1f, 100f, BaseColor.DARK_GRAY, Element.ALIGN_CENTER, -27f);

            doc.Add(p);
            doc.Add(line);
        }

PdfPageEvent code:

public class MyPdfPageEventHelpPageNo : iTextSharp.text.pdf.PdfPageEventHelper
    {
        public override void OnEndPage(PdfWriter writer, Document document)
        {
            document.Add(bckgr);

            Font fFooter = new Font(Font.FontFamily.HELVETICA, 9f, Font.NORMAL, BaseColor.GRAY);
            Rectangle page = document.PageSize;
            PdfPTable pageFooter = new PdfPTable(1);
            pageFooter.TotalWidth = page.Width;
            Phrase pagenr = new Phrase("Gegenereerd op " + DateTime.Now.ToString("dd/MM/yyyy") + " - Pagina " + document.PageNumber, fFooter);
            PdfPCell c = new PdfPCell(pagenr);
            c.Border = Rectangle.NO_BORDER;
            c.VerticalAlignment = Element.ALIGN_BOTTOM;
            c.HorizontalAlignment = Element.ALIGN_CENTER;
            pageFooter.AddCell(c);
            pageFooter.WriteSelectedRows(0, -1, 0, 25, writer.DirectContent);
        }
    }
BenMorel
  • 34,448
  • 50
  • 182
  • 322
wdumon
  • 105
  • 1
  • 8
  • It is absolutely forbidden to use the `document` instance for reasons other than read-only purposes in a page event. What is the `bckgr` object? You probably want to add the background to the `DirectContentUnder`. Also: a `PdfPTable` to add nothing but a centered line of text? Seriously? You can do better! – Bruno Lowagie Jun 24 '13 at 07:21
  • 1
    I was indeed able to do better. Thanks for the hints! – wdumon Jun 26 '13 at 20:00

0 Answers0