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;

未完待续~

 

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