Imports System.Drawing

Public Class web_elements_location
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
        Handles Me.Load
        If Not IsPostBack Then
            TxtUrl.Text = "https://selectpdf.com"
        End If
    End Sub

    Protected Sub BtnCreatePdf_Click(sender As Object, e As EventArgs)

        Dim renderingEngine As RenderingEngine = CType([Enum].Parse(GetType(RenderingEngine), DdlRenderingEngine.SelectedValue, True), RenderingEngine)

        ' instantiate a html to pdf converter object
        Dim converter As New HtmlToPdf()
        converter.Options.RenderingEngine = renderingEngine
        converter.Options.CefEnginePath = Server.MapPath(ConfigurationManager.AppSettings("CefEnginePath"))

        ' set the css selectors for the web elements
        ' whose positions will be retrieved
        converter.Options.WebElementsMappingOptions.CssSelectors = _
            TxtElements.Text.Split(New Char() {","c})

        ' create a new pdf document converting an url
        Dim doc As PdfDocument = converter.ConvertUrl(TxtUrl.Text)

        ' get all web elements and mark/highlight them with a green rectangle
        For Each element As WebElement In _
            converter.Options.WebElementsMappingOptions.Result
            ' each web element has several rendering rectangles 
            ' (if it is displayed in more than 1 page)
            For Each webElementRectangle As WebElementPdfRectangle In _
                element.PdfRectangles
                ' get the PDF page
                Dim page As PdfPage = doc.Pages(webElementRectangle.PageIndex)
                Dim rectangle As RectangleF = webElementRectangle.Rectangle

                ' highlight the HTML element
                Dim blueRectangle As New PdfRectangleElement( _
                    rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height)
                blueRectangle.ForeColor = Color.Blue

                page.Add(blueRectangle)
            Next
        Next

        ' save pdf document
        doc.Save(Response, False, "Sample.pdf")

        ' close pdf document
        doc.Close()
    End Sub
End Class