使用 ASP.NET C# 在 MS Word 中添加修订和评论

Add Track Changes and comments in MS Word using ASP.NET C#

我可以使用 C# 创建和打开 Word 文档。我可以插入一些新文本,但现在我正在尝试在此文档中插入曲目更改和评论。

我试过一些图书馆,但没有成功。是否有人面临此类问题或知道一些图书馆(免费或商业)可以帮助我找到解决此问题的方法。

下面是在新的Word文档中创建和添加文本的代码,我想在这个文档上添加修订,但我不知道如何:

public static void CreateDocument()
{
    try
    {
        //Create an instance for word app
        Word.Application winword = new Word.Application();

        //Set animation status for word application
        winword.ShowAnimation = false;

        //Set status for word application is to be visible or not.
        winword.Visible = false;

        //Create a missing variable for missing value
        object missing = System.Reflection.Missing.Value;

        //Create a new document
        Microsoft.Office.Interop.Word.Document document = winword.Documents.Add(ref missing, ref missing, ref missing, ref missing);

        //adding text to document
        document.Content.SetRange(0, 0);
        document.Content.Text = "In this text I should add track changes " + Environment.NewLine;

        //Allow track changes, but I don't know how to use now
        document.TrackRevisions = true;

        //Save the document
        object filename = @"c:\temp1.docx";
        document.SaveAs2(ref filename);
        document.Close(ref missing, ref missing, ref missing);
        document = null;
        winword.Quit(ref missing, ref missing, ref missing);
        winword = null;
    }
    catch (Exception ex)
    {
    }
}

与其创建自己的版本机制,不如使用 Word 已有的版本机制?我想 Revisions 就是您要找的。

您可能需要Revision Interface that have all info

Microsoft 目前不推荐也不支持从任何无人值守的非交互式客户端应用程序或组件(包括 ASP、ASP.NET 自动化 Microsoft Office 应用程序、DCOM 和 NT 服务),因为当 Office 在此环境中 运行 时,Office 可能表现出不稳定的行为 and/or 死锁。

如果您正在构建 运行 在服务器端上下文中的解决方案,您应该尝试使用已针对无人值守执行安全处理的组件。或者,您应该尝试找到至少允许 运行 客户端部分代码的替代方案。如果您从服务器端解决方案使用 Office 应用程序,该应用程序将缺少许多 运行 成功所必需的功能。此外,您将承担整体解决方案稳定性的风险。在 Considerations for server-side Automation of Office 文章中阅读更多相关信息。

作为解决方法,如果您只处理打开的 XML 文档,您可以考虑使用 Open XML SDK,请参阅 Welcome to the Open XML SDK 2.5 for Office。或者您可以考虑使用专为服务器端执行而设计的第三方组件。