DEV Community

Suresh Mohan for Syncfusion, Inc.

Posted on • Updated on • Originally published at blog.syncfusion.com

Converting Word to PDF programmatically [C#]

Let’s learn how to convert a Word document to a PDF programmatically in C# without Microsoft Office or interop.

Syncfusion Word library (Essential DocIO) renders the contents of a Word document page by page and adds the contents of each page to the PDF document using Syncfusion PDF library. This converter can be easily integrated into .NET Framework (WinForms, WPF, ASP.NET Web forms, and ASP.NET MVC), ASP.NET Core, and Xamarin applications. It works well on both full trust and partial trust environments such as Azure applications.

This converter offers several options to customize Word to PDF conversion,

  • Embed complete or subset font information for TrueType fonts.
  • Substitute alternate fonts from the device or upload the original fonts.
  • PDF conformance level PDF/A1B or PDF/X-1a:2001.
  • Convert accessible Word document (508 compliance) to Accessible or Tagged PDF
  • Export the headings or bookmarks in the Word document as PDF bookmarks.
  • Export the Word form fields as PDF form fields during Word to PDF conversion.
  • Adjust JPEG image quality and resolution for optimizing the converted PDF file size.

This converter supports all the elements of a typical Word document like text, formatting, images, tables, hyperlinks, fields, bookmarks, table of contents, shapes, headers, footers, etc. Refer our documentation to know more about Word to PDF conversion.

Steps to perform Word to PDF conversion programmatically in C# without Microsoft Office or interop:

1.Create a new C# console application (.NET Framework) project.
create-a-console-application-in-visual-studio
2.Install DocToPDFConverter.WinForms NuGet package as a reference to your applications from the NuGet.org. This package contains Syncfusion libraries to perform Word to PDF conversion without Microsoft Office or interop.Add Syncfusion Word to PDF converter NuGet package reference to the project
3.Include the following namespace in the Program.cs file. The following code snippet is used to open the Word document with DocIO.

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocToPDFConverter;
using Syncfusion.Pdf;

4.Use the following code snippet to create Word file with simple text.

//Get the path of existing Word document
string fullpath = @"...\..\DocToPDF.docx";

//Loads an existing Word document
WordDocument wordDocument = new WordDocument(fullpath, FormatType.Docx);

//Creates an instance of the DocToPDFConverter
DocToPDFConverter converter = new DocToPDFConverter();

//Converts Word document into PDF document
PdfDocument pdfDocument = converter.ConvertToPDF(wordDocument);

//Releases all resources used by DocToPDFConverter
converter.Dispose();

//Closes the instance of document objects
wordDocument.Close();

//Saves the PDF file 
pdfDocument.Save("DocToPDF.pdf");

//Closes the instance of document objects
pdfDocument.Close(true);

The following screenshot illustrates the input Word document.
Input Word document to perform Word to PDF conversion
The next screenshot shows the final PDF document converted from the Word document.

A complete working example of Word to PDF conversion can be downloaded from this GitHub location

An online sample link for Word to PDF conversion example.

If you want to convert the Word documents to PDF in Xamarin or ASP.NET Core applications or in Azure environment, please check the following pages.

Conclusion

Whether you need to convert a Word document to PDF in any .NET platforms, the Syncfusion Word to PDF converter libraries make work easier for .NET developers. Take a moment to peruse the Word to PDF conversion documentation, where you can find other options and features, all with code examples. If you are new to our DocIO library, it is highly recommended to follow our Getting Started documentation.

Explore more about the rich set of Syncfusion Word Framework and PDF Framework features.

As always, your feedback is highly appreciated, so please do reach out in our comments. You can also contact us through our support forum or Direct-Trac. We are happy to assist you!

If you’re already a Syncfusion user, you can download the product setup from the downloads page. If you’re not yet a Syncfusion user, you can download a free, 30-day trial on our website.

If you like this post, we think you’ll also like:

[Blog] 7 ways to compress PDF files in C#, VB.NET
[Ebook] SQL Server for C# Developers Succinctly
[Blog] Optical Character Recognition in PDF Using Tesseract Open-Source Engine
[Ebook] Object-Oriented Programming in C# Succinctly
[Blog] Word to PDF conversion in Xamarin and .NET Core

The post Converting Word to PDF programmatically [C#] appeared first on Syncfusion Blogs.

Top comments (2)

Collapse
 
codemouse92 profile image
Jason C. McDonald

I think you meant to use the csharp tag. (You typo'd.) ;)

Collapse
 
sureshmohan profile image
Suresh Mohan • Edited

Thank you for notifying, I have updated it :)