Click or drag to resize

Advanced Post Convert Customization of the Generated PDF Document

ExpertPdf is more than a html to pdf converter. ExpertPdf is a full featured PDF library. The main purpose of the component is to convert web pages to PDF, but the tool can also be used to work with existing PDF documents, merge several PDF documents into a single document, add pages, texts, images, shapes to PDF documents, etc.

The PdfConverter class offers a set of render methods producing Document objects created by the converter tool as a result of the html to pdf conversion.

public Document GetPdfDocumentObjectFromUrl(string url);
public Document GetPdfDocumentObjectFromUrl(string url, string internalLinksDocUrl);
public Document GetPdfDocumentObjectFromHtmlFile(string htmlFilePath);
public Document GetPdfDocumentObjectFromHtmlFile(string htmlFilePath, string internalLinksDocUrl);
public Document GetPdfDocumentObjectFromHtmlString(string htmlString);
public Document GetPdfDocumentObjectFromHtmlString(string htmlString, string urlBase);
public Document GetPdfDocumentObjectFromHtmlString(string htmlString, string urlBase, string internalLinksDocUrl);
public Document GetPdfDocumentObjectFromHtmlStream(System.IO.Stream htmlStream, Encoding streamEncoding);
public Document GetPdfDocumentObjectFromHtmlStream(System.IO.Stream htmlStream, Encoding streamEncoding, string urlBase);
public Document GetPdfDocumentObjectFromHtmlStream(System.IO.Stream htmlStream, Encoding streamEncoding, string urlBase, string internalLinksDocUrl);

The PDF document object resulted after conversion is an instance of the Document class. 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 to a file or to a stream using one of the Save methods of the Document class. The classes and methods that can be used to customize the generated PDF document are available in the ExpertPdf.HtmlToPdf.PdfDocument namespace.

The most important class defined in the ExpertPdf.HtmlToPdf.PdfDocument namespace is the Document class. When the Document object is returned by one of the render methods mentioned above, it already contains the PDF pages generated by the converter from the URL or from the HTML string being converted. The collection of PDF pages can be accessed with Pages property. New pages with the desired size, orientation and margins can be added to the collection of pages and new elements can be added to any page in the collection.

In the following sections we offer a detailed description of the elements that can be added to a page in a PDF document. You can add a HtmlToPdfElement object which makes possible multiple conversions in the same PDF document, a HtmlToImageElement object which embeds the image of a HTML document into the PDF document. You can also add new texts and images, shapes, digital signatures, bookmarks, templates, watermarks, file attachments and notes.

Sample Code

In the code sample below taken from the WinForms_HtmlElementsLocationInPdf demo application, all the H1 and IMG elements and the elements with the ID ID1 and ID2 will be highlighted with a green rectangle in the generated PDF:

PdfConverter pdfConverter = new PdfConverter();
pdfConverter.RenderingEngine = RenderingEngine.Chromium;

// the HTML elements whose position in the PDF we want: the images and the h1 / h2 headings
pdfConverter.HtmlElementsMappingOptions.HtmlElementSelectors = new string[] { "img", "h1", "h2" };

Document pdfDocument = pdfConverter.GetPdfDocumentObjectFromUrl("https://www.example.com/article");
try
{
    foreach (HtmlElementMapping elementMapping in pdfConverter.HtmlElementsMappingOptions.HtmlElementsMappingResult)
    {
        // an element can span several pages: one rectangle for each page where it was rendered
        foreach (HtmlElementPdfRectangle location in elementMapping.PdfRectangles)
        {
            PdfPage page = pdfDocument.Pages[location.PageIndex];
            RectangleF rectangle = location.Rectangle;

            // draw a green frame around the element
            RectangleElement frame = new RectangleElement(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
            frame.ForeColor = Color.Green;
            page.AddElement(frame);
        }
    }
    pdfDocument.Save("elements-location.pdf");
}
finally
{
    pdfDocument.Close();
}