Saving Word DOCX files as PDF

25,774

Solution 1

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();

Solution 2

To get from DocX -> PDF you need something that can render a DocX file and provides a PDF export/save capability. Needless to say, there aren't that many tools that can render DocX (Word, OpenOffice/LibreOffice and some other licensed products mentioned below). Depending on your runtime limitations/requirements, you could try:

  1. to use MS Automation to get Word to load the docx and save as PDF.
  2. tools that sit on top of OpenOffice (JODConverter/Docmosis) to do the conversion.
  3. try embedding other document libraries (Aspose, Windward)

I'm not sure about the auto print requirement sorry.

Solution 3

I've successfully used the Aspose suite of tools for this in the past: https://stackoverflow.com/a/5513946/54762. It's not free, but you can demo it before you buy it.

Share:
25,774
Stealth Rabbi
Author by

Stealth Rabbi

Tea. Earl Gray. Hot. Welcome to Earth. I dabble in .NET, Java, Android, SQL Server, Oracle, Google Earth, and some other things.

Updated on October 25, 2020

Comments

  • Stealth Rabbi
    Stealth Rabbi over 3 years

    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.