Aspose.Words for .NET使用文档教程(1):文档概述

下载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。
  • 要获取自定义文档属性,请使用Document.CustomDocumentProperties。

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-.NETstring 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-.NETDocument 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-.NETDocument doc = new Document(dataDir + "Properties.doc");doc.CustomDocumentProperties.Remove("Authorized Date");

下篇文章将与大家分享复制文档、保护文档等。如果你有任何问题或意见,欢迎在下方评论区留言~

为你推荐:Aspose专题 – Aspose最新资源合集


想要购买正版授权,或者获取更多Aspose.Words相关信息的朋友可以点击” 咨询在线客服 “~

在官 下载Aspose.Total速度慢吗nbsp;  ↓↓↓ Aspose.Total最新试用版大放送,更有海量资源!

Aspose新版本

标签:文档管理wordAspose.words文档处理

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2019年4月6日
下一篇 2019年4月6日

相关推荐

发表回复

登录后才能评论