阻止使用SDK 2.0在Word文档中编辑段落

时间:2012-12-11 16:07:00

标签: c# asp.net openxml-sdk

我正在使用Open XML SDK为用户下载的每个文档添加序列号。

这是代码:

public static void OpenAndAddTextToWordDocument(string filepath, string txt)
{
    // Open a WordprocessingDocument for editing using the filepath.
    WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filepath, true);

    // Assign a reference to the existing document body.
    Body body = wordprocessingDocument.MainDocumentPart.Document.Body;

    // Add new text.
    Paragraph para = body.AppendChild(new Paragraph());



    Run run = para.AppendChild(new Run());
    run.AppendChild(new Text(txt));

    DocumentWatermarkTest.AddWaterMark(wordprocessingDocument);

    // Close the handle explicitly.
    wordprocessingDocument.Close();
}
protected void Button1_Click1(object sender, EventArgs e)
{
    string strDoc = @"C:\Users\xxx\Desktop\2138-1.doc";
    string strTxt = "Serial number: 23jl4hk52345h32jkl";
    OpenAndAddTextToWordDocument(strDoc, strTxt);
}

现在我需要阻止用户在打开文档时删除序列号。

我在Open XML SDK 2.0 FAQ中读到了:

  

Open XML SDK仅支持保护文档的功能,例如   阻止在UI中编辑工作表

但是,它没有解释你是如何做到的。

1 个答案:

答案 0 :(得分:0)

您可以通过Microsoft Word中的“开发人员”标签创建内容占位符(标记)。在此标记的属性中,您可以限制用户“内容无法编辑”,也可以“无法删除内容控件”。现在,您可以以编程方式访问此内容持有人及其标记名称,并可以将您的序列号放在其中。