Aspose.Words for .NET使用教程:訪問、更新和刪除文檔屬性

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

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

文檔概述


Document是Aspose.Words中的中心類,代表文檔並提供各種文檔屬性和方法,如保存或保護文檔。

無論你想用Aspose.Words執行什麼:從頭開始創建一個新文檔,打開一個用於郵件合併的模板,從文檔中獲取不同的部分等等,使用Document類都可以實現。Document對象包含所有內容和格式,樣式, 內置和自定義屬性以及用於郵件合併的MailMerge對象。

document允許你檢索整個文檔或單獨部分的文本,書籤和表單字段。document包含Section對象的集合,便於你獲取特定部分或執行某些操作,如複製/移動部分。文檔可以隨時保存到文件或流中。文檔也可以發送到客戶端瀏覽器。

 

使用文檔屬性


文檔屬性允許一些有用的信息與文檔一起存儲。有系統(內置)和用戶定義(自定義)屬性。內置屬性包含文檔標題,作者姓名,文檔統計信息等內容。自定義屬性只有名稱和值,可由用戶定義。你可以在文檔自動化項目中使用文檔屬性來存儲一些有用的信息,例如文檔被接收/處理/加蓋時間等等。

在Microsoft Word中訪問文檔屬性

在Microsoft Word中,你可以使用File|Properties 菜單查看文檔屬性。

Working with Document Properties-001

在Aspose.Words中訪問文檔屬性

要在Aspose.Words中訪問文檔屬性,請執行以下操作:

Document.BuiltInDocumentProperties 返回BuiltInDocumentProperties對象,Document.CustomDocumentProperties 返回CustomDocumentProperties對象。 這兩個對象都是DocumentProperty對象的集合。這些對象可以通過索引器屬性通過名稱或索引獲得。此外,BuiltInDocumentProperties 還通過一組返回適當類型值的類型屬性提供對文檔屬性的訪問。CustomDocumentProperties 允許從文檔中添加或刪除文檔屬性。下面的代碼示例顯示瞭如何枚舉文檔中的所有內置屬性和自定義屬性。

// For complete examples and data files, please go to https://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類允許你獲取文檔屬性的名稱,值和類型:

  • 要獲取屬性的名稱,請使用DocumentProperty.Name。
  • 要獲取屬性的值,請使用DocumentProperty.Value。DocumentProperty.Value 返回一個Object,但是有一組方法允許你獲取轉換爲特定類型的屬性的值。
  • 要獲取屬性的類型,請使用DocumentProperty.Type。使用此屬性會返回PropertyType的枚舉值之一。在瞭解屬性的類型後,可以使用 DocumentProperty.ToXXX 方法(如DocumentProperty.ToString和DocumentProperty.ToInt)來獲取相應類型的值,而不是獲取 DocumentProperty.Value

更新內置文檔屬性

雖然Microsoft Word會在需要時自動更新某些文檔屬性,但Aspose.Words不會自動更改任何屬性。例如,Microsoft Word會更新文檔上次打印,保存的時間,更新統計屬性(單詞,段落,字符等計數)。 Aspose.Words不會自動更新任何屬性,但提供了更新某些統計內置文檔屬性的方法。調用Document.UpdateWordCount方法重新計算並更新 BuiltInDocumentProperties 集合中的BuiltInDocumentProperties.Characters,BuiltInDocumentProperties.CharactersWithSpaces,BuiltInDocumentProperties.Words 和 BuiltInDocumentProperties.Paragraphs屬性。這將確保它們與打開或創建文檔後所做的更改同步。

添加或刪除文檔屬性

你無法在Aspose.Words中添加或刪除內置文檔屬性,只能更改它們的值。要在Aspose.Words中添加自定義文檔屬性,請使用 CustomDocumentProperties.Add 傳遞新屬性的名稱和相應類型的值。 該方法返回新創建的DocumentProperty 對象。下面的代碼示例顯示瞭如何檢查文檔中是否存在具有給定名稱的自定義屬性,並添加更多自定義文檔屬性。

// For complete examples and data files, please go to https://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 https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "Properties.doc");
doc.CustomDocumentProperties.Remove("Authorized Date");

下篇文章將與大家分享複製文檔、保護文檔等。如果你有任何問題或意見,歡迎在下方評論區留言~


在官網下載Aspose.Total速度慢嗎?   ↓↓↓   Aspose.Total最新試用版大放送,更有海量資源!

Aspose新版本

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