使用自定義密碼加密、解密、保護 Word
Spire.Doc for .NET是一款專門對 Word 文檔進行操作的 .NET 類庫。在于幫助開發(fā)人員無需安裝 Microsoft Word情況下,輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔。擁有近10年專業(yè)開發(fā)經(jīng)驗Spire系列辦公文檔開發(fā)工具,專注于創(chuàng)建、編輯、轉(zhuǎn)換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團隊研發(fā),不依賴第三方軟件,不受其他國家的技術(shù)或法律法規(guī)限制,同時適配國產(chǎn)操作系統(tǒng)如中科方德、中標麒麟等,兼容國產(chǎn)文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
Word 加密是保護 Word 文檔的一種方法,它要求用戶為文檔提供密碼。沒有密碼,加密文件無法打開。本指南中的解決方案演示了如何通過 Spire.Doc for .NET 在 C# 和 VB.NET 中使用自定義密碼加密 Word 文檔。
在 C#、VB.NET 中使用自定義密碼加密 Word
尖頂文檔 .NET專門為 .NET 執(zhí)行 Word 處理任務(wù),提供了 Document.Encrypt 方法,使用戶能夠加密 Word。傳遞給此方法的重載是字符串密碼。首先,加載需要保護的Word文檔。其次,調(diào)用Document.Encrypt方法使用密碼進行加密。第三,保存加密文件并啟動查看。調(diào)試后會彈出一個對話框,要求輸入密碼。輸入密碼打開文件,文件信息會顯示如下,告訴用戶它是加密的。
	 
 
下載并安裝 Spire.Doc for .NET并使用以下代碼加密 Word。
[C#]
	
using Spire.Doc;
namespace Encryption
{
class Program
{
static void Main(string[] args)
{
//Load Document
Document document = new Document();
document.LoadFromFile(@"E:\Work\Documents\WordDocuments\Spire.Doc for .NET.docx");
//Encrypt
document.Encrypt("eiceblue");
//Save and Launch
document.SaveToFile("Encryption.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("Encryption.docx");
}
}
}
	
[VB.NET]
	
Imports Spire.Doc
Namespace Encryption
Friend Class Program
Shared Sub Main(ByVal args() As String)
'Load Document
Dim document As New Document()
document.LoadFromFile("E:\Work\Documents\WordDocuments\Spire.Doc for .NET.docx")
'Encrypt
document.Encrypt("eiceblue")
'Save and Launch
document.SaveToFile("Encryption.docx", FileFormat.Docx)
System.Diagnostics.Process.Start("Encryption.docx")
End Sub
End Class
End Namespace
	
在 C#、VB.NET 中解密 Word 文檔
Word Decryption 是對加密的 Word 文檔進行解碼的過程。它需要密碼或密鑰。如果讀者想要打開和閱讀一個受保護的Word,他們需要首先解密這個Word文檔。本指南演示了一個簡單方便的解決方案,用于通過 Spire.Doc for .NET 在 C# 和 VB.NET 中解密 Word。
尖頂文檔 .NET,專為程序員在沒有Word自動化的情況下操作Word而開發(fā),為用戶提供了Document類的方法Document.LoadFromFile(String fileName, FileFormat fileFormat, String password)來打開加密的Word文檔。它還提供了另一種方法Document.RemoveEncryption()在沒有任何保護的情況下解密 Word。通過這兩種方法,用戶可以使用 Spire.Doc for .NET 輕松解密 Word。下載并安裝 Spire.Doc for .NET。然后按照代碼解密。
[C#]
	
using Spire.Doc;
namespace DecryptWord
{
class Decryption
{
static void Main(string[] args)
{
//Load Encrypted Word
Document document = new Document();
document.LoadFromFile(@"E:\Work\Documents\Student Transcript.docx", FileFormat.Docx,"123456");
//Decrypt
document.RemoveEncryption();
//Save and Launch
document.SaveToFile("decryption.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("decryption.docx");
}
}
}
	
[VB.NET]
	
Imports Spire.Doc
Namespace DecryptWord
Friend Class Decryption
Shared Sub Main(ByVal args() As String)
'Load Encrypted Word
Dim document As New Document()
document.LoadFromFile("E:\Work\Documents\Student Transcript.docx", FileFormat.Docx, "123456")
'Decrypt
document.RemoveEncryption()
'Save and Launch
document.SaveToFile("decryption.docx", FileFormat.Docx)
System.Diagnostics.Process.Start("decryption.docx")
End Sub
End Class
End Namespace
	
在 C# 中使用指定的保護類型保護 Word
字保護不同于字加密。它允許用戶打開和查看 Word 文檔,但有一些用戶無法編輯或只能填寫字段的權(quán)限。本指南中的解決方案演示了使用 Spire.Doc for .NET 在 C# 和 VB.NET 中使用指定保護類型保護 Word 的解決方案。以下屏幕截圖顯示了只讀保護后的結(jié)果。
	 
 
	無保護:設(shè)置沒有保護的文檔。
AllowOnlyRevisions:允許向 Word 添加修訂標記。
AllowOnlyComments:允許修改 Word 中的注釋。
AllowOnlyFormFields:允許在 Word 的表單域中輸入數(shù)據(jù)。
AllowOnlyReading:只允許閱讀 Word。
下面的代碼顯示了如何使用 AllowOnlyReading 類型保護 Word。下載并安裝 Spire.Doc for .NET并按照代碼進行操作。
[C#]
	
using Spire.Doc;
namespace ProtectWord
{
class Program
{
static void Main(string[] args)
{
//Load Document
Document document = new Document();
document.LoadFromFile(@"E:\Work\Documents\.NET Framework.docx");
//Protect Word
document.Protect(ProtectionType.AllowOnlyReading, "123456");
//Save and Launch
document.SaveToFile("ProtectWord.docx");
System.Diagnostics.Process.Start("ProtectWord.docx");
}
}
}
	
[VB.NET]
	
Imports Spire.Doc
Namespace ProtectWord
Friend Class Program
Shared Sub Main(ByVal args() As String)
'Load Document
Dim document As New Document()
document.LoadFromFile("E:\Work\Documents\.NET Framework.docx")
'Protect Word
document.Protect(ProtectionType.AllowOnlyReading, "123456")
'Save and Launch
document.SaveToFile("ProtectWord.docx")
System.Diagnostics.Process.Start("ProtectWord.docx")
End Sub
End Class
End Namespace
以上便是如何在 C#、VB.NET 中使用自定義密碼加密、解密、保護 Word的教程,如果您有其他問題也可以繼續(xù)瀏覽本系列文章,獲取相關(guān)教程,你還可以給我留言或者加入我們的官方技術(shù)交流群。
歡迎下載|體驗更多E-iceblue產(chǎn)品
獲取更多信息請咨詢 ;技術(shù)交流Q群(767755948)


 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網(wǎng)安備
            50010702500608號
渝公網(wǎng)安備
            50010702500608號
             
            
 客服熱線
客服熱線