打開文檔以進(jìn)行只讀訪問
Aspose.Words是一種高級(jí)Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無需在跨平臺(tái)應(yīng)用程序中直接使用Microsoft Word。
Aspose API支持流行文件格式處理,并允許將各類文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
有時(shí)您想要打開文檔來檢查或檢索某些信息,并且希望以文檔保持不變的方式執(zhí)行此操作。在這些情況下,您希望以只讀方式打開文檔。
使用 Aspose.WordsAspose.Words 具有公共類WriteProtection,用于指定文檔的寫保護(hù)設(shè)置。使用ReadOnlyRecommended屬性和SetPassword方法將文檔設(shè)置為只讀以限制編輯。
以下代碼示例展示了如何將文檔設(shè)置為只讀:
// For complete examples and data files, please go to //github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Open document as read-only");
// Enter a password that's up to 15 characters long.
doc.WriteProtection.SetPassword("MyPassword");
// Make the document as read-only.
doc.WriteProtection.ReadOnlyRecommended = true;
// Apply write protection as read-only.
doc.Protect(ProtectionType.ReadOnly);
doc.Save(ArtifactsDir + "DocumentProtection.ReadOnlyProtection.docx");
您還可以使用 Open XML SDK 執(zhí)行相同的操作。同時(shí)請(qǐng)注意,它看起來有些更復(fù)雜、更麻煩。下面的代碼示例顯示了如何添加一些文本并嘗試保存更改以顯示訪問權(quán)限是只讀的。一旦您有權(quán)訪問主文檔部分的正文,您就可以通過添加Paragraph、Run 和 Text類的實(shí)例來添加文本。這會(huì)生成所需的WordprocessingML標(biāo)記。
以下代碼示例展示了如何將文檔設(shè)置為只讀:
public void OpenReadOnlyAccessFeature()
{
	// Open a WordprocessingDocument based on a filepath.
	using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(MyDir + "Open readonly access.docx", false))
	{
		// Assign a reference to the existing document body.  
		Body body = wordDocument.MainDocumentPart.Document.Body;
		// Attempt to add some text.
		Paragraph para = body.AppendChild(new Paragraph());
		Run run = para.AppendChild(new Run());
		run.AppendChild(new Text("Append text in body, but text is not saved - Open wordprocessing document readonly"));
		// Call the "Save" method to generate an exception and show that access is read-only.
		using (Stream stream = File.Create(ArtifactsDir + "Open readonly access - OpenXML.docx"))
		{
			wordDocument.MainDocumentPart.Document.Save(stream);
		}
	}
}
 下載此示例的示例文件。                
            
 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網(wǎng)安備
            50010702500608號(hào)
渝公網(wǎng)安備
            50010702500608號(hào)
             
            
 客服熱線
客服熱線