Skip to content

Repository files navigation

ConvertAPI C# Client

Overview

The ConvertAPI library provides a simple and efficient way to integrate the ConvertAPI service into your .NET Framework, .NET Core, and .NET projects. ConvertAPI enables you to easily convert various file formats by leveraging its robust API. Whether you need to convert documents, images, spreadsheets, or other file types, the ConvertAPI library streamlines the process with minimal code and maximum efficiency.

Features

Below is an overview of the key functionalities:

Document Conversion

  • Office to PDF: Convert Word (DOC, DOCX), Excel (XLS, XLSX), and PowerPoint (PPT, PPTX) documents to PDF or PDF/A formats. Example
  • PDF to Office: Transform PDFs into editable Word, Excel, and PowerPoint documents. Example
  • HTML/Web to PDF: Convert HTML files or web pages to PDF using a headless Chrome browser. Example
  • Image to PDF: Convert various image formats (e.g., JPG, PNG, BMP) to PDF. Example
  • PDF to Image: Convert PDFs to image formats such as JPG, PNG, TIFF, SVG, and WebP. Example
  • eBook Conversion: Convert DJVU and other eBook formats to PDF or images. Example
  • Email Conversion: Convert EML and MSG email files to PDF or image formats. Example

Document Processing & Transformation

  • Merge PDF: Combine multiple PDF documents into one PDF. Example
  • Split PDF: Split PDF into several parts, by page ranges, patterns, bookmarks and etc. Example
  • Rotate: Rotate pages in PDF. Example
  • Delete Pages: Remove unwanted pages from PDF. Example
  • Watermarking: Add text or image watermarks to PDF and images. Example
  • Flatten PDFs: Flatten PDF layers to prevent further editing. Example
  • Repair Documents: Recover corrupted or damaged PDF and DOCX files. Example
  • Rasterize PDFs: Convert vector PDFs into raster images. Example

Security & Optimization

  • Password Protection: Encrypt PDF and Office documents with passwords and AES-256 encryption. Example
  • Remove Protection: Unlock password-protected PDFs. Example
  • Redact PDF: Automatically detect and redact sensitive information in PDF files using AI. Example
  • Compress PDF: Reduce PDF sizes by up to 90% without compromising quality. Example
  • PDF/A Conversion: Convert PDFs to PDF/A format for long-term archiving. Example
  • Metadata Management: Edit or remove metadata from PDF documents. Example

Data Extraction & OCR

  • Text Extraction: Extract text content from PDFs, with optional OCR for scanned documents. Example
  • Table Extraction: Extract tabular data from PDFs into CSV or Excel formats. Example
  • Image Extraction: Extract images from PDFs into various image formats. Example
  • Form Data Extraction: Extract form field data from PDFs into FDF format. Example
  • Email Attachments: Extract attachments from EML and MSG email files. Example

Advanced Tools

  • Document Generation: Create DOCX or PDF documents dynamically using templates and JSON data. Example
  • Format Comparison: Compare DOCX documents to identify differences in content and formatting. Example
  • ZIP Archiving: Create ZIP archives from multiple files, with optional password protection. Example

For a complete list of supported conversions and tools, visit the ConvertAPI API Reference.

Compatibility

The ConvertAPI .NET library is compatible with a wide range of platforms that support .NET Standard 2.0+ or .NET Framework 4.0+. Supported environments include:

  • .NET Core 5, 6, 7, 8, 9, and above
  • .NET Standard 2.0 and above
  • .NET Framework 4.0, 4.6.1, 4.7.2, 4.8.1 and above
  • Windows, Linux, macOS
  • Microsoft Azure, Azure App Service, Azure Functions
  • Xamarin (iOS, macOS, Android)
  • Universal Windows Platform (UWP)
  • Web, Console, and Desktop applications

Installation

Run this line from Package Manager Console:

Install-Package ConvertApi

Usage

Configuration

Set credentials

You can get your credentials at https://www.convertapi.com/a/authentication

ConvertApiconvertApi=newConvertApi("api_token");

Set conversion location (optional)

You can choose from multiple conversion locations, including the EU API location for GDPR compliance. However, this selection is optional as ConvertAPI automatically detects the nearest server using GEO DNS. For more details, visit ConvertAPI Servers Location.

ConvertApi.ApiBaseUri="https://v2.convertapi.com";

File conversion

Example to convert file to PDF. All supported formats and options can be found here.

Office to PDF

Convert Word, Excel, and PowerPoint documents to PDF:

