Saving Word DOCX files as PDF

Stealth Rabbi picture Stealth Rabbi · Aug 20, 2012 · Viewed 24.7k times · Source

I'm using openxml to create Word DOCX files. I'd like to save these documents once they are created as PDF files. Is there a way I can do this in openxml? I assume the answer is no. If it is no, is there a recommended library or tool I can use to save / print DOCX files as PDF (programatically, in .NET)? I looked at sharpPDF (PDFSharp), and it seems this library is only for generating PDFs from scratch, not saving DOCX as PDF.

Can I somehow Print to an installed PDF printer, either Cute PDF or the PDF printer built in to Windows 7 in a fully automated fashion?

Update: Looking for free with non-viral license, and preferably doesn't require additional installations.

Answer

cellik picture cellik · Aug 21, 2012

You could do this with Word automation. You need to have word installed.

var TheDocument = TheWordApp.Documents.Open(docName);

TheDocument.ExportAsFixedFormat(
            docName.Replace(".docx", ".pdf"),
            Word.WdExportFormat.wdExportFormatPDF, 
            OptimizeFor: Word.WdExportOptimizeFor.wdExportOptimizeForOnScreen, 
            BitmapMissingFonts: true, DocStructureTags: false);

((Word._Document)TheDocument).Close();