鎖定 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 最新下載
部分保護允許用戶只能編輯表單(如果有)而不是其中的任何其他內容。當我們保護文檔時,我們可以指定文檔的特定部分受到保護。如果我們想要保護 Word 文檔的某些部分,這很有用。
以下代碼片段演示了相同的內容。
第 1 步:初始化 Document 類的一個實例。
Document doc = new Document();
第 2 步:在文檔中添加兩個部分。
Section s1 = doc.AddSection(); Section s2 = doc.AddSection();
第 3 步:將一些文本附加到第 1 節和第 2 節。
s1.AddParagraph().AppendText("section 1"); s2.AddParagraph().AppendText("section 2");
第 4 步:使用 AllowOnlyFormFields 保護類型保護文檔。
doc.Protect(ProtectionType.AllowOnlyFormFields, "123");
第 5 步:取消保護第 2 部分。
s2.ProtectForm = false;
第 6 步:保存文檔。
doc.SaveToFile("Protect_Section.docx");
結果:運行程序,我們應該得到第1節被保護的文件,只允許在表單域中編輯,而第2節可以自由編輯。
完整代碼:
[C#]
using Spire.Doc; namespace LockSection { class Program { static void Main(string[] args) { Document doc = new Document(); Section s1 = doc.AddSection(); Section s2 = doc.AddSection(); s1.AddParagraph().AppendText("section 1"); s2.AddParagraph().AppendText("section 2"); //protect all sections doc.Protect(ProtectionType.AllowOnlyFormFields, "123"); //unprotect section 2 s2.ProtectForm = false; doc.SaveToFile("Protect_Section.docx"); } } }
[VB.NET]
Imports Spire.Doc Namespace LockSection Class Program Private Shared Sub Main(args As String()) Dim doc As New Document() Dim s1 As Section = doc.AddSection() Dim s2 As Section = doc.AddSection() s1.AddParagraph().AppendText("section 1") s2.AddParagraph().AppendText("section 2") 'protect all sections doc.Protect(ProtectionType.AllowOnlyFormFields, "123") 'unprotect section 2 s2.ProtectForm = False doc.SaveToFile("Protect_Section.docx") End Sub End Class End Namespace
以上便是如何在 C#、VB.NET 中鎖定 Word 文檔的指定部分的教程,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
獲取更多信息請咨詢 ;技術交流Q群(767755948)