varresult=awaitconvertApi.ConvertAsync("docx","pdf",newConvertApiFileParam(@"C:\files\document.docx"));awaitresult.SaveFilesAsync(@"C:\output\");

PDF to Office

Convert PDF to editable Word, Excel, or PowerPoint formats:

varresult=awaitconvertApi.ConvertAsync("pdf","docx",newConvertApiFileParam(@"C:\files\document.pdf"));awaitresult.SaveFilesAsync(@"C:\output\");

HTML/Web to PDF

Convert HTML files or web pages to PDF:

varresult=awaitconvertApi.ConvertAsync("web","pdf",newConvertApiParam("Url","https://example.com"));awaitresult.SaveFilesAsync(@"C:\output\");

Image to PDF

Convert images (e.g., JPG, PNG) to PDF:

varresult=awaitconvertApi.ConvertAsync("jpg","pdf",newConvertApiFileParam(@"C:\files\image.jpg"));awaitresult.SaveFilesAsync(@"C:\output\");

PDF to Image

Convert PDF to image formats (e.g., JPG, PNG):

varresult=awaitconvertApi.ConvertAsync("pdf","jpg",newConvertApiFileParam(@"C:\files\document.pdf"));awaitresult.SaveFilesAsync(@"C:\output\");

eBook Conversion

Convert eBook formats (e.g., EPUB) to PDF:

varresult=awaitconvertApi.ConvertAsync("epub","pdf",newConvertApiFileParam(@"C:\files\book.epub"));awaitresult.SaveFilesAsync(@"C:\output\");

Email Conversion

Convert EML or MSG email files to PDF:

varresult=awaitconvertApi.ConvertAsync("eml","pdf",newConvertApiFileParam(@"C:\files\email.eml"));awaitresult.SaveFilesAsync(@"C:\output\");

Merge Documents

Merge multiple PDF files into one:

varresult=awaitconvertApi.ConvertAsync("pdf","merge",newConvertApiFileParam(@"C:\files\file1.pdf"),newConvertApiFileParam(@"C:\files\file2.pdf"));awaitresult.SaveFilesAsync(@"C:\output\");

Split PDF

Split a PDF into individual pages:

varresult=awaitconvertApi.ConvertAsync("pdf","split",newConvertApiFileParam(@"C:\files\document.pdf"));awaitresult.SaveFilesAsync(@"C:\output\");

Rotate Pages

Rotate pages in a PDF document:

varresult=awaitconvertApi.ConvertAsync("pdf","rotate",newConvertApiFileParam(@"C:\files\document.pdf"),newConvertApiParam("RotatePage","90"));awaitresult.SaveFilesAsync(@"C:\output\");

Delete Pages

Delete specific pages from a PDF:

varresult=awaitconvertApi.ConvertAsync("pdf","delete-pages",newConvertApiFileParam(@"C:\files\document.pdf"),newConvertApiParam("PageRange","2,4"));awaitresult.SaveFilesAsync(@"C:\output\");

Add Watermark

Add a text watermark to a PDF:

varresult=awaitconvertApi.ConvertAsync("pdf","watermark",newConvertApiFileParam(@"C:\files\document.pdf"),newConvertApiParam("Text","Confidential"));awaitresult.SaveFilesAsync(@"C:\output\");

Flatten PDF

Flatten a PDF to prevent further editing:

varresult=awaitconvertApi.ConvertAsync("pdf","flatten",newConvertApiFileParam(@"C:\files\document.pdf"));awaitresult.SaveFilesAsync(@"C:\output\");

Repair PDF

Repair a corrupted PDF file:

varresult=awaitconvertApi.ConvertAsync("pdf","repair",newConvertApiFileParam(@"C:\files\corrupted.pdf"));awaitresult.SaveFilesAsync(@"C:\output\");

Rasterize PDF

Convert a PDF to a rasterized image:

varresult=awaitconvertApi.ConvertAsync("pdf","rasterize",newConvertApiFileParam(@"C:\files\document.pdf"));awaitresult.SaveFilesAsync(@"C:\output\");

Password Protect PDF

Add password protection to a PDF:

varresult=awaitconvertApi.ConvertAsync("pdf","protect",newConvertApiFileParam(@"C:\files\document.pdf"),newConvertApiParam("UserPassword","secure123"));awaitresult.SaveFilesAsync(@"C:\output\");

Remove PDF Password

Remove password protection from a PDF:

varresult=awaitconvertApi.ConvertAsync("pdf","unprotect",newConvertApiFileParam(@"C:\files\protected.pdf"),newConvertApiParam("Password","secure123"));awaitresult.SaveFilesAsync(@"C:\output\");

Redact PDF

Redact sensitive information in a PDF:

varresult=awaitconvertApi.ConvertAsync("pdf","redact",newConvertApiFileParam(@"C:\files\document.pdf"),newConvertApiParam("Preset","gdpr"));awaitresult.SaveFilesAsync(@"C:\output\");

Compress PDF

Compress a PDF file to reduce its size:

varresult=awaitconvertApi.ConvertAsync("pdf","compress",newConvertApiFileParam(@"C:\files\document.pdf"),newConvertApiParam("Preset","web"));awaitresult.SaveFilesAsync(@"C:\output\");

Convert to PDF/A

Convert a PDF to PDF/A format for archiving:

varresult=awaitconvertApi.ConvertAsync("pdf","pdfa",newConvertApiFileParam(@"C:\files\document.pdf"),newConvertApiParam("PdfaVersion","pdfa2b"));awaitresult.SaveFilesAsync(@"C:\output\");

Remove Metadata

Remove metadata from a PDF:

varresult=awaitconvertApi.ConvertAsync("pdf","pdf",newConvertApiFileParam(@"C:\files\document.pdf"),newConvertApiParam("PdfTitle","' '"),newConvertApiParam("PdfSubject","' '"),newConvertApiParam("PdfAuthor","' '"),newConvertApiParam("PdfCreator","' '"),newConvertApiParam("PdfKeywords","' '"));awaitresult.SaveFilesAsync(@"C:\output\");

Extract Text from PDF

Extract text content from a PDF:

varresult=awaitconvertApi.ConvertAsync("pdf","txt",newConvertApiFileParam(@"C:\files\document.pdf"));awaitresult.SaveFilesAsync(@"C:\output\");

Extract Tables from PDF

Extract tables from a PDF into Excel:

varresult=awaitconvertApi.ConvertAsync("pdf","xlsx",newConvertApiFileParam(@"C:\files\document.pdf"));awaitresult.SaveFilesAsync(@"C:\output\");

Extract Images from PDF

Extract images from a PDF:

varresult=awaitconvertApi.ConvertAsync("pdf","extract-images",newConvertApiFileParam(@"C:\files\document.pdf"),newConvertApiParam("ImageOutputFormat","png"));awaitresult.SaveFilesAsync(@"C:\output\");

Extract Form Data

Extract form data from a PDF:

varresult=awaitconvertApi.ConvertAsync("pdf","fdf-extract",newConvertApiFileParam(@"C:\files\form.pdf"));awaitresult.SaveFilesAsync(@"C:\output\");

Extract Email Attachments

Extract attachments from an email file:

varresult=awaitconvertApi.ConvertAsync("email","extract",newConvertApiFileParam(@"C:\files\email.eml"));awaitresult.SaveFilesAsync(@"C:\output\");

Generate Document from Template

Generate a document from a template using JSON data:

varresult=awaitconvertApi.ConvertAsync("template","pdf",newConvertApiFileParam(@"C:\templates\template.docx"),newConvertApiParam("BindingMethod","placeholders"),newConvertApiParam("JsonPayload","[{\"Name\": \"name\", \"Value\": \"John\", \"Type\": \"string\"}]"));awaitresult.SaveFilesAsync(@"C:\output\");

Compare Documents

Compare two DOCX documents:

varresult=awaitconvertApi.ConvertAsync("docx","compare",newConvertApiFileParam(@"C:\files\doc1.docx"),newConvertApiFileParam("CompareFile",@"C:\files\doc2.docx"));awaitresult.SaveFilesAsync(@"C:\output\");

Create ZIP Archive

Create a ZIP archive from multiple files:

varresult=awaitconvertApi.ConvertAsync("any","zip",newConvertApiFileParam(@"C:\files\file1.pdf"),newConvertApiFileParam(@"C:\files\file2.docx"));awaitresult.SaveFilesAsync(@"C:\output\");

Other result operations:

// save all result files to folderresult.SaveFilesAsync(@"\result\");// get result filesProcessedFile[]files=result.Files;// get conversion costintcost=result.ConversionCost;

Convert file url

ConvertApiResponseresult=awaitconvertApi.ConvertAsync("pptx","pdf",newConvertApiFileParam(newUri("https://cdn.convertapi.com/cara/testfiles/presentation.pptx")));

Additional conversion parameters

ConvertAPI accepts extra conversion parameters depending on converted formats. All conversion parameters and explanations can be found here.

ConvertApiResponseresult=awaitconvertApi.ConvertAsync("pdf","jpg",newConvertApiFileParam(@"c:\source\test.pdf"),newConvertApiParam("ScaleImage","true"),newConvertApiParam("ScaleProportions","true"),newConvertApiParam("ImageHeight","300"),newConvertApiParam("ImageWidth","300"));

User information

You can always check the remaining conversions amount and other account information by fetching user information.

ConvertApiUseruser=awaitconvert.GetUserAsync();intconversionsTotal=user.ConversionsTotal;intconversionsConsumed=user.ConversionsConsumed;

More examples

You can find more advanced examples in the examples folder.

Converting your first file, complete example:

ConvertAPI is designed to make converting files super easy. The following snippet shows how easy it is to get started. Let's convert the WORD DOCX file to PDF:

try{varconvertApi=newConvertApi("api_token");varconversionTask=awaitconvertApi.ConvertAsync("docx","pdf",newConvertApiFileParam(@"c:\source\test.docx"));varfileSaved=awaitconversionTask.Files.SaveFilesAsync(@"c:\");}//Catch exceptions from asynchronous methodscatch(ConvertApiExceptione){Console.WriteLine("Status Code: "+e.StatusCode);Console.WriteLine("Response: "+e.Response);if(e.StatusCode==HttpStatusCode.Unauthorized)Console.WriteLine("Secret is not provided or no additional seconds left in the account to proceed conversion. More information https://www.convertapi.com/a");}

Issues & Comments

Please leave all comments, bugs, requests, and issues on the Issues page. We'll respond to your request ASAP!

License

The ConvertAPI .NET Library is licensed under the MIT license. Refere to the LICENSE file for more information.

About

A .NET library for the ConvertAPI

Resources

Stars

27 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages