如何以编程方式将Word文件转换为PDF?

我发现了几个开源/免费软件,允许您将.doc文件转换为.pdf文件,但它们都是应用程序/打印机驱动程序,没有附加SDK。

我发现有几个程序确实有一个SDK允许你将.doc文件转换成.pdf文件,但是它们都是专有的,2000美元左右的许可证。

有谁知道任何清洁,廉价(最好是免费的)程序化解决scheme,我的问题,使用C#或VB.NET?

谢谢!

使用foreach循环而不是for循环 – 它解决了我的问题。

 int j = 0; foreach (Microsoft.Office.Interop.Word.Page p in pane.Pages) { var bits = p.EnhMetaFileBits; var target = path1 +j.ToString()+ "_image.doc"; try { using (var ms = new MemoryStream((byte[])(bits))) { var image = System.Drawing.Image.FromStream(ms); var pngTarget = Path.ChangeExtension(target, "png"); image.Save(pngTarget, System.Drawing.Imaging.ImageFormat.Png); } } catch (System.Exception ex) { MessageBox.Show(ex.Message); } j++; } 

这是对我有用的程序的修改。 它使用安装了另存为PDF加载项的 Word 2007。 它search.doc文件的目录,在Word中打开它们,然后将它们保存为PDF。 请注意,您需要将对Microsoft.Office.Interop.Word的引用添加到解决scheme中。

 using Microsoft.Office.Interop.Word; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; ... // Create a new Microsoft Word application object Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); // C# doesn't have optional arguments so we'll need a dummy value object oMissing = System.Reflection.Missing.Value; // Get list of Word files in specified directory DirectoryInfo dirInfo = new DirectoryInfo(@"\\server\folder"); FileInfo[] wordFiles = dirInfo.GetFiles("*.doc"); word.Visible = false; word.ScreenUpdating = false; foreach (FileInfo wordFile in wordFiles) { // Cast as Object for word Open method Object filename = (Object)wordFile.FullName; // Use the dummy value as a placeholder for optional arguments Document doc = word.Documents.Open(ref filename, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); doc.Activate(); object outputFileName = wordFile.FullName.Replace(".doc", ".pdf"); object fileFormat = WdSaveFormat.wdFormatPDF; // Save document into PDF Format doc.SaveAs(ref outputFileName, ref fileFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); // Close the Word document, but leave the Word application open. // doc has to be cast to type _Document so that it will find the // correct Close method. object saveChanges = WdSaveOptions.wdDoNotSaveChanges; ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing); doc = null; } // word has to be cast to type _Application so that it will find // the correct Quit method. ((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing); word = null; 

总结一下vb.net用户的免费选项(必须安装office):

微软办公文集下载:

  • 办公室2010年
  • 办公室2007年的pia

  • 将引用添加到Microsoft.Office.Interop.Word.Application

  • 将使用或导入(vb.net)语句添加到Microsoft.Office.Interop.Word.Application

VB.NET例子:

  Dim word As Application = New Application() Dim doc As Document = word.Documents.Open("c:\document.docx") doc.Activate() doc.SaveAs2("c:\document.pdf", WdSaveFormat.wdFormatPDF) doc.Close() 

PDFCreator有一个COM组件,可以从.NET或VBScript调用(包含在下载中的示例)。

但是,在我看来,打印机正是你所需要的 – 只要把它和Word的自动化结合起来 ,你就应该好起来。

在Joel的讨论论坛上有关于将Word转换为PDF的库的完整讨论 。 从线程的一些build议:

  • 阅读Aspose
  • 的PDFCreator
  • PDFsharp

我将其作为发布过程的一部分 – 将Word文档转换为PDF。

http://www.suodenjoki.dk/us/productions/articles/word2pdf.htm和http://www.oooforum.org/forum/viewtopic.phtml?t=3772&highlight=pdf+form

不完全编程,但可能会帮助你。

