Word控件Spire.Doc 【页眉页脚】教程(10): 锁定标题以防止在 C# 中编辑 word 文档

Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。 

E-iceblue 功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式

Spire.Doc for.NET 最新下载(qun:767755948)icon-default.png?t=N2N8https://www.evget.com/product/3368/download

在 Spire.Doc 的帮助下,我们可以轻松地在 C# 中为 word 文档添加删除标题。本文我们将演示如何锁定标题信息以防止编辑。我们将把它分成两部分进行演示。一次是用header锁定已有word文档的header信息,一次是在新建的word文档上。

如何锁定现有word文档的页眉信息。

//Load the sample document with header<font></font>
Document doc = new Document();<font></font>
doc.LoadFromFile("sample.docx");<font></font>
<font></font>
//Get the first section from the word document<font></font>
Section section = doc.Sections[0];<font></font>
<font></font>
//Protect the document and set the ProtectionType as AllowOnlyFormFields<font></font>
doc.Protect(ProtectionType.AllowOnlyFormFields, "123");<font></font>
<font></font>
//Set the ProtectForm as false to unprotect the section<font></font>
section.ProtectForm = false;<font></font>
<font></font>
//Save the document to file<font></font>
doc.SaveToFile("Result.docx", FileFormat.Docx2013);<font></font>

头部有效截图已锁定,其他区域可编辑:

如何锁定新建word文档的页眉信息。

//Create a new instance of word document<font></font>
Document doc = new Document();<font></font>
<font></font>
//Add a section to the word document<font></font>
Section section = doc.AddSection();<font></font>
<font></font>
//Add header information to the section<font></font>
HeaderFooter header = section.HeadersFooters.Header;<font></font>
Paragraph HParagraph = header.AddParagraph();<font></font>
TextRange HText = HParagraph.AppendText("Protect header");<font></font>
<font></font>
//Add a paragraph to the section<font></font>
Paragraph Para = section.AddParagraph();<font></font>
Para.AppendText("Demo of how to lock the header information by Spire.Doc ");<font></font>
<font></font>
//Set the ProtectionType as AllowOnlyFormFields and then unprotect the section<font></font>
doc.Protect(ProtectionType.AllowOnlyFormFields, "123");<font></font>
section.ProtectForm = false;<font></font>
<font></font>
//Save the document to file<font></font>
doc.SaveToFile("Result.docx", FileFormat.Docx2013);<font></font>

以上便如锁定标题以防止在 C# 中编辑 word 文档,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。

猜你喜欢

转载自blog.csdn.net/m0_67129275/article/details/129986042
今日推荐