翻譯|使用教程|編輯:黃竹雯|2019-05-09 11:07:22.000|閱讀 730 次
概述:Document是Aspose.Words中的中心類,代表文檔并提供各種文檔屬性和方法,如保存或保護(hù)文檔。document允許你檢索整個(gè)文檔或單獨(dú)部分的文本,書(shū)簽和表單字段。document包含Section對(duì)象的集合,便于你獲取特定部分或執(zhí)行某些操作,如復(fù)制/移動(dòng)部分。文檔可以隨時(shí)保存到文件或流中。文檔也可以發(fā)送到客戶端瀏覽器。本文主要介紹文檔屬性。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Aspose.Words無(wú)需Microsoft Word也可在任何平臺(tái)上滿足Word文檔的一切操作需求。本文將與大家分享如何檢測(cè)文件格式和檢查格式兼容性。
【下載Aspose.Words for .NET最新試用版】
Document是Aspose.Words中的中心類,代表文檔并提供各種文檔屬性和方法,如保存或保護(hù)文檔。
無(wú)論你想用Aspose.Words執(zhí)行什么:從頭開(kāi)始創(chuàng)建一個(gè)新文檔,打開(kāi)一個(gè)用于郵件合并的模板,從文檔中獲取不同的部分等等,使用Document類都可以實(shí)現(xiàn)。Document對(duì)象包含所有內(nèi)容和格式,樣式, 內(nèi)置和自定義屬性以及用于郵件合并的MailMerge對(duì)象。
document允許你檢索整個(gè)文檔或單獨(dú)部分的文本,書(shū)簽和表單字段。document包含Section對(duì)象的集合,便于你獲取特定部分或執(zhí)行某些操作,如復(fù)制/移動(dòng)部分。文檔可以隨時(shí)保存到文件或流中。文檔也可以發(fā)送到客戶端瀏覽器。
文檔屬性允許一些有用的信息與文檔一起存儲(chǔ)。有系統(tǒng)(內(nèi)置)和用戶定義(自定義)屬性。內(nèi)置屬性包含文檔標(biāo)題,作者姓名,文檔統(tǒng)計(jì)信息等內(nèi)容。自定義屬性只有名稱和值,可由用戶定義。你可以在文檔自動(dòng)化項(xiàng)目中使用文檔屬性來(lái)存儲(chǔ)一些有用的信息,例如文檔被接收/處理/加蓋時(shí)間等等。
在Microsoft Word中,你可以使用File|Properties 菜單查看文檔屬性。
要在Aspose.Words中訪問(wèn)文檔屬性,請(qǐng)執(zhí)行以下操作:
Document.BuiltInDocumentProperties 返回BuiltInDocumentProperties對(duì)象,Document.CustomDocumentProperties 返回CustomDocumentProperties對(duì)象。 這兩個(gè)對(duì)象都是DocumentProperty對(duì)象的集合。這些對(duì)象可以通過(guò)索引器屬性通過(guò)名稱或索引獲得。此外,BuiltInDocumentProperties 還通過(guò)一組返回適當(dāng)類型值的類型屬性提供對(duì)文檔屬性的訪問(wèn)。CustomDocumentProperties 允許從文檔中添加或刪除文檔屬性。下面的代碼示例顯示了如何枚舉文檔中的所有內(nèi)置屬性和自定義屬性。
// 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會(huì)在需要時(shí)自動(dòng)更新某些文檔屬性,但Aspose.Words不會(huì)自動(dòng)更改任何屬性。例如,Microsoft Word會(huì)更新文檔上次打印,保存的時(shí)間,更新統(tǒng)計(jì)屬性(單詞,段落,字符等計(jì)數(shù))。
Aspose.Words不會(huì)自動(dòng)更新任何屬性,但提供了更新某些統(tǒng)計(jì)內(nèi)置文檔屬性的方法。調(diào)用Document.UpdateWordCount方法重新計(jì)算并更新 BuiltInDocumentProperties 集合中的BuiltInDocumentProperties.Characters,BuiltInDocumentProperties.CharactersWithSpaces,BuiltInDocumentProperties.Words 和 BuiltInDocumentProperties.Paragraphs屬性。這將確保它們與打開(kāi)或創(chuàng)建文檔后所做的更改同步。
你無(wú)法在Aspose.Words中添加或刪除內(nèi)置文檔屬性,只能更改它們的值。要在Aspose.Words中添加自定義文檔屬性,請(qǐng)使用 CustomDocumentProperties.Add 傳遞新屬性的名稱和相應(yīng)類型的值。 該方法返回新創(chuàng)建的DocumentProperty 對(duì)象。下面的代碼示例顯示了如何檢查文檔中是否存在具有給定名稱的自定義屬性,并添加更多自定義文檔屬性。
// 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); }
若要?jiǎng)h除自定義屬性,請(qǐng)使用 DocumentPropertyCollection.Remove 傳遞要?jiǎng)h除的屬性的名稱。下面的代碼示例顯示了如何刪除自定義文檔屬性。
// 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");
下篇文章將與大家分享復(fù)制文檔、保護(hù)文檔等。如果你有任何問(wèn)題或意見(jiàn),歡迎在下方評(píng)論區(qū)留言~
在官網(wǎng)下載Aspose.Total速度慢嗎? ↓↓↓ Aspose.Total最新試用版大放送,更有海量資源!
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn