HiQPdf Documentation

Get HTML Elements Position and Bounds in PDF

Quickly Create High Quality PDFs

There are many situations when it is necessary to find out details about the HTML elements laid out in PDF. For example, it is interesting to know the bounds in the PDF document where a HTML element was laid out. Having this information it is possible to replace the automatically laid out PDF content with a different content or it is possible to manually create outlines and links to the position where a HTML element was laid out.

Besides finding the bounds in PDF document where a HTML document was laid out it is also possible to get the HTML element ID, HTML code, CSS class, the tag and the text.

In order to select the HTML elements for which to retrieve information after conversion you have to set the HtmlToPdfSelectedHtmlElements property with a list of CSS selectors. The information about the selected elements will be returned in the SelectedHtmlElementsInfo property of the HiQPdfHtmlConversionInfo class. A reference to an object of the HtmlConversionInfo is given by the HtmlToPdfConversionInfo property after conversion.

HTML Elements Info Demo

In this demo you can learn how to hide a HTML image from the generated PDF document and how to place a different image in the same position. By default the CSS selector selects all the images from the HTML document for replacement. You can modify the CSS selector to select an image with a given ID using a selector like #ImageID. A similar approach can be used for example to replace the HTML form elements with PDF form elements in the generated PDF document in order to create interractive PDF forms.

Demo Source Code

C#
private void buttonCreatePdf_Click(object sender, EventArgs e)
{
    HtmlToPdf htmlToPdfConverter = new HtmlToPdf();

    htmlToPdfConverter.SelectedHtmlElements = new string[] { textBoxImageSelector.Text };
    htmlToPdfConverter.HiddenHtmlElements = new string[] { textBoxImageSelector.Text };

    Cursor = Cursors.WaitCursor;
    string pdfFile = Application.StartupPath + @"\DemoOutput\ReplaceHtmlElements.pdf";
    PdfDocument document = null;
    try
    {
        document = htmlToPdfConverter.ConvertUrlToPdfDocument(textBoxUrl.Text);

        foreach (HtmlElementInfo htmlImageInfo in htmlToPdfConverter.ConversionInfo.SelectedHtmlElementsInfo)
        {
            PdfPageRegion imagePdfRegion = htmlImageInfo.PdfRegions[0];
            PdfPage imagePdfPage = document.Pages[imagePdfRegion.PageIndex];

            // create the image element
            PdfImage pdfImage = new PdfImage(imagePdfRegion.Rectangle,
                Application.StartupPath + @"\DemoFiles\Images\HiQPdfLogo_Modified.png");
            pdfImage.AlphaBlending = true;
            pdfImage.ClipRectangle = imagePdfRegion.Rectangle;

            imagePdfPage.Layout(pdfImage);
        }

        document.WriteToFile(pdfFile);

    }
    catch (Exception ex)
    {
        MessageBox.Show(String.Format("Cannot create the PDF document. {0}", ex.Message));
        return;
    }
    finally
    {
        if (document != null)
            document.Close();
        Cursor = Cursors.Arrow;
    }

    // open the created PDF document
    try
    {
        System.Diagnostics.Process.Start(pdfFile);
    }
    catch (Exception ex)
    {
        MessageBox.Show(String.Format("The PDF document was created but cannot open '{0}'. {1}", pdfFile, ex.Message));
    }
}
See Also

Other Resources