SelectPdf for .NET - Get Web Elements Location in PDF with Html to Pdf Converter - VB.NET / ASP.NET MVC Sample

This sample shows how the html To pdf converter can locate certain web elements In the generated PDF Using SelectPdf Pdf Library For .NET.

The web elements are defined Using CSS selectors. 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 specified web elements will be marked/highlighted In PDF With a blue rectangle.

Url :

Web elements :

Sample Code VB.NET

Imports SelectPdf
Imports System.Drawing

Namespace Controllers
    Public Class WebElementsLocationController
        Inherits Controller

        ' GET: WebElementsLocation
        Function Index() As ActionResult
            Return View()
        End Function

        Public Function SubmitAction(fields As FormCollection) As ActionResult
            ' instantiate a html to pdf converter object
            Dim converter As New HtmlToPdf()

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

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

            ' get all web elements and mark/highlight them with a green rectangle
            For Each element As WebElement In
                ' each web element has several rendering rectangles 
                ' (if it is displayed in more than 1 page)
                For Each webElementRectangle As WebElementPdfRectangle In
                    ' 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


            ' save pdf document
            Dim pdf As Byte() = doc.Save()

            ' close pdf document

            ' return resulted pdf document
            Dim fileResult As FileResult = New FileContentResult(pdf, "application/pdf")
            fileResult.FileDownloadName = "Document.pdf"
            Return fileResult
        End Function
    End Class
End Namespace