Aspose.Words for .NET使用教程:克隆和保護文檔

Aspose.Words無需Microsoft Word也可在任何平臺上滿足Word文檔的一切操作需求。本文將與大家分享如何檢測文件格式和檢查格式兼容性。

下載Aspose.Words for .NET最新試用版

克隆文檔


如果你需要從單個文檔生成數百或數千個文檔,只需將文檔加載到內存中一次,克隆此文檔,然後使用你的數據填充克隆文檔。 這加速了文檔的生成,因爲不需要每次都從文件加載和解析文檔。克隆是使用Document.Clone方法完成的,該方法執行Document的深層副本並返回克隆文檔。下面的代碼示例顯示瞭如何深度克隆文檔。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocument();

// Load the document from disk.
Document doc = new Document(dataDir + "TestFile.doc");

Document clone = doc.Clone();

dataDir = dataDir + "TestFile_clone_out.doc";

// Save the document to disk.
clone.Save(dataDir);

在窗口的標題欄中顯示文檔的標題


如果你需要在PDF的窗口標題欄中顯示文檔的標題,PdfSaveOptions.DisplayDocTitle就可以實現此目的。將此屬性的值設置爲true,窗口的標題欄中就會顯示文檔的標題。如果此屬性的值爲false,則標題欄應顯示包含該文檔的PDF文件的名稱。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_QuickStart();

// Load the document from disk.
Document doc = new Document(dataDir + "Template.doc");

PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.DisplayDocTitle = true;

dataDir = dataDir + "Template_out.pdf";

// Save the document in PDF format.
doc.Save(dataDir, saveOptions);

保護文件


當文檔受到保護時,用戶只能進行有限的更改,例如添加註釋,修訂或填寫表單。即使文檔受密碼保護,Aspose.Words也不需要密碼來打開,修改或取消保護此文檔。使用Aspose.Words保護文檔時,你可以選擇保留現有密碼或指定新密碼。

如果你需要確保文檔確實受到保護,請考慮對文檔進行數字簽名。Aspose.Words支持檢測DOC,OOXML和ODT文檔的數字簽名。Aspose.Words還保留應用於文檔中包含的VBA項目(宏)的數字簽名。

注意:在Microsoft Word中保護的文檔可以很輕易地被一個沒有密碼的用戶變成不受保護的文檔。當文檔受到保護時,可以在Microsoft Word中打開,保存爲RTF或WordprocessingML文檔,然後使用記事本或任何純文本編輯器刪除保護密碼。之後,用戶可以在Microsoft Word中再次打開文檔並另存爲不受保護的DOC。

保護文檔


使用Document.Protect方法保護文檔免受更改。此方法通過傳遞一個Document.Protect來接受ProtectionType參數和可選的密碼。下面的代碼示例顯示瞭如何保護文檔。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(inputFileName);
doc.Protect(ProtectionType.AllowOnlyFormFields, "password");

取消保護文檔


調用Document.Unprotect即使具有保護密碼也會取消保護。下面的代碼示例顯示瞭如何取消保護文檔。請注意,密碼不是必需的。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(inputFileName);
doc.Unprotect();

獲得保護類型


你可以通過獲取Document.ProtectionType屬性的值來檢索文檔保護的類型。下面的代碼示例顯示瞭如何獲取當前在文檔中設置的保護類型。

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(inputFileName);
ProtectionType protectionType = doc.ProtectionType;

未完待續~

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章