SelectPdf Library for .NET

Written by

in

Integrating SelectPdf Library for .NET into C# and VB.NET Applications

Generating high-quality PDF documents directly from HTML code or web pages is a common requirement in modern enterprise applications. The SelectPdf library for .NET provides developers with a robust, fast, and feature-rich HTML-to-PDF conversion engine. It operates completely independently of external software, requiring no third-party tools or drivers to run.

This article provides a step-by-step technical guide on how to integrate the SelectPdf library into your C# and VB.NET applications to convert HTML text and URLs into polished PDF files. Prerequisites and Installation

Before writing any code, ensure your environment meets the necessary system requirements. SelectPdf supports .NET Framework 2.0 up to the latest version of .NET Core, .NET 5, 6, 7, 8, and beyond. Step 1: Install via NuGet Package Manager

The easiest way to add SelectPdf to your project is through NuGet. Open your Package Manager Console and run the following command: Install-Package Select.Pdf Use code with caution.

Alternatively, you can open the NuGet Package Manager UI in Visual Studio, search for Select.Pdf, and click Install. Core Implementation in C# and VB.NET

SelectPdf treats HTML strings and web URLs identically during rendering, using an internal WebKit-based rendering engine to paint the visual elements accurately before saving them to disk. 1. Converting a URL to PDF

The HtmlToPdf class is the primary interface for processing conversion tasks. The ConvertUrl method fetches the page contents, processes JavaScript/CSS, and outputs a PdfDocument. C# Implementation

using System; using SelectPdf; namespace PdfIntegrationDemo { class Program { static void Main(string[] args) { // Instantiate the converter HtmlToPdf converter = new HtmlToPdf(); // Convert the specified web page URL string url = “https://example.com”; PdfDocument doc = converter.ConvertUrl(url); // Save the document to a file doc.Save(“UrlOutput.pdf”); // Close the document to release resources doc.Close(); Console.WriteLine(“PDF successfully generated from URL.”); } } } Use code with caution. VB.NET Implementation

Imports SelectPdf Module Program Sub Main() ‘ Instantiate the converter Dim converter As New HtmlToPdf() ’ Convert the specified web page URL Dim url As String = “https://example.com” Dim doc As PdfDocument = converter.ConvertUrl(url) ‘ Save the document to a file doc.Save(“UrlOutput.pdf”) ’ Close the document to release resources doc.Close() Console.WriteLine(“PDF successfully generated from URL.”) End Sub End Module Use code with caution. 2. Converting Raw HTML Strings to PDF

If your application generates dynamic HTML reports, invoices, or receipts natively in code, use the ConvertHtmlString method. C# Implementation Use code with caution. VB.NET Implementation Use code with caution. Customizing Layouts and Page Settings

Enterprise production environments often demand advanced layouts, including specific margins, standard page scaling, header banners, or document authentication credentials. Setting Page Orientations, Margins, and Size

You can tune global render layouts via the Options property exposed on your HtmlToPdf instance. C# Example

HtmlToPdf converter = new HtmlToPdf(); // Adjust structural layout configurations converter.Options.PdfPageSize = PdfPageSize.A4; converter.Options.PdfPageOrientation = PdfPageOrientation.Landscape; converter.Options.MarginLeft = 30; converter.Options.MarginRight = 30; converter.Options.MarginTop = 20; converter.Options.MarginBottom = 20; // Enforce WebKit viewports to match screen breakpoints converter.Options.WebPageWidth = 1024; Use code with caution. VB.NET Example

Dim converter As New HtmlToPdf() ‘ Adjust structural layout configurations converter.Options.PdfPageSize = PdfPageSize.A4 converter.Options.PdfPageOrientation = PdfPageOrientation.Landscape converter.Options.MarginLeft = 30 converter.Options.MarginRight = 30 converter.Options.MarginTop = 20 converter.Options.MarginBottom = 20 ’ Enforce WebKit viewports to match screen breakpoints converter.Options.WebPageWidth = 1024 Use code with caution. Adding Dynamic Page Headers and Footers

SelectPdf offers the capability to construct repeating headers and footers with page-number token templates like {%PageNumber%} and {%TotalPages%}. C# Example

// Enable headers and footers in general settings converter.Options.DisplayHeader = true; converter.Options.DisplayFooter = true; // Build header content converter.Header.Height = 50; PdfTextElement headerText = new PdfTextElement(10, 10, “Corporate Annual Audit”, new System.Drawing.Font(“Arial”, 10)); converter.Header.Add(headerText); // Build footer content with automated dynamic tokens converter.Footer.Height = 30; PdfTextElement footerText = new PdfTextElement(10, 5, “Page {%PageNumber%} of {%TotalPages%}”, new System.Drawing.Font(“Arial”, 9)); converter.Footer.Add(footerText); Use code with caution. VB.NET Example

’ Enable headers and footers in general settings converter.Options.DisplayHeader = True converter.Options.DisplayFooter = True ‘ Build header content converter.Header.Height = 50 Dim headerText As New PdfTextElement(10, 10, “Corporate Annual Audit”, New System.Drawing.Font(“Arial”, 10)) converter.Header.Add(headerText) ’ Build footer content with automated dynamic tokens converter.Footer.Height = 30 Dim footerText As New PdfTextElement(10, 5, “Page {%PageNumber%} of {%TotalPages%}”, New System.Drawing.Font(“Arial”, 9)) converter.Footer.Add(footerText) Use code with caution. Summary of Integration Benefits

Integrating SelectPdf into C# or VB.NET apps streamlines document rendering for multiple reasons:

Zero Dependencies: Does not require installations of external browsers like Chrome or server-level printer engines.

Modern CSS Support: Adapts complex layouts gracefully, converting media assets, advanced fonts, tables, and standard JavaScript calculations cleanly into layout vectors.

Resource Optimization: Features deterministic API pipelines where calling .Close() strictly unloads massive underlying structures, preserving critical web server RAM.

By leveraging these clean, manageable code patterns across both C# and VB.NET languages, engineering teams can build reliable, automated reporting structures directly inside their .NET ecosystems.

If you would like to expand your implementation, let me know:

What project type you are using (.NET Core API, MVC, WPF, or Legacy WebForms)?

Do your HTML files reference external CSS frameworks (like Bootstrap or Tailwind)?

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *