.NET component for webpage or HTML to PDF conversion

We have created a .NET component lets you use the full functionality of our API in C# or VB.net. It lets you add web page and HTML to PDF conversion functionality in Windows and web applications and it also has built in exception handling. It is fully object oriented and conforms to the Microsoft standards for .NET framework components.

Simply download the 32-bit DLL or the 64-bit DLL and add PDFmyURL.NET.dll to your project and import the PDFmyURLdotNET namespace to start using our API. Below is the full documentation of the wrapper and you can also read more about it in the readme.txt file.

Basic usage

Below are some basic examples that show how you would use our wrapper to convert a web page to PDF. Further down the page you'll find more examples.

C# - convert a URL to PDF
PDFmyURL pdf = new PDFmyURL("yourlicensekey");
pdf.ConvertURL("http://www.example.com", Application.StartupPath + @"\example.pdf");
VB.net - convert a URL to PDF
Dim pdf As New PDFmyURL("yourlicensekey")
pdf.ConvertURL("http://www.example.com", Application.StartupPath & "\example.pdf")

Of course you can convert any web page as well as raw HTML with the .NET component. And you can set many options to control the PDF layout and the conversion.

You will use the component in the following sequence:

  1. Use properties to set options such as page size, margins, headers etc.
  2. Use one of the two functions to convert from a URL or HTML
  3. Handle the events to use the downloaded PDF or the exception that occurred

Properties for setting options

Below is a list of all properties that you can use to set options for the conversion, the page layout and dimensions, margins, headers and footers, watermarking and much more.

The following properties allow you to customize the page dimensions and margins of your PDFs.

PropertyDescription
PageSizeset the page size to one of the standard page formats like A4, B0, Letter etc
PageOrientationset the orientation to either portrait or landscape
PageDimensionsset the page size to exact dimensions or force a single page PDF
Marginsset the margins
DimensionUnitset the unit of measurement for custom dimensions to mm, in or pt

The following properties allow you to customize the headers and footers of your PDFs.

PropertyDescription
HeaderDefine the header of the PDF in HTML
FooterDefine the footer of the PDF in HTML
PageOffsetDefine the offset for page numbering

The following properties allow you to show or hide specific parts of your webpage for the conversion. See the documentation on partial page conversion for more info.

PropertyDescription
VisibleContentShow only content with this/these DIV id(s)
HiddenContentHide all content with this/these DIV id(s)

The following properties allow you to put a watermark over your PDFs or underneath your content, in which case we call it 'background'.

PropertyDescription
WatermarkThe options of a watermark
BackgroundThe options of a custom backgorund

The following properties allow you to protect your PDFs with passwords and against printing, content copying and annotation.

PropertyDescription
EncryptionLevelThe encryption level for the PDF
PermissionsThe permissions on the PDF for everyone except the owner (to disallow copying, printing or editing)
UserPasswordThe 'document open' password
OwnerPasswordThe owner password

The following properties allow you to define the way we can access your URLs in secure member areas.

PropertyDescription
CredentialsUser name and password for basic HTTP Authentication
SessionIDCookie with the JSESSIONID
CookieJarContents of a cookie jar
NetscapeCookieJarContents of a cookie jar in the old Netscape format
FormURLURL of the form that we need to log into
FormFieldsList of input fields and values of the form that we need to log into

The following properties allow you to set various other options to control the conversion.

PropertyDescription
EndPointthe endpoint for conversion - by default http://pdfmyurl.com/api
Licensethe license key of your subscription
CssMediaTypeCSS media type that controls screen layout or print-friendly layout
Grayscaleforce a PDF in grayscale
NoImagesdisable images
NoBackgrounddisable the background
NoInternalLinksdisable links within the domain
NoExternalLinksdisable links to external sites
NoJavaScriptdisable JavaScript
JavaScriptDelaythe amount of time in msec that JavaScript waits to complete
Titlethe title of the PDF
ZoomFactorthe amount of zoom we use during creation of the PDF

Functions for conversion from a URL or HTML

The .NET component lets you convert a URL to PDF or raw HTML to PDF with the functions listed below.

FunctionDescriptionParameters
ConvertURLConverts a URL into PDF url - URL of the web page to be converted
fileName - Optional local filename where the PDF document will be saved
async - Optional value indicating whether the PDF will be downloaded asynchronously
ConvertHTMLConverts a HTML string into PDFhtml - String value with HTML to be converted
fileName - Optional local filename where the PDF document will be saved
async - Optional value indicating whether the PDF will be downloaded asynchronously

Events for using the conversion result

When the conversion is done you can handle the downloaded result or the exception with the following events below.

EventOccurs whenParameters
DownloadCompletedThe download has completed successfullysender - Contains the object that raised this event
e - DownloadCompletedEventArgs object containing event-specific arguments
WebExceptionThe download is interruptedsender - Contains the object that raised this event
e - WebExceptionEventArgs object containing event-specific arguments

In addition, all properties listed above also contain their own individual PropertyChanged events. For example when the Margins property changes, the corresponding MarginsChanged event is raised.

Exception handling

If the download succeeds the DownloadCompleted event is raised and otherwise the WebException event is raised. This enables you to handle the exceptions properly.

In very rare cases you cases, such as invalid HTML or URL input, an exception error can still be raised. Therefore it makes sense to wrap your functions inside a try/catch statement to check the details of the error.

You can do so with the following try / catch code.

try
{
    pdf.ConvertURL("http://www.example.com", string.Empty);
}
catch (Exception ex)
{
    MessageBox.Show("Error: Unable to convert URL - " + ex.Message);
}

Further examples

Below are a few more examples of how you can use the .NET component to convert web pages to PDF. Please reach out to us if you'd like to see different examples or if you have other questions about web to pdf conversion in C# or VB.net.

C# - convert a URL to PDF asynchronously

This example shows how you can use the DownloadCompleted and WebException events as well as force the conversion to happen asynchronously.

private PDFmyURL pdf;

pdf = new PDFmyURL("yourlicensekey");
pdf.DownloadCompleted += pdf_DownloadCompleted;
pdf.WebException += pdf_WebException;
pdf.ConvertURL("http://www.example.com", Application.StartupPath + @"\example.pdf", true);

private void pdf_DownloadCompleted(object sender, DownloadCompletedEventArgs e)
{
    MessageBox.Show("Download completed successfully - Size: " + e.Data.Length.ToString() + " bytes");
}

private void pdf_WebException(object sender, WebExceptionEventArgs e)
{
    MessageBox.Show("Download failed - " + e.StatusCode.ToString() + ": " + PDFmyURL.StatusCodeDescription(e.StatusCode));
}
VB.net - convert a URL to PDF asynchronously
Private pdf As PDFmyURL

pdf = New PDFmyURL("yourlicensekey")
AddHandler pdf.DownloadCompleted, AddressOf pdf_DownloadCompleted
AddHandler pdf.WebException, AddressOf pdf_WebException
pdf.ConvertURL("http://www.example.com", Application.StartupPath & "\example.pdf", True)

Private Sub pdf_DownloadCompleted(sender As Object, e As DownloadCompletedEventArgs)
    MessageBox.Show("Download completed successfully - Size: " & e.Data.Length.ToString() & " bytes")
End Sub

Private Sub pdf_WebException(sender As Object, e As WebExceptionEventArgs)
    MessageBox.Show("Download failed - " & e.StatusCode.ToString() & ": " & PDFmyURL.StatusCodeDescription(e.StatusCode))
End Sub