How to convert any Document into PDF in C# .NET Framework

Converting between document formats is a difficult task at the best of times, and this is certainly the case in C#. Instead of getting lost in the quagmire of different format details and their parsing, we will be skipping past the entire mess by using an API. This API will allow us to achieve perfect results in a tiny fraction of the time normally needed to deal with this problem.

First we need to install our package:

Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.2.8

Now we can go ahead and toss our file into this function here:

using System;using System.Diagnostics;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;namespace Example{public class ConvertDocumentAutodetectToPdfExample{public void main(){// Configure API key authorization: ApikeyConfiguration.Default.AddApiKey("Apikey", "YOUR_API_KEY");// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// Configuration.Default.AddApiKeyPrefix("Apikey", "Bearer");var apiInstance = new ConvertDocumentApi();var inputFile = new System.IO.Stream(); // System.IO.Stream | Input file to perform the operation on.try{// Convert Document to PDFbyte[] result = apiInstance.ConvertDocumentAutodetectToPdf(inputFile);Debug.WriteLine(result);}catch (Exception e){Debug.Print("Exception when calling ConvertDocumentApi.ConvertDocumentAutodetectToPdf: " + e.Message );}}}}

And then we will be returned our PDF, simple as that.

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.