Posted by & filed under HTML to PDF API, HTML to PDF conversion.

Converting HTML to PDF in C# is an easy task with our API. Today we’ll be going over the functions in this library and we’ll show you code examples in C# that help you turn HTML into PDF.

Our HTML to PDF library works on any platform that has a HTTP connection and supports the following functions for HTML to PDF conversion and enhancing of the PDF:

  1. General options such as page format, margins, single page PDF creation etc
  2. Protection options such as password protection, encryption and rights management
  3. Watermarking or stamping

Basic API usage in C#

In order to use our API to get a PDF you just send a HTTP request with the right information. It’s like typing a URL in the browser address bar. The URL that you use contains the parameters that are the commands that control the API. In it’s simplest form you only pass an API key and a URL and we’ll then return the PDF based on the settings that you have defaulted in your members area.

The HTTP request for getting ‘’ as PDF looks like this:

In C# you’d code this as follows:

string apiKey = "yourapikey";
string url = "";

using (var client = new WebClient())
client.QueryString.Add("apikey", apiKey);
client.QueryString.Add("url", url);
client.DownloadFile("", @"c:\temp\mypdf.pdf");

So you don’t need to install any libraries or nothing. This works out of the box as long as you have subscribed to our simple HTML to PDF API and use your API key.

Additional options for HTML to PDF conversion in C#

As we described earlier, your members area has quite a few defaults that you can easily control there. So if you always need the same page format, margins, header, footer etc etc – you can just edit them there very easily. If you want to have some additional power to control each and every single PDF then you’ll use the additional options, which we’ll describe now.

General options

The table below shows all general options you can add to the HTTP request in order to change the layout of the PDF.

widthCustom width of the PDF
heightCustom height of the PDF, if you specify the width, but not the height, then we fit the whole page into a one page PDF by auto-adjusting the height of the PDF.
leftLeft margin
rightRight margin
topTop margin
bottomBottom margin
unitUnit for size & margin settings. Can be ‘mm’ (millimeters), ‘in’ (inches) or ‘pt’ (points).
We’ll use mm in case you don’t specify a unit.
popupIf you set this to y we will return a URL to the PDF on our server instead of the actual PDF.
This allows you to generate the PDF, without having to use bandwith to serve it to your visitors.

Encryption and protection options

The table below shows the options you have to encrypt the PDF and use protection and rights management options.

encryptionSpecifies the level of encryption for the PDF. You can choose the following values:

  • 40 for 40-bit RC4 encryption.
  • 128 for 128-bit RC4 encryption, requires Acrobat reader 5 or later
  • 128aes for 128-bit AES encryption, requires Acrobat reader 7 or later
  • 256 for 256-bit AES encryption, requires Acrobat reader X or later
ownerpassSpecifies the master password that controls all rights management settings for the PDF.
userpassSpecifies the password a user needs to open the PDF.
Also known as ‘document open password’.
noprintIf you pass this parameter, Acrobat Reader will prevent users from printing the PDF.
nocopyIf you pass this parameter, Acrobat Reader will prevent users from copying content from the PDF.
nomodifyIf you pass this parameter, Acrobat Reader will prevent users from modifying the PDF.

Watermarking and stamping

If you’d like to watermark or stamp your PDF you can do so with any image that you provide. We’ll add it to your PDF on every page automaticly. We consider stamping to be the same as watermarking, but then with an opacity of 1 (meaning fully opague).

The table below shows the parameters you can pass to control the watermark on your PDF.

wmSpecifies the full URL where we can find the image you want to use as watermark.
Please make sure the image is really there.
wm_xSpecifies the horizontal position where you want the watermark to start.
wm_ySpecifies the vertical position where you want the watermark to start.
wm_angleSpecifies the angle of rotation (in degrees) you want for your watermark.
wm_opacSpecifies the level of opacity you want for your watermark.
This is a “real” number between 0 and 1. 0 is fully transparant and 1 is fully opague (what some call a ‘stamp’).

Example with additional options in C#

Now let’s look at what happens if we want to pass additional parameters to control the PDF. In our initial example we were using the C# WebClient() object and then specified two parameters that we added to the query string: apikey & url. Since we have seen the other parameters now we can simple select the ones we want and add those to the query string as well.

So let’s say we want to protect our PDF with password ‘htm2pdf’ and we also would like to watermark it with the logo of our service (who wouldn’t want that?). We’ll use an opacity of 0.5 to make it half transparant and we’ll rotate it 50 degrees and put it on position 100, 100.

We would then do the following in C#:

string apiKey = "yourapikey";
string url = "";
string userpass = "htm2pdf";
string wm = "";
string wm_opac = "0.5";
string wm_angle = "50";
string wm_x = "100";
string wm_y = "100";
using (var client = new WebClient()) {
client.QueryString.Add("apikey", apiKey);
client.QueryString.Add("url", url);
client.QueryString.Add("userpass", userpass);
client.QueryString.Add("wm", wm);
client.QueryString.Add("wm_opac", wm_opac);
client.QueryString.Add("wm_angle", wm_angle);
client.QueryString.Add("wm_x", wm_x);
client.QueryString.Add("wm_y", wm_y);
client.DownloadFile("", @"c:\temp\mypdf.pdf");

Conclusion on the PDF C# library

As you can see the HTML to PDF API is very easy-to-use in C# and will work right off the bat without installing anything. If you’d like to learn more about all the options then please see the full documentation on the HTML to PDF API. If you think this is awesome then feel free to sign up now!

Leave a Reply

You must be logged in to post a comment.