C# 使用SDK 2.0防止在Word文档中编辑段落
我正在使用C# 使用SDK 2.0防止在Word文档中编辑段落,c#,asp.net,openxml-sdk,C#,Asp.net,Openxml Sdk,我正在使用openxmlsdk为用户将下载的每个文档添加一个序列号 代码如下: public static void OpenAndAddTextToWordDocument(string filepath, string txt) { // Open a WordprocessingDocument for editing using the filepath. WordprocessingDocument wordprocessingDocument = Wordproces
openxmlsdk
为用户将下载的每个文档添加一个序列号
代码如下:
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);
}
现在我需要防止用户在打开文档时删除序列号
我在OpenXMLSDK2.0常见问题解答中读到:
Open XML SDK仅支持保护文档的功能,例如
阻止在UI中编辑工作表
但是,它没有解释您是如何操作的。您可以通过microsoft word的“开发人员”选项卡创建内容占位符(标记)。在该标记的属性中,您可以限制用户“无法编辑内容”和“无法删除内容控制”。现在,您可以通过编程方式访问带有标签名的内容占位符,并将序列号放入其中。如果您能提供一些详细信息,我将非常高兴。谢谢你的回复。首先,你熟悉微软word的开发者标签吗?这会让我知道从哪里开始解释。从MS Word功能区转到Word选项,然后在“常用”选项卡下选择“在功能区中显示开发人员选项卡”。现在您可以看到“开发人员”选项卡出现在word菜单栏的顶部。在“开发人员”选项卡下打开设计模式。在这里,您还可以找到不同类型的内容占位符(富文本、文本、图片、组合框等)。在您的情况下,富文本将达到此目的。只需单击富文本,word就会将此控件放置在光标所在的位置。现在选择此标记并转到“设计模式”按钮下或右键单击可找到的属性。勾选相关框。也不要忘记给这个标签起名字,这样你就可以用它的名字通过程序访问这个标签,并且可以把你的序列号放在里面。如果您还需要通过C#访问此标记的代码示例,请告诉我。