翻譯|使用教程|編輯:黃竹雯|2019-05-09 11:07:22.000|閱讀 730 次
概述:Document是Aspose.Words中的中心類,代表文檔并提供各種文檔屬性和方法,如保存或保護文檔。document允許你檢索整個文檔或單獨部分的文本,書簽和表單字段。document包含Section對象的集合,便于你獲取特定部分或執(zhí)行某些操作,如復制/移動部分。文檔可以隨時保存到文件或流中。文檔也可以發(fā)送到客戶端瀏覽器。本文主要介紹文檔屬性。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.Words無需Microsoft Word也可在任何平臺上滿足Word文檔的一切操作需求。本文將與大家分享如何檢測文件格式和檢查格式兼容性。
【下載Aspose.Words for .NET最新試用版】
Document是Aspose.Words中的中心類,代表文檔并提供各種文檔屬性和方法,如保存或保護文檔。
無論你想用Aspose.Words執(zhí)行什么:從頭開始創(chuàng)建一個新文檔,打開一個用于郵件合并的模板,從文檔中獲取不同的部分等等,使用Document類都可以實現。Document對象包含所有內容和格式,樣式, 內置和自定義屬性以及用于郵件合并的MailMerge對象。
document允許你檢索整個文檔或單獨部分的文本,書簽和表單字段。document包含Section對象的集合,便于你獲取特定部分或執(zhí)行某些操作,如復制/移動部分。文檔可以隨時保存到文件或流中。文檔也可以發(fā)送到客戶端瀏覽器。
文檔屬性允許一些有用的信息與文檔一起存儲。有系統(內置)和用戶定義(自定義)屬性。內置屬性包含文檔標題,作者姓名,文檔統計信息等內容。自定義屬性只有名稱和值,可由用戶定義。你可以在文檔自動化項目中使用文檔屬性來存儲一些有用的信息,例如文檔被接收/處理/加蓋時間等等。
在Microsoft Word中,你可以使用File|Properties 菜單查看文檔屬性。
要在Aspose.Words中訪問文檔屬性,請執(zhí)行以下操作:
Document.BuiltInDocumentProperties 返回BuiltInDocumentProperties對象,Document.CustomDocumentProperties 返回CustomDocumentProperties對象。 這兩個對象都是DocumentProperty對象的集合。這些對象可以通過索引器屬性通過名稱或索引獲得。此外,BuiltInDocumentProperties 還通過一組返回適當類型值的類型屬性提供對文檔屬性的訪問。CustomDocumentProperties 允許從文檔中添加或刪除文檔屬性。下面的代碼示例顯示了如何枚舉文檔中的所有內置屬性和自定義屬性。
// For complete examples and data files, please go to //github.com/aspose-words/Aspose.Words-for-.NET string fileName = dataDir + "Properties.doc"; Document doc = new Document(fileName); Console.WriteLine("1. Document name: {0}", fileName); Console.WriteLine("2. Built-in Properties"); foreach (DocumentProperty prop in doc.BuiltInDocumentProperties) Console.WriteLine("{0} : {1}", prop.Name, prop.Value); Console.WriteLine("3. Custom Properties"); foreach (DocumentProperty prop in doc.CustomDocumentProperties) Console.WriteLine("{0} : {1}", prop.Name, prop.Value);
DocumentProperty類允許你獲取文檔屬性的名稱,值和類型:
雖然Microsoft Word會在需要時自動更新某些文檔屬性,但Aspose.Words不會自動更改任何屬性。例如,Microsoft Word會更新文檔上次打印,保存的時間,更新統計屬性(單詞,段落,字符等計數)。
Aspose.Words不會自動更新任何屬性,但提供了更新某些統計內置文檔屬性的方法。調用Document.UpdateWordCount方法重新計算并更新 BuiltInDocumentProperties 集合中的BuiltInDocumentProperties.Characters,BuiltInDocumentProperties.CharactersWithSpaces,BuiltInDocumentProperties.Words 和 BuiltInDocumentProperties.Paragraphs屬性。這將確保它們與打開或創(chuàng)建文檔后所做的更改同步。
你無法在Aspose.Words中添加或刪除內置文檔屬性,只能更改它們的值。要在Aspose.Words中添加自定義文檔屬性,請使用 CustomDocumentProperties.Add 傳遞新屬性的名稱和相應類型的值。 該方法返回新創(chuàng)建的DocumentProperty 對象。下面的代碼示例顯示了如何檢查文檔中是否存在具有給定名稱的自定義屬性,并添加更多自定義文檔屬性。
// For complete examples and data files, please go to //github.com/aspose-words/Aspose.Words-for-.NET Document doc = new Document(dataDir + "Properties.doc"); CustomDocumentProperties props = doc.CustomDocumentProperties; if (props["Authorized"] == null) { props.Add("Authorized", true); props.Add("Authorized By", "John Smith"); props.Add("Authorized Date", DateTime.Today); props.Add("Authorized Revision", doc.BuiltInDocumentProperties.RevisionNumber); props.Add("Authorized Amount", 123.45); }
若要刪除自定義屬性,請使用 DocumentPropertyCollection.Remove 傳遞要刪除的屬性的名稱。下面的代碼示例顯示了如何刪除自定義文檔屬性。
// For complete examples and data files, please go to //github.com/aspose-words/Aspose.Words-for-.NET Document doc = new Document(dataDir + "Properties.doc"); doc.CustomDocumentProperties.Remove("Authorized Date");
下篇文章將與大家分享復制文檔、保護文檔等。如果你有任何問題或意見,歡迎在下方評論區(qū)留言~
在官網下載Aspose.Total速度慢嗎? ↓↓↓ Aspose.Total最新試用版大放送,更有海量資源!
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn