创建启用了“跟踪更改”的OOXML文档

时间:2016-12-16 16:46:55

标签: openxml

如何在启用跟踪更改的情况下创建.docx?我被告知word/settings.xml我应该修改w:proofState但是我发现OOXML的在线文档中的所有信息都与语法和拼写检查有关,但没有关于启用跟踪更改的信息。

1 个答案:

答案 0 :(得分:4)

它位于word/settings.xml,但您要查找的元素是w:trackRevisions

要使用OpenXML SDK在C#中执行此操作,您可以使用TrackRevisions类。以下代码将创建一个包含一个段落且已启用更改跟踪的文档:

using (WordprocessingDocument package = WordprocessingDocument.Create(filename, WordprocessingDocumentType.Document))
{
    // Add a new main document part. 
    package.AddMainDocumentPart();

    //create a body and a paragraph
    Body body = new Body();
    Paragraph paragraph = new Paragraph();
    paragraph.AppendChild(new Run(new Text("This document should have change tracking switched on")));
    body.AppendChild(paragraph);
    package.MainDocumentPart.Document = new Document(body);

    //change tracking code
    DocumentSettingsPart documentSettingsPart = package.MainDocumentPart.AddNewPart<DocumentSettingsPart>();
    Settings settings = new Settings();
    TrackRevisions trackRevisions = new TrackRevisions();
    settings.Append(trackRevisions);
    documentSettingsPart.Settings = settings;

    // Save changes to the main document part. 
    package.MainDocumentPart.Document.Save();
}