ASP.NET MVC

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.

Faisal Pathan

Faisal Pathan is a founder of TheCodeHubs, .NET Project Manager/Team Leader, and C# Corner MVP. He has extensive experience with designing and developing enterprise-scale applications. He has good skills in ASP.NET C#, ASP.NET Core, ASP.NET MVC, AngularJS, Angular, React, NodeJS, Amazon S3, Web API, EPPlus, Amazon MWS, eBay Integration, SQL, Entity Framework, JavaScript, eCommerce Integration like Walmart, Tanga, Newegg, Group-on Store, etc. and Windows services.

View Comments

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

2 years ago