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 文档,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值