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

Introduction

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
                        };
                        pdfData.SetGState(graphicsState);
                        pdfData.BeginText();


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

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

                        pdfData.BeginText();
                        string text1 = "faisalmpathan@gmail.com";
                        pdfData.ShowTextAligned(1, text1, 550, 775, 0);
                        pdfData.EndText();

                       
                    }
                }
                output.Close();
                output.Dispose();
            }            
            return View();
        }

And that’s it when you run this code block, it will add “Faisal Pathan” left side and “faisalmpathan@gmail.com” 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. If you like this blog you can buy me Pizza.

2 Comments

  1. Chit Hsu Heinn

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

    0
    0
    Reply
    1. Thanks for feedback.

      0
      0
      Reply

Submit a Comment

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

Subscribe

Select Categories