Web/HTML To PDF REST API for C# (CSharp) Samples

SelectPdf HTML to PDF online API provides an easy to use, powerful tool that can be used to generate fully structured PDFs with just a few lines of code.

SelectPdf is a web service that can be called from any programming language: Java, PHP, .NET, Ruby, Perl, Python, Javascript, etc. Try SelectPdf API to experience SaaS at its best.

The following examples are in C# for SelectPdf Online REST API. Make sure that you are interested in the online API, because if you are interested in the .NET library, there are other places where you can find C# samples for SelectPdf .NET Library.

Until recently, we did not provide any .NET samples for the online API, because the recommended product if .NET was available, was our SelectPdf Library for .NET. What changed is that more and more users are moving to systems where the full .NET Framework (required by our .NET library) is not available (they use Azure websites or .NET Core or Mono), but they still code in VB.NET or C#. Here, the online Html to Pdf API comes into play.

There are a variety of methods in .NET that you can use to access a REST API. We will show you 3 of them in this article. One important thing to remember is that the PDF is returned to the client in binary form. Trying to read this binary format as text will cause problems and your PDF will not be saved correctly. So, remember, binary mode for PDF reading.

Later Edit: The recommended approach to access SelectPdf Online REST API from C# is to use the dedicated .NET Client Library for SelectPdf API. Alternatively, you can write your own code to access the API. Below are some examples for this approach.

Example #1 – How to use C# to convert a webpage to PDF and save it on the disk using HttpWebRequest / HttpWebResponse

This code converts an url to pdf in C# using SelectPdf REST API through a POST request. It uses HttpWebRequest / HttpWebResponse (and Newtonsoft for JSON serialization). The content is saved into a file on the disk.

Example #2 – How to use C# to convert a webpage to PDF and save it on the disk using WebClient

This code converts an url to pdf in C# using SelectPdf HTML to PDF REST API through a POST request. It uses WebClient (and Newtonsoft for JSON serialization). The content is saved into a file on the disk.

Example #3 – How to use C# to convert a webpage to PDF and save it on the disk using RestSharp

This code converts an url to pdf in C# using SelectPdf Online Web Service through a POST request. It uses RestSharp for the HTTP communication with the API. The content is saved into a file on the disk.

The above samples use only the 2 mandatory parameters (key and url or html) for the html to pdf conversion. For the full list of optional parameters, take a look at SelectPdf HTML To PDF API – SaaS page. Using the API optional parameters, you can control the page size, orientation and margins in the pdf document, the document information and security, the headers and footers and a lot more.

Hopefully these examples will help you to be on your way using the API for URL to PDF conversion in C#.