1

I am trying to create a pdf with footer. I tried the following code to have footer in my pdf file.

private void AddFooter(string filephysicalpath, string documentname)
        {
            byte[] bytes = System.IO.File.ReadAllBytes(filephysicalpath);
            Font blackFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, BaseColor.BLACK);
            using (MemoryStream stream = new MemoryStream())
            {
                PdfReader reader = new PdfReader(bytes);
                using (PdfStamper stamper = new PdfStamper(reader, stream))
                {
                    int pages = reader.NumberOfPages;

                    for (int i = 1; i <= pages; i++)
                    {
                        string footer = Convert.ToString(Session["Footer"]);
                        footer += "\n";
                        footer += documentname;
                        Phrase ph = new Phrase(footer);
                        Rectangle rect = new Rectangle(10f, 10f, 0);
                        ColumnText ct = new ColumnText(stamper.GetUnderContent(i));
                        ct.SetSimpleColumn(rect);
                        ct.AddElement(new Paragraph("line 1"));
                        ct.AddElement(new Paragraph("line 2"));
                        ct.Go();    
                        // ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_JUSTIFIED, new Phrase(ph), 8f, 5f, 0);
                    }
                }
                bytes = stream.ToArray();
            }
            System.IO.File.WriteAllBytes(filephysicalpath, bytes);
        }

I am not getting the footer with this code.

Ankit
  • 760
  • 6
  • 15
Sandip Gend
  • 45
  • 1
  • 8
  • Looks ok at first glance, if you use `stamper#GetOverContent(i)`, does the text still not show up? – Samuel Huylebroeck Nov 22 '16 at 14:22
  • This can never work because `new Rectangle(10f, 10f, 0)` can never be a rectangle that fits your text. – Bruno Lowagie Nov 22 '16 at 14:25
  • i change code......ColumnText ct = new ColumnText(stamper.GetUnderContent(i)); ct.SetSimpleColumn(new Phrase(new Chunk(footer, FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.NORMAL))), 46, 190, 590, 36, 25, Element.ALIGN_LEFT | Element.ALIGN_BOTTOM); ct.Go(); it give correct output but it override to pdf text how can i solve issue – Sandip Gend Nov 22 '16 at 14:26
  • How did you determine the coordinates? Why are they hard coded? – Bruno Lowagie Nov 22 '16 at 14:28
  • any other solution to add footer with multi line? – Sandip Gend Nov 22 '16 at 14:34

1 Answers1

0

This is wrong for two reasons:

Rectangle rect = new Rectangle(10f, 10f, 0);

Reason #1:

There is no way this code can work, because you're creating a rectangle with lower-left coordinate (0, 0), upper-right coordinate (10, 10) and rotation 0. This is a rectangle that measures 0.14 x 0.14 inch (or 0.35 x 0.35 cm). This can never fit the two lines!

Reason #2:

You are using hard coded values to define the Rectangle. Please take a look at the documentation, more specifically at the FAQ entry How to position text relative to page?

You have to get the dimensions of the existing page, and define your coordinates accordingly!

float marginLR = 36;
float marginB = 2;
float footerHeight = 34;
Rectangle pagesize = reader.GetCropBox(i);
if (pagesize == null) {
    pagesize = reader.GetPageSize(i);
}
Rectangle rect = new Rectangle(
    pagesize.Left + marginLR, pagesize.Bottom + margin,
    pagesize.Right - marginLR, pagesize.Bottom + margin + footerheight
);

You may also benefit from reading Get exact cordinates of the page to add a watermark with different page rotation using iTextSharp

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165