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 the Word application. PDF is a Portable Document Format, developed by Adobe. It is one of the most commonly used file types today to protect and secure documents. Word documents reformat documents and don’t provide great security for sharing historical data. While PDF retains formatting, it supports great file management and security to protect your sensitive information using a password or encryption certificate. In such cases, we may need to transform the 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 File Format Library

I will use the .NET SDK of GroupDocs.Conversion Cloud API to convert a Word document to a PDF. It is a feature-rich and high-performance cloud SDK to convert back and forth between over 50 types of documents and images, including 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#, Java, PHP, Ruby, 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 obtain 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

converting a Word document to a PDF can be a useful way to preserve the appearance and security of the document, and make it easier to share with others. In CSharp, you can convert Word Doc to PDF file using REST API by following the steps mentioned below.

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:

  • Firstly, create an instance of the ConvertApi
  • Secondly, create an instance of the ConvertSettings
  • Thirdly, provide the input DOCX file path
  • Now, assign “pdf” to the format
  • Next, provide the output file path
  • Then, create ConvertDocumentRequest with ConvertSettings
  • Finally, 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

In this section, you can convert Word documents to PDF files with some advanced settings by following the steps given below:

  • Firstly, Create an instance of the ConvertApi
  • Secondly, create an instance of the ConvertSettings
  • Next, provide the DOCX file path
  • Next, assign “pdf” to format
  • Now, create an instance of the DocxLoadOptions
  • Next, 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.
  • Then, provide the output file path
  • Next, create ConvertDocumentRequest with ConvertSettings
  • Lastly, 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:

  • Firstly, create an instance of the ConvertApi
  • Secondly, create an instance of the ConvertSettings
  • Thirdly, provide the input DOCX file path
  • Now, assign “pdf” to the format
  • Next, create an instance of the PdfConvertOptions
  • Provide a page range to convert from start page number and total pages to convert
  • Also provide the output file path
  • Create ConvertDocumentRequest with ConvertSettings
  • Finally, 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:

  • Firstly, create an instance of the ConvertApi
  • Secondly, create an instance of the ConvertSettings
  • Next, provide the input DOCX file path
  • Now, assign “pdf” to the format
  • Create an instance of the PdfConvertOptions
  • Provide specific page numbers to convert
  • Next, provide the output file path
  • Then, create ConvertDocumentRequest with ConvertSettings
  • Lastly, 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

We are ending this article here. In this blog post, we 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;

Moreover, 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.

How do I convert Word document to PDF offline in windows?

Please visit this link to download Word to PDF converter software free for windows. This Word to PDF converter software will perform conversion quickly, with a single click.

How to convert DOC file to PDF free online?

Online DOC to PDF converter free allows you to transform Word document to PDF format, quickly and easily. Once the conversion is completed, you can download the PDF file.

See Also