Converting DOCX File to C# PDF

Question:

I have an application and I need to convert a DOCX file to PDF without losing formatting.

I found some DLLs that help, but for corporate use you have to buy a license so it gets complicated.

In addition, I did some more research and found some posts that use Microsoft.Office.Interop.Word

Answer:

It is very easy to use the Microsoft.Office.Interop.Word library to convert to PDF.
However, it is necessary that MS-Word is installed on the machine where you will run the application.

An example of code in C# to do the conversion is as follows:

string arqDoc = @"C:\Desenv\DocTeste.docx";
string arqPdf = @"C:\Desenv\DocTeste.pdf";

var appWord = new Microsoft.Office.Interop.Word.Application();
var wordDocument = appWord.Documents.Open(arqDoc);

wordDocument.ExportAsFixedFormat(arqPdf, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);       

wordDocument.Close();
Scroll to Top