How to convert Word (DOCX) to PDF using C# and VB.NET

Maintaining the same formatting and giving a shareable aspect to important documents are the main reasons why we often need to convert Word documents to PDF files.

You need to use the fastest solution possible when you are handling several DOCX files programmatically and need to convert them to PDF.

With GemBox.Document you can convert word files to PDF in C# and VB.NET without using Microsoft Office Interop. GemBox.Document's API is simpler and more intuitive so you will be able to save hours of programming.

How to install and configure the GemBox.Document library

Add the GemBox.Document component as a package using the following command from the NuGet Package Manager Console:

After installing the GemBox.Document library, you must call the ComponentInfo.SetLicense method before using any other member of the library.

In this tutorial, by using “FREE-LIMITED-KEY”, you will be using GemBox's free mode. This mode allows you to use the library without purchasing a license, but with some limitations. If you purchased a license, you can replace “FREE-LIMITED-KEY” with your serial key.

You can check this page for a complete step-by-step guide to installing and setting up GemBox.Document in other ways.

How to convert Word (DOCX) files to PDF in C#

    Load the DOCX file into the DocumentModel object.

var document = DocumentModel.Load("Input.docx");
document.Save("Output.pdf");

This can can be simplified even more by writing it in a single line of code:

DocumentModel.Load("Input.docx").Save("Output.pdf");

The following screenshots show how the output file will look like:

Output of a Word to PDF conversion

How to convert Word pages to PDF files

Besides converting a whole Word document to a single PDF file, it's also possible to convert each page of a document into a separate PDF file.

    Load the DOCX file you need to convert to PDF using the DocumentModel.Load() method.

var document = DocumentModel.Load("Input.docx");

Get the Word pages from the document.

var pages = document.GetPaginator().Pages;

Create a PdfSaveOptions instance and use the PdfSaveOptions.ImageDpi property to optimize and reduce the size of the resulting PDF files.

var pdfSaveOptions = new PdfSaveOptions() < ImageDpi = 220 >;

Then iterate through Word pages and save each one as a separate PDF file.

for (int pageIndex = 0; pageIndex < pages.Count; pageIndex++) < var page = pages[pageIndex]; page.Save($"Page.pdf", pdfSaveOptions); >

After executing the code above, the output document should look like this:

Output of a Word to PDF conversion

How to convert DOCX files to PDF/A in C#

GemBox.Document also supports saving the Word document to PDF/A, an ISO-standardized version of the Portable Document Format (PDF) specialized for use in archiving and long-term preservation of electronic documents.

    Load the Word file using the DocumentModel.Load() method.

var document = DocumentModel.Load("Input.docx");
var options = new PdfSaveOptions() < ConformanceLevel = PdfConformanceLevel.PdfA1a >;
document.Save("Output.pdf", options);

Conclusion

This article showed you how you can use GemBox.Document to convert Word to Pdf programmatically in .NET.

If you would like to learn about various ways you can convert a Pdf document to a DOCX file, check this example:

For more information regarding the GemBox.Document API, check the documentation pages. We also recommend checking our GemBox.Document examples where you can learn how to use other features by running code.

See also

Preservation of unsupported Word features in C# and VB.NET

Preservation of unsupported Word features in C# and VB.NET

Next steps

GemBox.Document is a .NET component that enables you to read, write, edit, convert, and print document files from your .NET applications using one simple API. How about testing it today?

GemBox.Document is a .NET component that enables you to read, write, edit, convert, and print document files from your .NET applications using one simple API.

Our customers

What do they say

We, at Stock&Buy needed a simple solution for our customers to customize their invoices and documents generated within our systems. Docx templating using Mail Merge functionality is quite known in the industry and our customers are very familiar with it. We started looking at available packages and we were lucky to find Gembox.Document. The library is very simple to use, has powerful and standardized APIs. The bindings to .NET objects is an amazing addition that simplifies the development process. The pricing model is very reasonable and the support is second to none. We recently reported an issue and we got a hotfix within 12 hours which is amazing for a software provider. This library is a true gem and we highly recommend it to anyone in the lookout for affordable, simple, and powerful Mail Merge library.

Tahar
CEO and Co-Founder of Stock&Buy AS

My company has implemented Gembox.Document, Gembox.Spreadsheet, and Gembox.Presentation. The customer service has been outstanding. I truly can’t be happier with the customer service. The documentation has been straight forward and easy. I trialed a couple of other competitors prior to finding Gembox, but where those competitors failed, Gembox succeeded. I’m glad we found Gembox.

Liem
Senior Software Engineer at GetDocsNow.com All Examples

© GemBox Ltd. — All rights reserved.