Converting Word to PDF programmatically [C#]
Tags: csharp, productivity, tutorial, wordtopdf
Published: 2019-06-19 11:00:46

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

<a href="https://www.syncfusion.com/word-framework/net/word-library">Syncfusion Word library</a> (Essential DocIO) renders the contents of a Word document page by page and adds the contents of each page to the PDF document using <a href="https://www.syncfusion.com/pdf-framework/net/pdf-library">Syncfusion PDF library</a>. 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 <a href="https://help.syncfusion.com/file-formats/docio/word-to-pdf#customization-settings">customize Word to PDF conversion</a>,
<ul>
 	<li>Embed complete or subset font information for TrueType fonts.</li>
 	<li>Substitute alternate fonts from the device or upload the original fonts.</li>
 	<li>PDF conformance level '<strong>PDF/A1B</strong>' or '<strong>PDF/X-1a:2001</strong>'.</li>
 	<li>Convert <strong>accessible Word document</strong> (508 compliance) to <strong>Accessible</strong> or <strong>Tagged</strong> <strong>PDF</strong></li>
 	<li>Export the headings or bookmarks in the Word document as PDF bookmarks.</li>
 	<li>Export the Word form fields as PDF form fields during Word to PDF conversion.</li>
 	<li>Adjust JPEG image quality and resolution for optimizing the converted PDF file size.</li>
</ul>
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 <a href="https://help.syncfusion.com/file-formats/docio/word-to-pdf">Word to PDF conversion</a>.
<h2><span style="font-size: 14pt;">Steps to perform Word to PDF conversion programmatically in C# without Microsoft Office or interop:</span></h2>
<p style="padding-left: 30px;">1. Create a new C# console application (.NET Framework) project.<img class="aligncenter wp-image-33091 size-large" src="https://blog.syncfusion.com/wp-content/uploads/2014/05/create-a-new-csharp-console-application-project-1024x703.png" alt="create-a-console-application-in-visual-studio" width="1024" height="703" /></li>
<p style="padding-left: 30px;">2. Install <a href="https://www.nuget.org/packages/Syncfusion.DocToPDFConverter.WinForms/">DocToPDFConverter.WinForms</a> NuGet package as a reference to your applications from the <a href="https://www.nuget.org/">NuGet.org</a>. This package contains Syncfusion libraries to perform Word to PDF conversion without Microsoft Office or interop.<strong>Note:</strong> Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.<img class="aligncenter wp-image-33092 size-large" src="https://blog.syncfusion.com/wp-content/uploads/2014/05/install-syncfusion-doctopdfconverter-nuget-package-for-winforms-1024x526.png" alt="Add Syncfusion Word to PDF converter NuGet package reference to the project" width="1024" height="526" /></li>
<p style="padding-left: 30px;">3. Include the following namespace in the Program.cs file. The following code snippet is used to open the Word document with DocIO.

```csharp
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocToPDFConverter;
using Syncfusion.Pdf;
```
<p style="padding-left: 30px;">4. Use the following code snippet to create Word file with simple text.
```csharp
//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.

<img class="aligncenter wp-image-33093 size-full" src="https://blog.syncfusion.com/wp-content/uploads/2014/05/word-to-pdf-conversion-input-word-document.png" alt="Input Word document to perform Word to PDF conversion" width="802" height="895" />

The next screenshot shows the final PDF document converted from the Word document.

<img class="aligncenter wp-image-33096 size-full" src="https://blog.syncfusion.com/wp-content/uploads/2018/12/the-converted-pdf-document-after-word-to-pdf-conversion.png" alt="" width="804" height="898" />

A complete working example of Word to PDF conversion can be downloaded from <a href="https://github.com/SyncfusionExamples/convert-word-to-pdf">this GitHub repository</a>

An online link for <u><a href="https://ej2.syncfusion.com/aspnetmvc/DocIO/DOCtoPDF#/material">Word to PDF conversion example</a></u>.

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.
<ul>
 	<li><a href="https://blog.syncfusion.com/post/word-to-pdf-conversion-in-xamarin-and-net-core.aspx">Word to PDF Conversion in Xamarin and .NET Core</a>.</li>
 	<li><a href="https://www.syncfusion.com/kb/7760/is-it-possible-to-perform-word-to-pdf-conversion-in-azure-environment">Word to PDF Conversion in Azure</a>.</li>
</ul>
<h2><span style="font-size: 14pt;">Conclusion</span></h2>
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 <u><a href="https://help.syncfusion.com/file-formats/docio/word-to-pdf">Word to PDF conversion documentation</a></u>, 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 <a href="https://help.syncfusion.com/file-formats/docio/getting-started">Getting Started documentation</a>.

Explore more about the rich set of Syncfusion <u><a href="https://www.syncfusion.com/word-framework/net">Word Framework</a></u> and <u><a href="https://www.syncfusion.com/pdf-framework/net">PDF Framework</a></u> features.

As always, your feedback is highly appreciated, so please do reach out in our comments. You can also contact us through our support <a href="https://www.syncfusion.com/forums">forum</a> or <a href="https://www.syncfusion.com/support/directtrac/">Direct-Trac</a>. We are happy to assist you!

If you’re already a Syncfusion user, you can download the product setup <a href="https://www.syncfusion.com/support/directtrac/downloads/16_1_0_24?file=fileformats">on Direct-Trac</a>. If you’re not yet a Syncfusion user, you can download a free, 30-day trial on our website.

<em>If you like this post, we think you'll also like:</em>
<ul>
 	<li>[Blog] <a href="https://blog.syncfusion.com/post/7-ways-to-compress-pdf-files.aspx">7 ways to compress PDF files in C#, VB.NET</a></li>
 	<li>[Ebook] <a href="https://www.syncfusion.com/ebooks/sql_server_for_c_sharp_developers_succinctly"><em>SQL Server for C# Developers Succinctly</em></a></li>
 	<li>[Blog] <a href="https://blog.syncfusion.com/post/optical-character-recognition-in-pdf-using-tesseract-open-source-engine.aspx">Optical Character Recognition in PDF Using Tesseract Open-Source Engine</a></li>
 	<li>[Ebook] <a href="https://www.syncfusion.com/ebooks/oop-csharp">Object-Oriented Programming in C# Succinctly</a></li>
 	<li>[Blog] <a href="https://blog.syncfusion.com/post/word-to-pdf-conversion-in-xamarin-and-net-core.aspx">Word to PDF conversion in Xamarin and .NET Core</a></li>
</ul>

The post [Converting Word to PDF programmatically [C#]](https://www.syncfusion.com/blogs/post/converting-word-docs-to-pdfs.aspx) appeared first on [Syncfusion Blogs](https://www.syncfusion.com/blogs/).