Click or drag to resize

Retrieve HTML Elements Mapping to PDF

The converter can report the position in the generated PDF document of any HTML element: the page and the rectangle where the element was rendered. With these positions you can create bookmarks and internal links to HTML elements, place text or images over them, or put a digital signature on the place of an element.

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 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 CSS class name, the element text and the element outer HTML code. An element that spans several pages has one rectangle for each page. GetElementByHtmlId(String) and GetElementsByTagName(String) find the elements in the result.

Code Sample

The code below draws a green frame around all the images and the h1 and h2 headings of the page in the generated PDF document.

PdfConverter pdfConverter = new PdfConverter();

// 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
{
    HtmlElementMappingsCollection mappings =
        pdfConverter.HtmlElementsMappingOptions.HtmlElementsMappingResult;
    foreach (HtmlElementMapping elementMapping in mappings)
    {
        // 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();
}
See Also