Click or drag to resize
Pdf Library for .NET

Getting Started with Html to Pdf Converter for .NET

SelectPdf Html To Pdf Converter - Community Edition provides a powerful and easy to use free html to pdf converter that can be used in any .NET applications to generate pdf documents from web pages and html code.

SelectPdf Html To Pdf Converter - Community Edition provides most of the features of the Professional Select.Pdf Library for .NET. The main limitation is related to the number of pages of the generated pdf documents. The community edition generates pdf documents up to 5 pages.

Quick Start

Using the Html to Pdf Converter is very easy. The first thing that needs to be done is the namespace importing.

using SelectPdf.Universal;

Sample code that shows how to convert an url to pdf using Free Select.Pdf Html To Pdf Converter for .NET:

// instantiate a html to pdf converter object
HtmlToPdf converter = new HtmlToPdf();

// create a new pdf document converting an url
PdfDocument doc = converter.ConvertUrl(url);

// save pdf document
doc.Save("Sample.pdf");

// close pdf document
doc.Close();

Sample code that shows how to convert raw html code to pdf using Free Select.Pdf Html To Pdf Converter for .NET:

// instantiate a html to pdf converter object
HtmlToPdf converter = new HtmlToPdf();

// create a new pdf document converting the html code
PdfDocument doc = converter.ConvertHtmlString(htmlString, baseUrl);

// save pdf document
doc.Save("Sample.pdf");

// close pdf document
doc.Close();

PdfDocument implements IDisposable, so a using statement closes the document too. Calling Close and disposing the document are the same; doing both is safe.

using (PdfDocument doc = converter.ConvertUrl(url))
{
    doc.Save("Sample.pdf");
}
Documents longer than 5 pages

When the converted content is longer than 5 pages, the Community Edition keeps the first 5 and the conversion still succeeds. To know whether pages were left out, compare PdfPageCountBeforeLimit, the number of pages the content produced, with PdfPageCount, the number of pages in the document, after the conversion:

HtmlToPdf converter = new HtmlToPdf();
PdfDocument doc = converter.ConvertUrl(url);

HtmlToPdfResult result = converter.ConversionResult;
if (result.PdfPageCountBeforeLimit > result.PdfPageCount)
{
    int missing = result.PdfPageCountBeforeLimit - result.PdfPageCount;
    Console.WriteLine(missing + " page(s) were not included.");
}

doc.Save("Sample.pdf");
doc.Close();

The licensed SelectPdf.Universal package has no page limit; there the two values are always equal.

Html to Pdf Converter Properties

Options - controls the html to pdf conversion process and the generated pdf document properties.

Header - controls the properties of the custom header of the generated pdf document.

Footer - controls the properties of the custom footer of the generated pdf document.

ConversionResult - offers information about the result of the html to pdf conversion process.

These properties will be described in details in the next sections of the Developers Guide.

See Also