Convert Word to PDF Programmatically in C#

How to Convert Word to PDF Programmatically in C#
How to Convert Word to PDF Programmatically in C#

Word is the most popular word processing document format, developed by Microsoft. It allows you to create, edit, view, and share your documents quickly and easily using Word application. PDF is a for Portable Document Format, developed by Adobe. It is one of the most commonly used file types today to protect and secure documents. Word documents reformats documents and don’t provide great security for sharing historical data. While PDF retains formatting, supports great file management and security to protect your sensitive information using password or encryption certificate. For such cases, we may need to transform Word file into PDF format. So, in this article I will demonstrate how to convert Word to PDF programmatically in C# using REST API.

The following topics shall be covered in this article:

Document and File Conversion API – .NET SDK

For converting Word to PDF file, I will be using the .NET SDK of GroupDocs.Conversion Cloud API. It is a feature-rich and high-performance Cloud SDK to convert back and forth between over 50 types of documents and images, including all Microsoft Office and OpenDocument file formats, PDF , HTML, CAD, raster images and many more. GroupDocs.Conversion Cloud API allows you to convert and extract format-specific information from a wide list of supported source document formats into any supported target format. It provides a flexible set of settings to customize the conversion process. Currently, it also provides C#, JavaPHPRuby, Python and Node.js SDKs as its document conversion family members for the Cloud API.

You can install GroupDocs.Conversion Cloud to your Visual Studio Code project from the NuGet Package manager or using the following command in the Package Manager console:

dotnet add package GroupDocs.Conversion-Cloud --version 22.10.0

You need to get your Client ID and Client Secret from the dashboard before you start following the steps and available code samples. Add your Client ID and Client Secret in the code as demonstrated below:

Convert Word to PDF Programmatically in C# using REST API

You can convert Word file to PDF in CSharp using REST API by following the simple steps mentioned below:

  • Upload the Word document to the Cloud
  • Convert Word file to PDF using REST API
  • Download the converted file

Upload the Word Document

Firstly, upload the Word document to the Cloud using any of the following methods:

  • Using the dashboard
  • Upload source file using Upload File API from the browser
  • Upload programmatically using the code example given below:

As the result, Word files will be uploaded to the Cloud Storage

Convert Word File to PDF Online

This simple code example demonstrates how to convert Word to PDF programmatically in C# file using REST API. Follow the steps mentioned below:

  • Create an instance of the ConvertApi
  • Create an instance of the ConvertSettings
  • Provide the input DOCX file path
  • Assign “pdf” to the format
  • Provide the output file path
  • Create ConvertDocumentRequest with ConvertSettings
  • Convert by calling the convertDocument() method with ConvertDocumentRequest

The following code example shows how to convert DOCX to PDF in C# using REST API:

How to Convert Word File to PDF Online
Convert Word File to PDF Online

Download the Converted File

The above code sample will save the uploaded PDF file on the cloud. You can download it using the following code sample:

Convert DOCX File to PDF in C# using Advanced Options

You can convert Word documents to PDF files with some advanced settings by following the steps given below:

  • Create an instance of the ConvertApi
  • Create an instance of the ConvertSettings 
  • Provide the DOCX file path
  • Assign “pdf” to format
  • Create an instance of the DocxLoadOptions
  • Set password as load option
  • Create an instance of the PdfConvertOptions
  • Optionally set various convert options such as CenterWindow, FromPage, margins (top, left, right, bottom), etc.
  • Provide the output file path
  • Create ConvertDocumentRequest with ConvertSettings
  • Convert by calling the convertDocument() method with ConvertDocumentRequest

The following code example shows how to convert Word document to PDF document with advanced convert options:

How to Convert Range of Pages from Word to PDF in C#

You can convert selected pages of Word to PDF file. For this purpose, you need to provide a range of pages as demonstrated in the code example below. Convert a range of pages from a Word document to a PDF file programmatically by following the steps mentioned below:

  • Create an instance of the ConvertApi
  • Create an instance of the ConvertSettings
  • Provide the input DOCX file path
  • Assign “pdf” to the format
  • Create an instance of the PdfConvertOptions
  • Provide a page range to convert from start page number and total pages to convert
  • Provide the output file path
  • Create ConvertDocumentRequest with ConvertSettings
  • Convert by calling the convertDocument() method with ConvertDocumentRequest

The following code sample shows how to convert a range of pages from DOCX to PDF using REST API in C#:

How to Convert Range of Pages from Word to PDF in C#
How to Convert Range of Pages from Word to PDF in C#

Please follow the steps mentioned earlier to upload and download a file.

How to Convert Specific Pages of Word to PDF in C#

You can convert specific pages of a Word document to a PDF file programmatically by following the steps mentioned below:

  • Create an instance of the ConvertApi
  • Create an instance of the ConvertSettings
  • Provide the input DOCX file path
  • Assign “pdf” to the format
  • Create an instance of the PdfConvertOptions
  • Provide specific page numbers to convert
  • Provide the output file path
  • Create ConvertDocumentRequest with ConvertSettings
  • Convert by calling the convertDocument() method with ConvertDocumentRequest

The following code example shows how to convert specific pages of Word document to PDF using REST API in C#:

Please follow the steps mentioned earlier to upload and download a file.

How to Convert Specific Pages of Word to PDF in C#
How to Convert Specific Pages of Word to PDF in C#

Word to PDF Converter Online Free

How to free convert Word to PDF online? Please try the following free online Word to PDF converter without changing format to convert DOCX to PDF online free, which is developed using the above API.

Conclusion

In this article, you have learned:

  • how to convert Word documents to PDF files on the cloud;
  • convert selected pages from DOCX to PDF programmatically in C#;
  • how to convert specific pages of Word document to a PDF using C#;
  • programmatically upload the DOCX file on the cloud and then download the converted PDF file from the cloud;

You can learn more about GroupDocs.Conversion Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser.

Finally, groupdocs.com is writing new blog articles on online file converter between multiple file formats. So, please stay in touch for regular updates.

Ask a question

For queries/discussions about Word DOCX to PDF converter, feel free to visit our Free Support Forum.

FAQs

How do I convert a DOCX file to PDF without changing font?

Please follow this link to learn the C# code snippet for how to create a pdf from word quickly.

How to download a Word document as a PDF?

Install word to pdf converter software free download C# library to create, download, and process Word DOCX to PDF conversion programmatically.

See Also