How to Convert a Word Document to PDF using Aspose.Words for .NET

« Back to Blog List | By Ben Mensah | 16 Jan 2018
Aspose.Words for .NET

Welcome to one of the video tutorials for Aspose.Words for .NET

This tutorial shows how to easily convert a Microsoft Word document to a PDF using a Aspose.Words for .NET.

For this tutorial we're going to use Visual Studio to create a console application, which uses the Aspose.Words component to quickly and easily open a docx file and save it as a PDF document. Once this is complete we're going to compare the output to the original, just to show that the conversion using Aspose.Words is done with high quality.

First let's create a console application by using Visual Studio. To do this select file and then new project, choose console application and ensure that the target version of.NET framework is appropriate for the application. In this case we target the .NET framework.

Next enter the name of the project and specify the location where it will be saved then click OK.

Before we take advantage of the great features Aspose.Words has to offer, we have to add a reference to Aspose.Words for .NET DLL. To do this right-click the references drop-down in your solution and choose add reference you are presented with a list of .NET components installed on the machine if you install Aspose.Words through the MSI installer, then the reference will appear in the .NET tab. To easily find the reference to Aspose.Words, sort the list alphabetically by clicking on the component name column.

It's good to note that the version of the Aspose.Words DLL runtime seen here, can be targeted to any version of the.NET framework and you can run it in a .NET application and so forth. If you did not use the .MSI to install Aspose.Words then you will need to add the reference to Aspose.Words. Manually locate where the Aspose.Words DLL was extracted to by using the Browse tab after selecting Aspose.Words. Using either method click OK to add it as a reference in the application. Aspose.Words has now been added to your application.

Let's add the Aspose.Words namespace to the project, so we can reference the Aspose.Words classes and methods easily. Now that's all taken care of let's take a look at the document we want to convert to PDF. The documents folder contains the document to convert the PDF. Let's get a quick overview of the document. As we can see the document contains numerous types of contents such as different formatted paragraphs a wide variety of tables, lists, images and a few more advanced features like fields and a table of contents.

Now let's write the code to convert this document to a PDF. Using a slow start words to work with the document in Aspose.Words, you load it into the document object. This is the main class of a Aspose.Words and it allows you to work with or convert any document loaded into it.

A document is loaded by passing a file path in the form of a string or a stream containing the document. It's not necessary to specify the file format of the document we are loading because the format of the incoming document is automatically detected by Aspose.Words. Even when loading a document from the stream now, it's just a simple call to save the method to save the document as PDF format. Again we specify the location to save in the form of a file path. This time the output format is automatically detected from the file extension. You can also specify a save format explicitly by passing a safe format extension to the document .save method. We're going to come back to that later.

Let's run the application to see the output produced. The document is generated in a fraction of a second. Let's open it and we can see that the PDF is generated with high fidelity and all features look just like the original.

Let's look through all pages we can also do a side-by-side comparison of the original and output produced by Aspose.Words. The results look great. Let's take a quick look at some of the other possible formats that we could save this document - by taking a look at the say format extension, this option is passed to the save method to explicitly set the format to save the document as. As you can see, Aspose.Words supports conversion of a wide number of formats. Further options that control how a document is output can be found in the form of the save options classes. Most export formats have their own dedicated option class, for example PDF has the PDF save option class, HTML has the HTML save option class.

Let's take a look at the PDF save option class to see how we can specify options to control our PDF output. We can change the compliance of the generated PDF document. Set this property to PDF A1b to generate a version of PDF document instead of a PDF, which is produced by default. We must not forget to pass the save options object to the save method for it to take effect. Now let's recompile the application and run it to see the results. We can verify that the PDF was produced as the version as specified in our save options.

Aspose.Words provides a variety of save options to assist with document conversion. That's pretty much it guys thank you for watching this tutorial if you have any comments or feedback please let us know.