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.
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 PDFPDFmyURL 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:
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.
Property | Description |
---|---|
PageSize | set the page size to one of the standard page formats like A4, B0, Letter etc |
PageOrientation | set the orientation to either portrait or landscape |
PageDimensions | set the page size to exact dimensions or force a single page PDF |
Margins | set the margins |
DimensionUnit | set the unit of measurement for custom dimensions to mm, in or pt |
Property | Description |
---|---|
Header | Define the header of the PDF in HTML |
Footer | Define the footer of the PDF in HTML |
PageOffset | Define 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.
Property | Description |
---|---|
VisibleContent | Show only content with this/these DIV id(s) |
HiddenContent | Hide 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'.
Property | Description |
---|---|
Watermark | The options of a watermark |
Background | The options of a custom backgorund |
The following properties allow you to protect your PDFs with passwords and against printing, content copying and annotation.
Property | Description |
---|---|
EncryptionLevel | The encryption level for the PDF |
Permissions | The permissions on the PDF for everyone except the owner (to disallow copying, printing or editing) |
UserPassword | The 'document open' password |
OwnerPassword | The owner password |
The following properties allow you to define the way we can access your URLs in secure member areas.
Property | Description |
---|---|
Credentials | User name and password for basic HTTP Authentication |
SessionID | Cookie with the JSESSIONID |
CookieJar | Contents of a cookie jar |
NetscapeCookieJar | Contents of a cookie jar in the old Netscape format |
FormURL | URL of the form that we need to log into |
FormFields | List 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.
Property | Description |
---|---|
EndPoint | the endpoint for conversion - by default http://pdfmyurl.com/api |
License | the license key of your subscription |
CssMediaType | CSS media type that controls screen layout or print-friendly layout |
Grayscale | force a PDF in grayscale |
NoImages | disable images |
NoBackground | disable the background |
NoInternalLinks | disable links within the domain |
NoExternalLinks | disable links to external sites |
NoJavaScript | disable JavaScript |
JavaScriptDelay | the amount of time in msec that JavaScript waits to complete |
Title | the title of the PDF |
ZoomFactor | the amount of zoom we use during creation of the PDF |
The .NET component lets you convert a URL to PDF or raw HTML to PDF with the functions listed below.
Function | Description | Parameters |
---|---|---|
ConvertURL | Converts 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 |
ConvertHTML | Converts a HTML string into PDF | html - 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 |
When the conversion is done you can handle the downloaded result or the exception with the following events below.
Event | Occurs when | Parameters |
---|---|---|
DownloadCompleted | The download has completed successfully | sender - Contains the object that raised this event e - DownloadCompletedEventArgs object containing event-specific arguments |
WebException | The download is interrupted | sender - 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.
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); }
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 asynchronouslyThis 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
For business use