我找到了几个开源/免费软件程序,允许您将.doc文件转换为.pdf文件,但它们都是应用程序/打印机驱动程序的种类,没有附加SDK.
我发现有几个程序有一个SDK,允许你将.doc文件转换成.pdf文件,但它们都是专有类型,2000美元的许可证或附近.
有没有人知道使用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++; }
这是对我有用的程序的修改.它使用Word 2007并安装了另存为PDF加载项.它在目录中搜索.doc文件,在Word中打开它们,然后将它们另存为PDF.请注意,您需要向解决方案添加对Microsoft.Office.Interop.Word的引用.
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用户总结一下,免费选项(必须安装办公室):
Microsoft office assembies下载:
2010年办公室的pia
pia for office 2007
添加对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的自动化相结合,您应该很高兴.
只是想补充说我使用了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 pdfFileName = Path.ChangeExtension(path, ".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; }
在Joel的论坛上有关于将Word转换为PDF的库的完整讨论.线程的一些建议:
阅读Aspose
的PDFCreator
PDFsharp
当有人用10000个单词文件转换为PDF以转换为PDF时,我经历了Word到PDF的痛苦.现在我用C#做了它并且使用了Word互操作但是如果我试图使用PC那么它很慢并且崩溃了......非常令人沮丧.
这让我发现我可以转储interops和它们的缓慢.....对于我使用的Excel(EPPLUS)然后我发现你可以获得一个名为Spire的免费工具,允许转换为PDF ...有限制!
http://www.e-iceblue.com/Introduce/free-doc-component.html#.VtAg4PmLRhE