MS Word and PDF documents are among the popular document formats used by almost every business and industry. Due to their extensive usage, there has been a huge requirement to convert these formats into each other. In this article, we will learn how to convert MS Word documents of DOC/DOCX format into PDF format using C#. Additionally, we will see how to transform specific pages of any password-protected Word document using C# with .NET API for document conversion.
.NET API to Convert Word Files to PDF
GroupDocs empowers .NET developers with GroupDocs.Conversion solution. It helps programmers in the conversion of various documents and image formats with its efficient and reliable .NET API. In the examples of this article, I will use its GroupDocs.Conversion for .NET API to convert Word documents into PDF format.
You can download the DLLs or MSI installer from the downloads section or install the API in your .NET application via NuGet.
PM> Install-Package GroupDocs.Conversion
How to Convert Word (DOC/DOCX) to PDF using C#
Let’s start with the basic conversion of a Word document into a PDF using C#. The following steps transform all the pages of a Word file into a PDF file.
- Load the Word DOC/DOCX file using the Converter class.
- Call the Convert method to transform the loaded document into PDF format.
The following C# code converts the whole Word document into PDF format.
Convert Selected Pages of Password-Protected Word Documents using C#
You can also convert protected or locked Word documents. The following steps show how to convert selective pages of a locked Word document into PDF format using C#.
- Prepare the loading options using the WordProcessingLoadOptions class.
- Now, load the Word DOC/DOCX file using the Converter class.
- Prepare the conversion options for the PDF file using the PdfConvertOptions class.
- Define the list of candidate pages to convert using Pages, PageNumber, and PageCount properties.
- Convert the loaded Word file into PDF format using the Convert method.
The following C# code converts the selected pages of the password-protected Word document into PDF format.
Conclusion
To conclude the article, we discussed converting the MS Word (DOC/DOCX) files to PDF format using C#. Furthermore, we converted the selected pages of password-protected word documents into PDF using the same .NET API.
You can learn more about the .NET Conversion Automation API using the documentation, API Reference, or by experiencing the GitHub examples. You can reach us for any query via the forum.