我发现了一些开源/免费的程序,允许你将。doc文件转换为。pdf文件,但它们都是应用程序/打印机驱动程序,没有附加SDK。
我发现有几个程序确实有一个SDK,允许你将。doc文件转换为。pdf文件,但它们都是专有类型,一个许可证大约需要2000美元。
有人知道使用c#或VB.NET来解决我的问题的干净、便宜(最好是免费)的编程解决方案吗?
谢谢!
我发现了一些开源/免费的程序,允许你将。doc文件转换为。pdf文件,但它们都是应用程序/打印机驱动程序,没有附加SDK。
我发现有几个程序确实有一个SDK,允许你将。doc文件转换为。pdf文件,但它们都是专有类型,一个许可证大约需要2000美元。
有人知道使用c#或VB.NET来解决我的问题的干净、便宜(最好是免费)的编程解决方案吗?
谢谢!
当前回答
只是想补充一点,我用的是微软。互操作库,特别是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;
}
其他回答
PDFCreator有一个COM组件,可从。net或VBScript调用(下载中包含示例)。
但是,在我看来,一台打印机正是你所需要的——只要把它与Word的自动化混合在一起,你就应该没问题了。
这里似乎有一些相关信息:
在ASP中将msword文档转换为PDF。网
此外,由于Office 2007具有发布到PDF的功能,我猜你可以使用办公自动化在Word 2007中打开*. doc文件并另存为PDF。我不太喜欢办公室自动化,因为它很慢,而且容易挂起来,但我只是把它放在那里……
使用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();
}
}
只是想补充一点,我用的是微软。互操作库,特别是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;
}
为vb.net用户总结一下,免费选项(必须安装office):
microsoftofficeassemblies下载:
2010年的Pia办公室 皮娅竞选2007年总统 添加对Microsoft.Office.Interop.Word.Application的引用 在Microsoft.Office.Interop.Word.Application中添加using或import (vb.net)语句
VB。净的例子:
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()