C# 将 Word DOCX 文件另存为 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12044620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Saving Word DOCX files as PDF
提问by Stealth Rabbi
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.
我正在使用 openxml 创建 Word DOCX 文件。一旦这些文档被创建为 PDF 文件,我想保存它们。有没有办法在 openxml 中做到这一点?我认为答案是否定的。如果不是,是否有推荐的库或工具可以用来将 DOCX 文件保存/打印为 PDF(以编程方式,在 .NET 中)?我查看了sharpPDF(PDFSharp),似乎这个库仅用于从头开始生成PDF,而不是将DOCX另存为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?
我能否以某种方式以完全自动化的方式打印到已安装的 PDF 打印机(Cute PDF 或 Windows 7 内置的 PDF 打印机)?
Update: Looking for free with non-viral license, and preferably doesn't require additional installations.
更新:寻找免费的非病毒许可证,最好不需要额外的安装。
采纳答案by cellik
You could do this with Word automation. You need to have word installed.
您可以使用 Word 自动化来做到这一点。你需要安装word。
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();
回答by Paul Jowett
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:
要从 DocX -> PDF 获取,您需要可以呈现 DocX 文件并提供 PDF 导出/保存功能的东西。不用说,可以呈现 DocX 的工具并不多(Word、OpenOffice/LibreOffice 和下面提到的一些其他许可产品)。根据您的运行时限制/要求,您可以尝试:
- to use MS Automation to get Word to load the docx and save as PDF.
- tools that sit on top of OpenOffice (JODConverter/Docmosis) to do the conversion.
- try embedding other document libraries (Aspose, Windward)
- 使用 MS 自动化让 Word 加载 docx 并另存为 PDF。
- 位于 OpenOffice (JODConverter/Docmosis) 之上的工具来进行转换。
- 尝试嵌入其他文档库(Aspose、Windward)
I'm not sure about the auto print requirement sorry.
抱歉,我不确定自动打印要求。
回答by vinny
I've successfully used the Asposesuite 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.
过去我已经成功地使用了Aspose工具套件:https: //stackoverflow.com/a/5513946/54762。它不是免费的,但您可以在购买之前进行演示。
回答by gammay
回答by OnceUponATimeInTheWest
We produce WordGlue .NET which is a generic WP layout engine that can handle doc/docx.
我们生产 WordGlue .NET,它是一个通用的 WP 布局引擎,可以处理 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.
它是完全托管的,可用于导出为各种格式,如图像、XPS 或(借助 ABCpdf 等 PDF 库)PDF。
I work on the ABCpdf .NET software component so my replies may feature concepts based around ABCpdf. It's just what I know. :-)
我在 ABCpdf .NET 软件组件上工作,因此我的回复可能包含基于 ABCpdf 的概念。这只是我所知道的。:-)

