我已经找到了几个开源/免费软件,可以将.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++;
}
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):
Microsoft office程序集下载:
添加到Microsoft.Office.Interop.Word.Application的参考
在Microsoft.Office.Interop.Word.Application中添加使用或导入(vb.net)语句
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()
只是想补充一下,我使用了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;
}
app.Visible = false;
并添加调用 app.Quit();
。 - Dan KornPDFCreator拥有一个COM组件,可以从.NET或VBScript中调用(下载包含示例)。
但是,我认为你只需要打印机-然后将其与Word的自动化混合使用,就可以开始了。
有一次,有人让我将10000个Word文件转换为PDF,我经历了Word转PDF的痛苦。后来我用C#和Word互操作性完成了它,但速度很慢,而且如果我尝试使用电脑,它会崩溃...非常令人沮丧。
这使我发现可以摆脱互操作性及其缓慢的问题.....对于Excel,我使用(EPPLUS),然后我发现你可以获得一个免费的工具叫做Spire,它允许转换为PDF...但有限制!
http://www.e-iceblue.com/Introduce/free-doc-component.html#.VtAg4PmLRhE
使用Microsoft.Office.Interop.Word
轻松将WORD转换为PDF的代码和解决方案
using Word = Microsoft.Office.Interop.Word;
private void convertDOCtoPDF()
{
object misValue = System.Reflection.Missing.Value;
String PATH_APP_PDF = @"c:\..\MY_WORD_DOCUMENT.pdf"
var WORD = new Word.Application();
Word.Document doc = WORD.Documents.Open(@"c:\..\MY_WORD_DOCUMENT.docx");
doc.Activate();
doc.SaveAs2(@PATH_APP_PDF, Word.WdSaveFormat.wdFormatPDF, misValue, misValue, misValue,
misValue, misValue, misValue, misValue, misValue, misValue, misValue);
doc.Close();
WORD.Quit();
releaseObject(doc);
releaseObject(WORD);
}
添加以下步骤以释放内存:
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
//TODO
}
finally
{
GC.Collect();
}
}
这里似乎有一些相关信息:
此外,由于Office 2007具有发布为PDF功能,您可以使用Office自动化打开Word 2007中的*.DOC文件并另存为PDF。虽然我不太喜欢Office自动化,因为它速度较慢且容易挂起,但这只是一个建议...
微软的Word PDF插件似乎是目前最好的解决方案,但需要考虑到它并不能将所有Word文档正确转换为PDF,在某些情况下,您会看到Word和输出PDF之间存在巨大差异。不幸的是,我找不到任何可以完全正确转换所有Word文档的API。
我找到的唯一解决方案是通过打印机驱动程序转换文档以确保转换100%正确。缺点是文档会排队并逐个转换,但您可以确信生成的PDF与Word文档布局完全相同。
我个人更喜欢使用UDC(通用文档转换器),并在服务器上安装了Foxit Reader(免费版本),然后通过启动“进程”并将其动词属性设置为“打印”来打印文档。您还可以使用FileSystemWatcher设置信号,以便在转换完成时发出通知。
pandoc manual.docx -o manual.pdf
即可。 - Colonel Panicdocker "run" "--rm" "--entrypoint" "soffice" "-v" "$(pwd):/usr/src/project" "linuxserver/libreoffice:latest" "--headless" "--convert-to" "pdf" "--outdir" "/usr/src/project" "/usr/src/project/foo.docx"
- nicojs