Click or drag to resize

Post Convert Customization of the Generated PDF Document

The converter offers a set of render methods described in Advanced Post Convert Customization section, returning the internal Document object created by the converter. This Document object offers access to the collection of pages of PDF document. You can iterate through the document pages, add new pages to the document, append external PDF documents or add new elements like text and images to the document pages. After modification, the document can be saved into a file or to a stream using one of the Save methods of the Document class.

The section Advanced Post Convert Customization contains a detailed description of the classes and methods available in the ExpertPdf.HtmlToPdf.PdfDocument namespace.

Sample Code

Below there is a complete example of how to convert many HTML documents into the same PDF document using post convert customization of the Document object. The code is taken from the WinForms_MultipleHtmlConversions sample. Each additional HTML to PDF conversion is represented by a HtmlToPdfElement object that can be added in any position in a PDF page. The first conversion is performed by the call to pdfConverter.GetPdfDocumentObjectFromUrl, the next two conversions (the conversion of another URL and the conversion of a HTML string) are achieved by adding two HtmlToPdfElement objects to the document pages:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ExpertPdf.HtmlToPdf;
using ExpertPdf.HtmlToPdf.PdfDocument;

namespace WinForms_ConvertMultipleURLsToPdf
    public partial class ConvertMultipleURLsToPdf : Form
        public ConvertMultipleURLsToPdf()

        private void btnConvert_Click(object sender, EventArgs e)
                PdfConverter pdfConverter = new PdfConverter();

                // add header and footer
                if (cbAddHeader.Checked)
                if (cbAddFooter.Checked)

                // call the converter and get a Document object from URL
                Document pdfDocument = pdfConverter.GetPdfDocumentObjectFromUrl(textBoxURL1.Text.Trim());

                // get the conversion summary object from the event arguments
                ConversionSummary conversionSummary = pdfConverter.ConversionSummary;

                // the PDF page where the previous conversion ended
                PdfPage lastPage = pdfDocument.Pages[conversionSummary.LastPageIndex];
                // the last rectangle in the last PDF page where the previous conversion ended
                RectangleF lastRectangle = conversionSummary.LastPageRectangle;

                // the result of adding an element to a PDF page
                // ofers the index of the PDF page where the rendering ended 
                // and the bounding rectangle of the rendered content in the last page
                AddElementResult addResult = null;

                // the converter for the second URL
                HtmlToPdfElement htmlToPdfURL2 = null;

                if (cbStartOnNewPage.Checked)
                    // render the HTML from the second URL on a new page after the first URL
                    PdfPage newPage = pdfDocument.Pages.AddNewPage();
                    htmlToPdfURL2 = new HtmlToPdfElement(0, 0, textBoxURL2.Text);
                    addResult = newPage.AddElement(htmlToPdfURL2);
                    // render the HTML from the second URL immediately after the first URL
                    htmlToPdfURL2 = new HtmlToPdfElement(lastRectangle.Left, lastRectangle.Bottom, textBoxURL2.Text);
                    addResult = lastPage.AddElement(htmlToPdfURL2);

                // the PDF page where the previous conversion ended
                lastPage = pdfDocument.Pages[addResult.EndPageIndex];

                // add a HTML string after all the rendered content
                HtmlToPdfElement htmlStringToPdf = new HtmlToPdfElement(addResult.EndPageBounds.Left, 
                    "<b><i>The rendered content ends here</i></b>", null);


                // save the PDF bytes in a file on disk
                string outFilePath = System.IO.Path.Combine(Application.StartupPath, "Result.pdf");

                // save the PDF document to a file on disk
                    // close the Document to realease all the resources

                // open the generated PDF document in an external viewer
                DialogResult dr = MessageBox.Show("Open the rendered file in an external viewer?", 
                    "Open Rendered File", MessageBoxButtons.YesNo);

                if (dr == DialogResult.Yes)
            catch (Exception ex)

        private void AddHeader(PdfConverter pdfConverter)
            string headerAndFooterHtmlUrl = System.IO.Path.Combine(Application.StartupPath, 

            //enable header
            pdfConverter.PdfDocumentOptions.ShowHeader = true;
            // set the header height in points
            pdfConverter.PdfHeaderOptions.HeaderHeight = 60;
            // set the header HTML area
            pdfConverter.PdfHeaderOptions.HtmlToPdfArea = new HtmlToPdfArea(0, 0, -1, 
                pdfConverter.PdfHeaderOptions.HeaderHeight, headerAndFooterHtmlUrl, 1024, -1);
            pdfConverter.PdfHeaderOptions.HtmlToPdfArea.FitHeight = true;

        private void AddFooter(PdfConverter pdfConverter)
            string headerAndFooterHtmlUrl = System.IO.Path.Combine(Application.StartupPath, 

            //enable footer
            pdfConverter.PdfDocumentOptions.ShowFooter = true;
            // set the footer height in points
            pdfConverter.PdfFooterOptions.FooterHeight = 60;
            //write the page number
            pdfConverter.PdfFooterOptions.TextArea = new TextArea(0, 30, "This is page &p; of &P; ",
                new System.Drawing.Font(new System.Drawing.FontFamily("Times New Roman"), 10, 
            pdfConverter.PdfFooterOptions.TextArea.EmbedTextFont = true;
            pdfConverter.PdfFooterOptions.TextArea.TextAlign = HorizontalTextAlign.Right;
            // set the footer HTML area
            pdfConverter.PdfFooterOptions.HtmlToPdfArea = new HtmlToPdfArea(0, 0, -1, 
                    headerAndFooterHtmlUrl, 1024, -1);
            pdfConverter.PdfFooterOptions.HtmlToPdfArea.FitHeight = true;
See Also