How to convert a WordDocument to PDF in C#.Net


Word Document is an essential part of any application, it may help users to prepare reports etc. You can easily create Documents programamatically using Microsoft.Office.Interop.Word Library in C#.Net.

This Library also provide capability of converting document to two another monstly used file format, PDF and XPS. Let’s beggin

Reference Library

First up all add refrence to Microsoft.Office.Interop.Word and Include to the project

Microsoft Visual Studio Reference Manager

Start Converting

using Microsoft.Office.Interop.Word;

and do the following

  • Open the File want to convert with Document constructor
  • Use ExExportAsFixedFormat to convert document into pdf
 Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
                    var wordDocument = appWord.Documents.Open("mydoc.docx");
                    wordDocument.ExportAsFixedFormat("mydoc.pdf", Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
                    wordDocument.Close();
appWord.Quit();

In the similary way you can convert document to XPS too

Ask Masrshal to realease resources

Finally we are no need to hold the Word resources , ask Marshal to release them. Do this when ever you use file resources.

System.Runtime.InteropServices.Marshal.FinalReleaseComObject(appWord);

1 thought on “How to convert a WordDocument to PDF in C#.Net

  1. Pingback: Convert Doc file to PDF,XPS,SVG,XML,HTML using Spire in C# | Manoj's Developer Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.