首页 文章

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

提问于
浏览
209

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

我发现有几个程序有一个SDK,允许你将.doc文件转换成.pdf文件,但它们都是专有类型,2000美元的许可证或附近 .

有没有人知道使用C#或VB.NET来解决我的问题的任何干净,廉价(最好免费)的程序化解决方案?

谢谢!

15 回答

  • 1

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

  • 3

    我对Gembox(http://www.gemboxsoftware.com/)留下了深刻的印象,他提供有限的免费版文档管理(包括pdf转换) . 他们还为电子表格制作库 . 1开发人员许可证,如果你超过他们的限制(我想你会),但大约是580美元(http://www.gemboxsoftware.com/document/pricelist) . 好吧,如果你不想自己动手,那么's not free (or in my opinion relatively inexpensive) but it is a lot cheaper than $2000. As I understand it from their price list there is no royalty either for server deployments. Might be worth approaching them and seeing if they'会做一笔交易 .

  • -4

    当我偶然发现服务器端局自动化的一些问题时,我们研究了here on codeproject描述的技术 . 它使用OpenOffice的可移植版本(可以通过xcopy部署)与宏结合使用 . 虽然我们还没有自己完成切换,但它看起来非常有意义 .

  • 1

    为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()
    
  • 4
  • 1

    当有人用10000个单词文件转换为PDF以转换为PDF时,我经历了Word的痛苦 . 现在我用C#做了它并且使用了Word互操作但是如果我试图使用PC那么它很慢并且崩溃了......非常令人沮丧 .

    这让我发现我可以转储interops和它们的缓慢.....对于我使用的Excel(EPPLUS)然后我发现你可以获得一个名为Spire的免费工具,允许转换为PDF ...有限制!

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

  • 192

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

    Converting MS Word Documents to PDF in ASP.NET

    此外,由于Office 2007具有发布到PDF功能,我想您可以使用办公自动化在Word 2007中打开* .DOC文件并另存为PDF . 我不太热衷于办公自动化,因为它很慢并且容易悬挂,但只是扔掉那里......

  • 14

    使用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++;
    }
    

    这是对我有用的程序的修改 . 它使用安装了Save As PDF add-in的Word 2007 . 它在目录中搜索.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;
    
  • 32

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

    但是,在我看来,打印机正是你所需要的 - 只需将它与Word's automation混合,你就应该好好去 .

  • 3

    我使用了ABCpdf这是一个程序化的选项,并不是太贵,300美元/许可证 . 它适用于OpenOffice,或者如果OpenOffice不可用则退回到Word . 使用OpenOffice COM权限设置有点棘手,但绝对值得外包应用程序的这一部分 .

  • 7

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

  • 0

    我之前使用过iTextSharp来生成PDF . 它是来自Java世界的iText的开源端口,非常强大 .

    我没有明确地完成Word到PDF的转换,但我已经用它以编程方式创建和操作了PDF .

    这是该项目的另一个link .

  • 1

    只是想补充说我使用了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;
    }
    
  • 7

    我这是发布的一部分过程 - 将Word Doc转换为PDF .

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

    不完全以编程方式,但可能会帮助你 .

  • 2

    对于处于无法在其服务器上安装Office或在某些 Cloud 环境中运行的程序员而言,其他答案的廉价替代方案是Api2Pdf,它支持将Word文件转换为PDF以及任何其他MS Office文件 . 它是一个Web API,并在引擎盖下使用LibreOffice .

相关问题