我发现了一些开源/免费的程序,允许你将。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;
}

其他回答

只是想补充一点,我用的是微软。互操作库,特别是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;
}

这里似乎有一些相关信息:

在ASP中将msword文档转换为PDF。网

此外,由于Office 2007具有发布到PDF的功能,我猜你可以使用办公自动化在Word 2007中打开*. doc文件并另存为PDF。我不太喜欢办公室自动化,因为它很慢,而且容易挂起来,但我只是把它放在那里……

为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()

我经历了Word转PDF的痛苦,有人把我的10000字文件转换成PDF。现在我在c#和使用Word互操作,但它很慢,如果我试图使用PC在所有崩溃。非常令人沮丧。

这让我发现我可以抛弃互操作和他们的缓慢.....我使用的Excel (EPPLUS),然后我发现你可以得到一个叫做Spire的免费工具,允许转换为PDF…限制!

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

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

但是,在我看来,一台打印机正是你所需要的——只要把它与Word的自动化混合在一起,你就应该没问题了。