当我偶然发现服务器端办公自动化的一些问题时,我们研究了这里介绍的关于codeproject的技术。 它使用OpenOffice的可移植版本(可以通过xcopy部署)与macros组合使用。 虽然我们还没有做过自己的转变,但看起来很有希望。

只是想补充一点,我使用Microsoft.Interop库,特别是ExportAsFixedFormat函数,我没有看到在这个线程中使用。

  using Microsoft.Office.Interop.Word; using System.Runtime.InteropServices; using System.IO; using Microsoft.Office.Core;Application app; public string CreatePDF(string path, string exportDir) { Application app = new Application(); app.DisplayAlerts = WdAlertLevel.wdAlertsNone; app.Visible = true; var objPresSet = app.Documents; var objPres = objPresSet.Open(path, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse); var baseFileName = Path.GetFileNameWithoutExtension(path); var pdfFileName = baseFileName + ".pdf"; var pdfPath = Path.Combine(exportDir, pdfFileName); try { objPres.ExportAsFixedFormat( pdfPath, WdExportFormat.wdExportFormatPDF, false, WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportAllDocument ); } catch { pdfPath = null; } finally { objPres.Close(); } return pdfPath; } 

当有人用10000字的文件转换成PDF格式时,我经历了PDF的痛苦。 现在我在C#中完成了这个工作,并且使用了Word interop,但是如果我尝试使用PC,它会很慢,崩溃。非常令人沮丧。

这导致我发现我可以转储interops和他们的缓慢…..我使用的Excel(EPPLUS),然后我发现,你可以得到一个免费的工具称为尖峰,允许转换为PDF …有限制!

http://www.e-iceblue.com/Introduce/free-doc-component.html#.VtAg4PmLRhE

似乎是一些相关的信息在这里:

在ASP.NET中将MS Word文档转换为PDF

此外,随着Office 2007发布到PDFfunction,我想你可以使用办公自动化在Word 2007中打开* .DOC文件并另存为PDF。 我不太喜欢办公自动化,因为它很慢,而且很容易挂,但只是扔在那里…

我用ABCpdf这是一个程序化的选项,并不是太昂贵,$ 300 /许可证。 它适用于OpenOffice,或者在OpenOffice不可用的情况下回退到Word。 OpenOffice的COM权限设置有点棘手,但绝对值得将应用程序的这部分外包。

目前Microsoft PDF插件似乎是最好的解决scheme,但是您应该考虑到它不会将所有word文档正确地转换为pdf,并且在某些情况下,您会看到word和output pdf之间的巨大差异。 不幸的是,我找不到任何可以正确转换所有word文档的api。 我发现确保转换的唯一解决scheme是100%正确的是通过打印机驱动程序转换文档。 缺点是文档排队和逐一转换,但你可以肯定,结果PDF是完全一样的文字文档布局。 我个人更喜欢使用UDC(通用文档转换器),并在服务器上安装Foxit Reader(免费版),然后通过启动“Process”并将其Verb属性设置为“print”来打印文档。 转换完成后,您还可以使用FileSystemWatcher设置一个信号。

只要您安装了Word 2010或更高版本,可以使用DocTo提供命令行应用程序来执行此操作。

Gembox( http://www.gemboxsoftware.com/ )给我留下了深刻印象,他们提供有限的免费版本的文档pipe理(包括pdf转换)。 他们也为电子表格做图书馆。 如果你超出了他们的限制(我想你会这么做),那么这个开发者许可证的价格大概在580美元左右( http://www.gemboxsoftware.com/document/pricelist )。 好吧,这不是免费的(或者我认为相对便宜),但是比2000美元便宜很多。 据我了解,从他们的价格列表中没有皇室用于服务器部署。 可能值得接近他们,看看他们是否会做一个交易,如果你不想自己推出。

我曾经使用过iTextSharp来生成PDF。 它是来自Java世界的iText的开源端口,function非常强大。

我还没有明确地完成Word到PDF的转换,但是我已经用它编程创build和操作了PDF。

这是另一个项目的链接 。