Saving Word DOCX files as PDF [closed]

2019-02-09 21:14发布

问题:

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.

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


回答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.



回答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.



回答4:

I have used this VBScript and it works well. It works in the background (opens the docx and saves as PDF using "Save/Export as PDF" option). Check the system requirements listed on the page.



回答5:

We produce WordGlue .NET which is a generic WP layout engine that can handle doc/docx.

It's completely managed and can be used to export to a variety of formats like image, XPS or (with the help of a PDF library such as ABCpdf) PDF.

I work on the ABCpdf .NET software component so my replies may feature concepts based around ABCpdf. It's just what I know. :-)