【问题标题】:automation of Doc to PDF in c#在 C# 中自动将 Doc 转换为 PDF
【发布时间】:2011-02-17 14:40:51
【问题描述】:

我有大约 200 个 word 文档需要 pdf。

显然,我无法将它们逐个 pdf,因为首先它需要很长时间,其次我确信这样做不是一个好习惯。

我需要找到一种方法来自动化这种转换,因为我们需要一次又一次地这样做。

我用的是C#,但解决方案不一定非得是c#,但还是首选。

我查看了一些库,例如 PDfCreator、Office 2007 插件、ITextSharp 等,论坛上没有任何明确的答案。

PDFCreator 有 c# 示例,但它只适用于 txt 文件。 Office 2007 插件没有自动化必须具备的文档锁定功能。

以前有没有人实现过这样的场景?我想听听你的建议。

提前致谢

问候

【问题讨论】:

标签: c# automation pdf-generation doc


【解决方案1】:

【讨论】:

  • +1 - 我以前也用过这个。顺便说一句,嗨约翰!我碰巧看到了你的名字,不得不仔细考虑。
  • 这行得通...我正在尝试锁定文档,但它确实有效...谢谢。
【解决方案2】:

我这样做是为了自动将我们的 doc 和 docx 文档转换为 pdf:

private bool ConvertDocument(string file)
{
    object missing = System.Reflection.Missing.Value;

    OW.Application word = null;
    OW.Document doc = null;

    try
    {
        word = new OW.Application();
        word.Visible = false;
        word.ScreenUpdating = false;

        Object filename = (Object)file;

        doc = word.Documents.Open(ref filename, ref missing,
            ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing);
        doc.Activate();

        if (Path.GetExtension(file) == ".docx")
            file = file.Replace(".docx", ".pdf");
        else
            file = file.Replace(".doc", ".pdf");

        object fileFormat = OW.WdSaveFormat.wdFormatPDF;

        doc.ExportAsFixedFormat(file, OW.WdExportFormat.wdExportFormatPDF, false, OW.WdExportOptimizeFor.wdExportOptimizeForPrint,
            OW.WdExportRange.wdExportAllDocument, 1, 1, OW.WdExportItem.wdExportDocumentContent, true, true, OW.WdExportCreateBookmarks.wdExportCreateNoBookmarks,
            true, true, false, ref missing);
    }
    catch(Exception ex)
    {
        return false;
    }
    finally
    {
        if (doc != null)
        {              
            object saveChanges = OW.WdSaveOptions.wdDoNotSaveChanges;
            ((OW._Document)doc).Close(ref saveChanges, ref missing, ref missing);
            doc = null;
        }

        if (word != null)
        {
            ((OW._Application)word).Quit(ref missing, ref missing, ref missing);
            word = null;
        }
    }

    return true;
}

其中 OW 是 Microsoft.Office.Interop.Word 的别名。

【讨论】:

  • +1 这或多或少是我发布的 MSDN 示例的更简洁版本。 注意,您不需要 .NET 4 中的所有 ref missing。这个答案应该被接受!
  • @HuBeZa:谢谢!是的,我知道,但在我的工作中,我们仍然坚持使用 3.5,这就是示例缺少参数的原因。
【解决方案3】:

你检查过这个MSDN article吗?


编辑:

请注意,此“操作方法”示例无法按原样工作,因为:

  1. 由于某些原因,它在 #77、#81 和 #82 行中的程序参数 (ConvertDocCS.exe [sourceDoc] [targetDoc] [targetFormat]) 上运行。
  2. 我将项目转换为 VS 2010 并且不得不重新引用 Microsoft.Office.Core。这是一个名为 Microsoft Office 12.0 Object Library 的 COM 引用。
  3. 示例不包含相对路径。

我相信你会设法克服这些障碍 :)


最后一件事。如果您使用的是 .NET 4,则无需发送所有烦人的Missing.Value,这要归功于可选参数的奇妙之处。

【讨论】:

    【解决方案4】:

    您可以尝试Aspose.Words for .NETconvert DOC files to PDF。它可以在任何带有 C# 或 VB.NET 的 .NET 应用程序中使用,就像任何其他 .NET 程序集一样。它也适用于任何 Windows 操作系统和 32/64 位系统。

    披露:我在 Aspose 担任开发布道师。

    【讨论】:

      【解决方案5】:

      正如HuBeZa所说,如果您的工作站上安装了Word,您可以使用Word Automation将您的文件一个一个打开并保存为PDF。 您所需要的只是引用 COM 组件“Microsoft Word 对象库”并使用该程序集的类。

      执行时间可能会有点长,但您的转换将是自动化的。

      【讨论】:

        【解决方案6】:

        我们可以为文字自动化设置字体,我将单一字体应用到从我的解决方案中针对同一应用程序生成的所有文档中——节省了我手动进入每个模板并为每个标签和标题等单独设置字体的时间。 .

         using (WordprocessingDocument wordProcessingDocument = WordprocessingDocument.Open(input, true))
                        {
                            // Get all content control elements
                            List<DocumentFormat.OpenXml.OpenXmlElement> elements =
                                wordProcessingDocument.MainDocumentPart.Document.Body.ToList();
                            // Get and set the style properties of each content control
                            foreach (var itm in elements)
                            {
                                try
                                {
                                    List<RunProperties> list_runProperties = 
                                          itm.Descendants<RunProperties>().ToList();
                                    foreach (var item in list_runProperties)
                                    {
                                        if (item.RunFonts == null)
                                            item.RunFonts = new RunFonts();
        
                                        item.RunFonts.Ascii = "Courier New";
                                        item.RunFonts.ComplexScript = "Courier New";
                                        item.RunFonts.HighAnsi = "Courier New";
                                        item.RunFonts.Hint = FontTypeHintValues.ComplexScript;
                                    }
                                }
                                catch (Exception)
                                {
                                    //continue for other tags in document 
                                    //throw;
                                }
                            }
                            wordProcessingDocument.MainDocumentPart.Document.Save();
                        }
        

        【讨论】:

          【解决方案7】:

          我认为直接回答是否定的! 但可以通过我建议的解决方法是使用 imagemagik 或一些库,看看它是否可以提供你的 word doc 的图像,然后在 itextsharp 中使用这些图像来创建 pdf

          【讨论】:

            猜你喜欢
            • 2011-06-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-02-05
            • 2014-12-09
            • 2012-04-14
            相关资源
            最近更新 更多