SelectPdf for .NET - Control Page Breaks with Html to Pdf Converter - VB.NET / ASP.NET Sample

This sample shows how to use SelectPdf html to pdf converter to convert a raw html code to pdf and also how to control the page breaks from the html code.

To insert a page break in pdf, the html code must have an element with the style page-break-before: always or page-break-after: always. Before or after that specific element, a page break will be inserted into the generated pdf document. The element can be anything (image, table, table row, div, text, etc).

To instruct a certain section from the content to remain on the same page in the generated pdf document, an element with the style page-break-inside: avoid must be used.

Html Code:


Base Url:


Sample Code Vb.Net



Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports SelectPdf

Namespace SelectPdf.Samples
    Partial Public Class page_breaks
        Inherits System.Web.UI.Page

        Protected Sub Page_Load(sender As Object, e As EventArgs)
            If Not IsPostBack Then
                TxtHtmlCode.Text = Helper.PageBreaksText()
            End If
        End Sub

        Protected Sub BtnCreatePdf_Click(sender As Object, e As EventArgs)
            ' read parameters from the webpage
            Dim htmlString As String = TxtHtmlCode.Text
            Dim baseUrl As String = TxtBaseUrl.Text

            ' instantiate a html to pdf converter object
            Dim converter As New HtmlToPdf()

            converter.Options.MarginTop = 10
            converter.Options.MarginBottom = 10
            converter.Options.MarginLeft = 10
            converter.Options.MarginRight = 10

            ' create a new pdf document converting an url
            Dim doc As PdfDocument = converter.ConvertHtmlString(htmlString, baseUrl)

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

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