Add Header And Footer To Existing PDF Using itextsharp In C#


In this blog, I explain how to add header and footer in exciting or new PDF with help of itextsharp. sometimes it’s difficult to modify an existing pdf if wanna add something like the header. footer or any other text at a specific location. I’ll use the free itextsharp package to achieve this functionality.

Copy the below code and add your pdf file path which you want to modify.

public ActionResult ModifyPDF()
            var filePath = "YOUR FILE PATH";
            byte[] bytes = System.IO.File.ReadAllBytes(filePath);
            var pdfReader = new PdfReader(bytes);
            using (Stream output = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
                using (PdfStamper pdfStamper = new PdfStamper(pdfReader, output))
                    for (int pageIndex = 1; pageIndex <= pdfReader.NumberOfPages; pageIndex++)
                        pdfStamper.FormFlattening = false;
                        Rectangle pageRectangle = pdfReader.GetPageSizeWithRotation(pageIndex);
                        PdfContentByte pdfData = pdfStamper.GetOverContent(pageIndex);
                        pdfData.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 10);
                        PdfGState graphicsState = new PdfGState
                            FillOpacity = 0.3F

                        // select the font properties
                        BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                        pdfData.SetFontAndSize(bf, 8);

                        // write the text in the pdf content
                        string text = "Faisal Pathan";
                        // put the alignment and coordinates here
                        pdfData.ShowTextAligned(1, text, 70, 775, 0);

                        string text1 = "";
                        pdfData.ShowTextAligned(1, text1, 550, 775, 0);

            return View();

And that’s it when you run this code block, it will add “Faisal Pathan” left side and “” to the right side in the header.

I hope you guys found something useful. Please give your valuable feedback/comments/questions about this article. Please let me know how you like and understand this article and how I could improve it.

1 Comment

  1. Chit Hsu Heinn

    This is a really valuable post. All my problems are solved.


Submit a Comment

Your email address will not be published. Required fields are marked *

Footer Logo


Select Categories