A couple of weeks has passed since my last post, but here I am again to give you a very easy and nice way to convert a Word file (by Word, read .docx, .doc, .rtf, etc) to a PDF. I have searched a lot on the internet about conversion to PDF and it’s not something easy, although it is simple to understand this code I am going to give…
First of all, to get this tutorial working correctly, you MUST have Office installed in your computer. Otherwise it won’t work. You have plenty of other tutorials even using iTextSharp to create your PDF or Java using JodConverter and OpenOffice like this.
So let’s get it started.
We create a new project and add Microsoft.Office.Interop.Word as refference like showing in the pictures bellow.
I created a form just to make things easier, but what really matters in this tutorial is the method called ConvertWord2PDF.
Okay, form already setup. We should go to our main method. Basically, to convert a Word file to PDF we must:
- Open
- Export
- Close
So, that’s what we have by following this order.
Well, that’s pretty much what you need. I just want to focus on both methods, Open and ExportAsFixedFormat. There’s a bunch of parameters we are not using, if you want to understand one by one, all you need is to read the documentation.
Here we have our Open method.
And here the ExportAsFixedFormat method.
Finally, testing our application and…
Easy, huh? I know its restriction about the need of Microsoft Office installed but it’s the simplest way I have ever seen.Hope you guys get use of this tutorial, you can download the whole application here.
Thanks for all the attention.