SelectPdf Online REST API is a platform independent PDF manipulation API. SelectPdf REST API is cloud based and it can be used with any language: .NET (C# or VB.NET), Java, PHP, Python, Go, Ruby, Node.js, Perl and many more. We are presenting today the dedicated .NET client library for SelectPdf API.
Using the SelectPdf Online REST API .NET client library you can easily take advance of the API features offered by SelectPdf:
HTML to PDF REST API – SelectPdf HTML To PDF Online REST API is a professional solution that lets you create PDF from web pages and raw HTML code in your applications.
PDF to TEXT REST API – SelectPdf Pdf To Text REST API is an online solution that lets you extract text from your PDF documents or search your PDF document for certain words.
PDF Merge REST API – SelectPdf Pdf Merge REST API is an online solution that lets you merge local or remote PDFs into a final PDF document.
All these APIs can be easily integrated with .NET using the dedicated client library, that can be used with .NET Framework 2.0 and above, .NET Core 2.1 and above, .NET 5, .NET 6, etc.
Installation
Install SelectPdf .NET Client for Online API via Nuget: SelectPdf API on Nuget.
OR
Download selectpdf-api-dotnet-client-1.4.0.zip, unzip it and add a reference to SelectPdf.Api.dll to your project.
OR
Clone selectpdf-api-dotnet-client from Github and install the library.
cd selectpdf-api-dotnet-client
Get a trial key for SelectPdf online REST API
Once the library is installed, you need a key to be able to access the API.
GET A DEMO LICENSE KEY NOW
The free trial key for the online API is valid for 7 days and it includes 200 conversions.
Sample Code
The .NET client library makes accessing SelectPdf online REST API very easy. Here are a few samples that present the main features of the API. For details and full list of parameters access the SelectPdf API .NET Client Documentation or the individual pages of the APIs: HTML to PDF API or PDF to TEXT API or PDF Merge API.
Convert HTML to PDF in .NET
The following sample shows the main features of the HTML To PDF API. Comment/uncomment code to convert an url to file or memory or also convert raw HTML to file or memory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
using System; using SelectPdf.Api; namespace SelectPdf.Api.Tests { class Program { static void Main(string[] args) { string url = "https://selectpdf.com"; string localFile = "Test.pdf"; string apiKey = "Your API key here"; Console.WriteLine("This is SelectPdf-{0}.", ApiClient.CLIENT_VERSION); try { HtmlToPdfClient client = new HtmlToPdfClient(apiKey); // set parameters - see full list at https://selectpdf.com/html-to-pdf-api/ client // main properties .setPageSize(PageSize.A4) // PDF page size .setPageOrientation(PageOrientation.Portrait) // PDF page orientation .setMargins(0) // PDF page margins .setRenderingEngine(RenderingEngine.WebKit) // rendering engine .setConversionDelay(1) // conversion delay .setNavigationTimeout(30) // navigation timeout .setShowPageNumbers(false) // page numbers .setPageBreaksEnhancedAlgorithm(true) // enhanced page break algorithm // additional properties // .setUseCssPrint(true) // enable CSS media print // .setDisableJavascript(true) // disable javascript // .setDisableInternalLinks(true) // disable internal links // .setDisableExternalLinks(true) // disable external links // .setKeepImagesTogether(true) // keep images together // .setScaleImages(true) // scale images to create smaller pdfs // .setSinglePagePdf(true) // generate a single page PDF // .setUserPassword("password") // secure the PDF with a password // generate automatic bookmarks // .setPdfBookmarksSelectors("H1, H2") // create outlines (bookmarks) for the specified elements // .setViewerPageMode(PageMode.UseOutlines) // display outlines (bookmarks) in viewer ; Console.WriteLine("Starting conversion ..."); // convert url to file client.convertUrlToFile(url, localFile); // convert url to memory // byte[] pdf = client.convertUrl(url); // convert html string to file // client.convertHtmlStringToFile("This is some <b>html</b>.", localFile); // convert html string to memory // byte[] pdf = client.convertHtmlString("This is some <b>html</b>."); Console.WriteLine("Finished! Number of pages: {0}.", client.getNumberOfPages()); // get API usage UsageClient usageClient = new UsageClient(apiKey); UsageInformation usage = usageClient.getUsage(false); Console.WriteLine("Conversions remained this month: {0}.", usage.Available); } catch (Exception ex) { Console.WriteLine("An error occurred: " + ex.Message); } } } } |
Convert HTML to PDF with custom header/footer in .NET
The following sample shows how to convert a web page to PDF and also setting a custom header or footer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
using System; using SelectPdf.Api; namespace SelectPdf.Api.Tests { class Program { static void Main(string[] args) { string url = "https://selectpdf.com"; string localFile = "Test.pdf"; string apiKey = "Your API key here"; Console.WriteLine("This is SelectPdf-{0}.", ApiClient.CLIENT_VERSION); try { HtmlToPdfClient client = new HtmlToPdfClient(apiKey); // set parameters - see full list at https://selectpdf.com/html-to-pdf-api/ client .setMargins(0) // PDF page margins .setPageBreaksEnhancedAlgorithm(true) // enhanced page break algorithm // header properties .setShowHeader(true) // display header // .setHeaderHeight(50) // header height // .setHeaderUrl(url) // header url .setHeaderHtml("This is the <b>HEADER</b>!!!!") // header html // footer properties .setShowFooter(true) // display footer // .setFooterHeight(60) // footer height // .setFooterUrl(url) // footer url .setFooterHtml("This is the <b>FOOTER</b>!!!!") // footer html // footer page numbers .setShowPageNumbers(true) // show page numbers in footer .setPageNumbersTemplate("{page_number} / {total_pages}") // page numbers template .setPageNumbersFontName("Verdana") // page numbers font name .setPageNumbersFontSize(12) // page numbers font size .setPageNumbersAlignment(PageNumbersAlignment.Center) // page numbers alignment (2-Center) ; Console.WriteLine("Starting conversion ..."); // convert url to file client.convertUrlToFile(url, localFile); // convert url to memory // byte[] pdf = client.convertUrl(url); // convert html string to file // client.convertHtmlStringToFile("This is some <b>html</b>.", localFile); // convert html string to memory // byte[] pdf = client.convertHtmlString("This is some <b>html</b>."); Console.WriteLine("Finished! Number of pages: {0}.", client.getNumberOfPages()); // get API usage UsageClient usageClient = new UsageClient(apiKey); UsageInformation usage = usageClient.getUsage(false); Console.WriteLine("Conversions remained this month: {0}.", usage.Available); } catch (Exception ex) { Console.WriteLine("An error occurred: " + ex.Message); } } } } |
Extract text from PDF in .NET
The following sample shows how to extract the text from a PDF document using SelectPdf API. Comment/uncomment code to convert a local PDF or a PDF from a remote url to file or memory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
using System; using SelectPdf.Api; namespace SelectPdf.Api.Tests { class Program { static void Main(string[] args) { string testUrl = "https://selectpdf.com/demo/files/selectpdf.pdf"; string testPdf = "Input.pdf"; string localFile = "Result.txt"; string apiKey = "Your API key here"; Console.WriteLine("This is SelectPdf-{0}.", ApiClient.CLIENT_VERSION); try { PdfToTextClient client = new PdfToTextClient(apiKey); // set parameters - see full list at https://selectpdf.com/pdf-to-text-api/ client .setStartPage(1) // start page (processing starts from here) .setEndPage(0) // end page (set 0 to process file til the end) .setOutputFormat(OutputFormat.Text) // set output format (0-Text or 1-HTML) ; Console.WriteLine("Starting pdf to text ..."); // convert local pdf to local text file client.getTextFromFileToFile(testPdf, localFile); // extract text from local pdf to memory // string text = client.getTextFromFile(testPdf); // print text // Console.WriteLine(text); // convert pdf from public url to local text file // client.getTextFromUrlToFile(testUrl, localFile); // extract text from pdf from public url to memory // string text = client.getTextFromUrl(testUrl); // print text // Console.WriteLine(text); Console.WriteLine("Finished! Number of pages processed: {0}.", client.getNumberOfPages()); // get API usage UsageClient usageClient = new UsageClient(apiKey); UsageInformation usage = usageClient.getUsage(false); Console.WriteLine("Conversions remained this month: {0}.", usage.Available); } catch (Exception ex) { Console.WriteLine("An error occurred: " + ex.Message); } } } } |
Search for text in PDF using .NET
The following sample shows how to search a PDF document for a specific text.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
using System; using SelectPdf.Api; namespace SelectPdf.Api.Tests { class Program { static void Main(string[] args) { string testUrl = "https://selectpdf.com/demo/files/selectpdf.pdf"; string testPdf = "Input.pdf"; string apiKey = "Your API key here"; Console.WriteLine("This is SelectPdf-{0}.", ApiClient.CLIENT_VERSION); try { PdfToTextClient client = new PdfToTextClient(apiKey); // set parameters - see full list at https://selectpdf.com/pdf-to-text-api/ client .setStartPage(1) // start page (processing starts from here) .setEndPage(0) // end page (set 0 to process file til the end) .setOutputFormat(OutputFormat.Text) // set output format (0-Text or 1-HTML) ; Console.WriteLine("Starting search pdf ..."); // search local pdf IList<TextPosition> results = client.searchFile(testPdf, "pdf"); // search pdf from public url // IList<TextPosition> results = client.searchUrl(testUrl, "pdf"); Console.WriteLine("Search results:\n{0}\nSearch results count: {1}.", string.Join("\n", results), results.Count); Console.WriteLine("Finished! Number of pages processed: {0}.", client.getNumberOfPages()); // get API usage UsageClient usageClient = new UsageClient(apiKey); UsageInformation usage = usageClient.getUsage(false); Console.WriteLine("Conversions remained this month: {0}.", usage.Available); } catch (Exception ex) { Console.WriteLine("An error occurred: " + ex.Message); } } } } |
Merge PDFs using .NET
The following sample shows how merge several PDF documents into a final file. The source PDFs can be local files or PDFs from remote urls. The final PDF can be retrieved in memory or saved to a local file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
using System; using SelectPdf.Api; namespace SelectPdf.Api.Tests { class Program { static void Main(string[] args) { string testUrl = "https://selectpdf.com/demo/files/selectpdf.pdf"; string testPdf = "Input.pdf"; string localFile = "Result.pdf"; string apiKey = "Your API key here"; Console.WriteLine("This is SelectPdf-{0}.", ApiClient.CLIENT_VERSION); try { PdfMergeClient client = new PdfMergeClient(apiKey); // set parameters - see full list at https://selectpdf.com/pdf-merge-api/ client // specify the pdf files that will be merged (order will be preserved in the final pdf) .addFile(testPdf) // add PDF from local file .addUrlFile(testUrl) // add PDF From public url // .addFile(testPdf, "pdf_password") // add PDF (that requires a password) from local file // .addUrlFile(testUrl, "pdf_password") // add PDF (that requires a password) from public url ; Console.WriteLine("Starting pdf merge ..."); // merge pdfs to local file client.saveToFile(localFile); // merge pdfs to memory // byte[] pdf = client.save(); Console.WriteLine("Finished! Number of pages: {0}.", client.getNumberOfPages()); // get API usage UsageClient usageClient = new UsageClient(apiKey); UsageInformation usage = usageClient.getUsage(false); Console.WriteLine("Conversions remained this month: {0}.", usage.Available); } catch (Exception ex) { Console.WriteLine("An error occurred: " + ex.Message); } } } } |
The above .NET samples can also be found in GitHub repository in the following versions: C# Samples or VB.NET Samples.