EvoPDF/Winnovative – Adding Header, Footer, and Page Number in Existing PDF without using HtmlToPdfConverter

Adding to my previous blog post on using EvoPDF/Winnovative to convert Html to PDF, How we can add Header, Footer, and Page Number in existing PDFs.

When you look examples given by EvoPDF/Winnovative for adding Header/Footer/Page Number in existing PDF, Most of examples are with HtmlToPdfConverter. Which means example of existing PDFs and also converting URL/Html to PDF at a time.

But if we do not want to do any conversion, Just add Header/Footer/Page Number in existing PDF document without using HtmlToPdfConverter than there isn’t any solution documented. So, here we will see how we can achieve this functionality:

Merge Existing PDFs & Add Header/Footer/Page Numbers

 //Create a document to merge all selected PDFs
 Document MergedDocument = new Document();

 //Get individual existing PDFs to merge and add header/footer/page number
 Document PDFDoc1 = new Document(Server.MapPath("~/PDF1.pdf"));
 Document PDFDoc2 = new Document(Server.MapPath("~/PDF2.pdf"));

 //Merge existing PDFs into New Merge Document
 MergedDocument.AppendDocument(PDFDoc1, true, true, true);
 MergedDocument.AppendDocument(PDFDoc2, true, true, true);
 //Appending document with this syntax MergedDocument.AppendDocument(PDFDoc1);
 //will not add the header/footer/page number on the PDF. To add it properly,
 //you need to append document in above syntax Doc.AppendDocument(PDFDoc,
 //enableHeaderAndFooter, drawHeaderOnFirstPage, drawFooterOnFirstPage)

 //Append Header, Footer and Page Number to the merged document so that it will get
 //applied to all existing PDFs appended to the document           
 CreateHeader(MergedDocument, 100, "<h3>Header</h3>", False, "Page &p; of &P;", 10, 60, "Verdana", 10, "Silver", True, "Silver");
 CreateFooter(MergedDocument, 100, "<h3>Footer</h3>", True, "Page &p; of &P;", 10, 60, "Verdana", 10, "Silver", True, "Silver");
            
 byte[] outPdfBuffer = MergedDocument.Save();
 Response.AddHeader("Content-Type", "application/pdf");
 Response.AddHeader("Content-Disposition", String.Format("attachment;  filename=Header_Footer_in_External_PDF.pdf; size={0}", outPdfBuffer.Length.ToString()));
 Response.BinaryWrite(outPdfBuffer);
 Response.End();

/// <summary>
/// Creates the document header
/// </summary>
 private void CreateHeader(Document pdfDocument, int headerHeight, string   headerHtml, bool addPageNumbers, string pageNumberText, int pageNumberLocationX,
int pageNumberLocationY, string pageNumberFontFamily, int pageNumberFontSize, string pageNumberFontColor, bool drawHeaderLine, string headerLineColor)
        {
            pdfDocument.AddHeaderTemplate(headerHeight);
            HtmlToPdfElement HtmlElement = new HtmlToPdfElement(headerHtml, "");
            HtmlElement.FitHeight = true;
            pdfDocument.Header.AddElement(HtmlElement);
            if (addPageNumbers)
            {
                TextElement pageNumberElement = new TextElement(pageNumberLocationX, pageNumberLocationY, pageNumberText,
                    new System.Drawing.Font(new System.Drawing.FontFamily(pageNumberFontFamily), pageNumberFontSize, System.Drawing.GraphicsUnit.Point));
                pageNumberElement.ForeColor = Color.FromName(pageNumberFontColor);
                pageNumberElement.EmbedSysFont = true;
                pdfDocument.Header.AddElement(pageNumberElement);
            }
            if (drawHeaderLine)
            {
                float Width = pdfDocument.Header.Width;
                float Height = pdfDocument.Header.Height;
                LineElement headerLine = new LineElement(0, Height - 1, Width, Height - 1);
                headerLine.ForeColor = Color.FromName(headerLineColor);
                pdfDocument.Header.AddElement(headerLine);
            }
        }
/// <summary>
/// Creates the document footer
/// </summary>
 private void CreateFooter(Document pdfDocument, int footerHeight, string footerHtml, bool addPageNumbers, string pageNumberText, int pageNumberLocationX,
int pageNumberLocationY, string pageNumberFontFamily, int pageNumberFontSize, string pageNumberFontColor, bool drawFooterLine, string footerLineColor)
        {
            pdfDocument.AddFooterTemplate(footerHeight);
            HtmlToPdfElement HtmlElement = new HtmlToPdfElement(footerHtml, "");
            HtmlElement.FitHeight = true;
            pdfDocument.Footer.AddElement(HtmlElement);
            if (addPageNumbers)
            {
                TextElement pageNumberElement = new TextElement(pageNumberLocationX, pageNumberLocationY, pageNumberText,
                    new System.Drawing.Font(new System.Drawing.FontFamily(pageNumberFontFamily), pageNumberFontSize, System.Drawing.GraphicsUnit.Point));
                pageNumberElement.ForeColor = Color.FromName(pageNumberFontColor);
                pageNumberElement.EmbedSysFont = true;
                pdfDocument.Footer.AddElement(pageNumberElement);
            }
            if (drawFooterLine)
            {
                float Width = pdfDocument.Footer.Width;
                LineElement footerLine = new LineElement(0, 0, Width, 0);
                footerLine.ForeColor = Color.FromName(footerLineColor);
                pdfDocument.Footer.AddElement(footerLine);
            }
        }

 

This might help you if you are stuck in same situation, Please share if any other way with which we can implement this functionality.

Thanks for reading 🙂