Click or drag to resize

Retrieve HTML Elements Mapping to PDF

This is a very powerful feature of the converter which allows you to obtain the position in the generated PDF document for any HTML element. Knowing the position in the generated PDF document of any element from the HTML document allows you to create bookmarks for elements from the HTML document, create internal links between HTML elements, place texts or images over the HTML elements or assign a digital signature to a certain element from HTML.

This feature can be accessed using the HtmlElementsMappingOptions property of the PdfConverter object. This property allows you to define a list of page elements for which you want to retrieve position. That is done using the HtmlElementSelectors property. This property specifies the CSS selectors of the HTML elements. For example, the selector for all the H1 elements is "H1", the selector for all the elements with the CSS class name 'myclass' is "*.myclass" and the selector for the elements with the id 'myid' is "*#myid".

Read more about CSS selectors here.

The HtmlElementsMappingOptions property must be set before calling the convert method.

The mapping works with the Chromium, Blink and WebKit engines. The HTML elements mapping is returned in the HtmlElementsMappingResult property. The HtmlElementsMappingResult result is a collection of HtmlElementMapping objects which offers the PDF page index where the element was mapped by the converter and the rectangle where the element was rendered inside that page, the element HTML ID, the element tag name, the element text and the element outer HTML code.

Code Sample

Code sample showing how to use this feature to highlight a specified list of HTML elements in the generated PDF document. The code sample below has been 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();
}