Convert DOCX to PDF Using .NET Core

Convert DOCX to PDF Using .NET Core

In this topic we are going to show you how to convert DOCX to PDF Using .NET Core. We are going to use the following file as our input file:

https://www.docconversionapi.com/samples/example.docx

Convert DOCX to PDF using .NET Core and C#

First we start a conversion job by making a POST request to the following URL and include these conversion parameters into your request: inputFile, outputFormat, conversionParameters and async

Request parameter descriptions (for the full and detailed description, please click here):

X-ApplicationID Your application ID (which you get from your Doc Conversion API Account)
X-SecretKey Your secret key  (which you get from your Doc Conversion API Account)
outputFormat DOC
inputFile The file you would like to convert or a URL
conversionParameters A JSON String containing additional conversion options
async Defines if the request should be sync or async

docx to pdf

Conversion URL

https://api2.docconversionapi.com/jobs/create

URL conversion example (docx to pdf)
using System;
using System.Globalization;
using System.IO;
using System.Net.Http;

namespace sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (HttpClient client = new HttpClient())
            {
                // Adding authentication headers
                client.DefaultRequestHeaders.Add("X-ApplicationID", "Get your key from https://app.docconversionapi.com/#/applications");
                client.DefaultRequestHeaders.Add("X-SecretKey", "Get your key from https://app.docconversionapi.com/#/applications");

                var inputFileUrl = "https://www.docconversionapi.com/samples/example.docx";

                using (var requestContent = new MultipartFormDataContent(
                    "Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture)
                    ))
                {
                    // Setting body parameters
                    requestContent.Add(new StringContent(inputFileUrl), "inputFile");
                    requestContent.Add(new StringContent("pdf"), "outputFormat");
                    requestContent.Add(new StringContent("{}"), "conversionParameters");
                    requestContent.Add(new StringContent("false"), "async");

                    // Sending a POST request to DCA API
                    using (var requestResultTask = client.PostAsync(
                        "https://api2.docconversionapi.com/jobs/create", requestContent
                    ))
                    {
                        var requestResult = requestResultTask.Result;

                        if (requestResult.IsSuccessStatusCode)
                        {
                            Console.WriteLine([email protected]"This is a response JSON object where you can find fileDownloadUrl: 
                            {requestResult.Content.ReadAsStringAsync().Result}");
                        }
                        else
                        {
                            Console.WriteLine($"Error has occurred! {requestResult.Content.ReadAsStringAsync().Result}");
                            Console.ReadKey();
                        }
                    }
                }
            }
        }
    }
}
Local file conversion example (docx to pdf)
using System;
using System.Globalization;
using System.IO;
using System.Net.Http;

namespace sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (HttpClient client = new HttpClient())
            {
                // Adding authentication headers
                client.DefaultRequestHeaders.Add("X-ApplicationID", "Get your key from https://app.docconversionapi.com/#/applications");
                client.DefaultRequestHeaders.Add("X-SecretKey", "Get your key from https://app.docconversionapi.com/#/applications");

                var inputFile = new MemoryStream();
                using (var localFile = new FileStream("../../../example.docx", FileMode.Open))
                {
                    localFile.CopyTo(inputFile);
                    inputFile.Seek(0, SeekOrigin.Begin);
                    using (var requestContent = new MultipartFormDataContent(
                        "Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture)
                        ))
                    {
                        // Setting body parameters
                        requestContent.Add(new StreamContent(inputFile), "inputFile", "example.docx");
                        requestContent.Add(new StringContent("pdf"), "outputFormat");
                        requestContent.Add(new StringContent("{}"), "conversionParameters");
                        requestContent.Add(new StringContent("false"), "async");

                        // Sending a POST request to DCA API
                        using (var requestResultTask = client.PostAsync(
                            "https://api2.docconversionapi.com/jobs/create", requestContent
                        ))
                        {
                            var requestResult = requestResultTask.Result;

                            if (requestResult.IsSuccessStatusCode)
                            {
                                inputFile.Close();
                                Console.WriteLine([email protected]"This is a response JSON object where you can find fileDownloadUrl: {requestResult.Content.ReadAsStringAsync().Result}");
                            }
                            else
                            {
                                inputFile.Close();
                                Console.WriteLine($"Error has occurred! {requestResult.Content.ReadAsStringAsync().Result}");
                                Console.ReadKey();
                            }
                        }
                    }
                }
            }
        }
    }
}
DotnetFiddle example (docx to pdf)

https://dotnetfiddle.net/1RNxcb

After the conversion job has finished processing your file, you will get a response like the following:

{
    "id": 1,
    "fileName": "demo.docx",
    "fileId": "fd4957df-8514-4e49-bfa4-123v8dta06r1",
    "userId": 2059,
    "startedAt": "2020-02-07T14:07:22.347669+00:00",
    "finishedAt": "2020-02-07T14:07:37.0907497+00:00",
    "outputFileName": "fd4957df-8514-4e49-bfa4-123v8dta06r1.pdf",
    "outputFormat": "pdf",
    "conversionParametersApplied": "{}",
    "creditCost": 1,
    "status": "Success",
    "fileDownloadUrl": "https://api2.docconversionapi.com/files?userId=2059&fileId=fd4957df-8514",
    "statusCode": 200,
    "errorMessage": null
}

 

If you have decided to make an async request instead of sync then your response will have the same structure but will not contain information like fileDownloadUrl, fileId, outputFileName etc.

To get your conversion result you will need to send a GET request to the following URL and use the previously returned ID of your conversion job.
https://api2.docconversionapi.com/jobs/{{id}}

If you have any questions or need any further help, please contact us at [email protected].