【问题标题】:Convert .doc to .docx using C# [closed]使用 C# 将 .doc 转换为 .docx [关闭]
【发布时间】:2016-03-10 17:23:04
【问题描述】:

我使用 PDFFocus.net dll 将 PDF 文件转换为 word 文件。但对于我的系统,我想要 .docx 文件。我尝试了不同的方法。有一些可用的库。但那些不是免费的。这是我的 pdf 到 doc 转换代码。

    Using System;
    Using System.Collections.Generic;
    Using System.Linq;
    Using System.Text;
    Using System.Threading.Tasks;
    Using iTextSharp.text;
    Using iTextSharp.text.pdf;

    namespace ConsoleApplication
    {
          class Program
          {
               static void main(String[] args)
               {
                    SautinSoft.PdfFocus f=new SautinSoft.PdfFocus();
                    f.OpenPdf(@"E:\input.pdf");

                         t.ToWord(@"E:\input.doc");
                }
          }
    }

这项工作成功。 然后我尝试使用下面的代码将 .doc 转换为 .docx。但它给了我错误。

//Open a Document.
Document doc=new Document("input.doc");
//Save Document.
doc.save("output.docx");

谁能帮帮我。

【问题讨论】:

  • Gembox.DocumentSpireDoc.NET Free 这样的库可能会有所帮助 - 加载 .doc 并另存为 .docx
  • 基于对documentation 的快速浏览,没有迹象表明PDFFocus 支持RTF 输出以外的任何内容(即使使用.doc 文件扩展名)。你确定它可以生成基于 Open XML 的 Word 格式 (.docx)?

标签: c#


【解决方案1】:

尝试使用 Microsoft.Office.Interop.Word 程序集。

一篇MSDN文章可以找到Here

在您的项目中包含引用,并通过上面显示的链接中的示例在代码模块中启用它们

using System.Collections.Generic;
using Word = Microsoft.Office.Interop.Word;

【讨论】:

  • 感谢您发布此问题的答案!这个答案很短,但没有提供太多背景信息。请解释它背后的一些原因,它将对提问者和未来的读者变得更加有用。谢谢!
  • 我稍微调整了一下。欢迎来到堆栈。
【解决方案2】:

是的,就像 Erop 说的那样。您可以使用Microsoft Word 14.0 Object Library。然后从 doc 转换为 docx 真的很容易。例如具有这样的功能:

    public void ConvertDocToDocx(string path)
    {
        Application word = new Application();

        if (path.ToLower().EndsWith(".doc"))
        {
            var sourceFile = new FileInfo(path);
            var document = word.Documents.Open(sourceFile.FullName);

            string newFileName = sourceFile.FullName.Replace(".doc", ".docx");
            document.SaveAs2(newFileName,WdSaveFormat.wdFormatXMLDocument, 
                             CompatibilityMode: WdCompatibilityMode.wdWord2010);

            word.ActiveDocument.Close();
            word.Quit();

            File.Delete(path);
        }
    }

确保添加CompatibilityMode: WdCompatibilityMode.wdWord2010 否则文件将保持兼容模式。还要确保在您要运行应用程序的计算机上安装了 Microsoft Office。

另一件事,我不知道PDFFocus.net,但您是否尝试过直接从pdf 转换为docx。像这样:

     static void main(String[] args)
     {
           SautinSoft.PdfFocus f=new SautinSoft.PdfFocus();
           f.OpenPdf(@"E:\input.pdf");

                t.ToWord(@"E:\input.docx");
     }

我认为这是可行的,但这只是一个假设。

【讨论】:

  • 非常感谢戴夫。这对我有用。我用 .docx 尝试了 PDFFocus.net。但 PDFFocus.net 仅支持 .Doc 文件。不过非常感谢您的回答..
  • 请注意,有 2 个 Word InterOp 程序集。我在控制台应用程序中使用 v15.0 成功测试了第一个代码块。即使确保我有 CompatibilityMode 行,该文档也以兼容模式打开,但我认为这并不重要。注意事项-不应从“服务器”代码尝试-包括来自本地网站或 Windows 服务-因为它是在差异用户的上下文中运行的,而不是登录的用户给出的:CO_E_SERVER_EXEC_FAILURE (0x80080005): Server execution failedsupport.microsoft.com/en-us/help/257757/…
  • 顺便说一句,如果你尝试这个并得到那个错误,我发现这个可以克服错误:stackoverflow.com/questions/3477086/…
猜你喜欢
  • 2012-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-20
相关资源
最近更新 更多