使用文档属性
文档属性允许存储有关文档的一些有用信息。 这些属性可以分为两组:
- 包含文档标题、作者姓名、文档统计信息等值的系统或内置。
- 用户定义或自定义,作为名称-值对提供,用户可以在其中定义名称和值。
知道有关API和版本号的信息直接写入输出文档非常有用。 例如,将文档转换为PDF时,Aspose.Words用"Aspose.Words"填充"应用程序"字段,用"PDF生产者"字段用"Aspose.Words填充JavaYY。M.N",其中YY.M.N是用于转换的Aspose.Words的版本。 有关更多详细信息,请参阅 输出文档中包含的生成器或生产者名称.
访问文档属性
要访问Aspose.Words中的文档属性,请使用:
-
BuiltInDocumentProperties获取内置属性。
-
CustomDocumentProperties获取自定义属性。
BuiltInDocumentProperties
BuiltInDocumentProperties
DocumentProperty类允许您获取文档属性的名称、值和类型。 [价值]https://reference.aspose.com/words/java/com.aspose.words/documentproperty#Value)返回一个对象,但有一组方法允许您获取转换为特定类型的属性值。了解该方法是怎样的,可以使用DocumentProperty.ToXXX方法,例如DocumentProperty.ToString和DocumentProperty.ToInt,获取相应类型的值。
下面的代码示例演示如何枚举文档中的所有内置属性和自定义属性:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
String fileName = dataDir + "Properties.doc"; | |
Document doc = new Document(fileName); | |
System.out.println("1. Document name: " + fileName); | |
System.out.println("2. Built-in Properties"); | |
for (DocumentProperty prop : doc.getBuiltInDocumentProperties()) | |
System.out.println(prop.getName() + " : " + prop.getValue()); | |
System.out.println("3. Custom Properties"); | |
for (DocumentProperty prop : doc.getCustomDocumentProperties()) | |
System.out.println(prop.getName() + " : " + prop.getValue()); |
在Microsoft Word中,您可以使用"文件→属性"菜单访问文档属性。

添加或删除文档属性
不能使用Aspose.Words添加或删除内置文档属性。 您只能更改或更新它们的值。
要使用Aspose.Words添加自定义文档属性,请使用Add方法,传递新的属性名称和相应类型的值。 该方法返回新创建的DocumentProperty对象。
若要删除自定义属性,请使用Remove方法,将要删除的属性名称传递给它,或使用RemoveAt方法按索引删除属性。 您还可以使用Clear方法删除所有属性。
下面的代码示例检查文档中是否存在具有给定名称的自定义属性,并添加更多的自定义文档属性:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "Properties.doc"); | |
CustomDocumentProperties props = doc.getCustomDocumentProperties(); | |
if (props.get("Authorized") == null) { | |
props.add("Authorized", true); | |
props.add("Authorized By", "John Smith"); | |
props.add("Authorized Date", new Date()); | |
props.add("Authorized Revision", doc.getBuiltInDocumentProperties().getRevisionNumber()); | |
props.add("Authorized Amount", 123.45); | |
} |
下面的代码示例演示如何删除自定义文档属性:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "Properties.doc"); | |
doc.getCustomDocumentProperties().remove("Authorized Date"); |
更新内置文档属性
Aspose.Words不会像Microsoft Word对某些属性所做的那样自动更新文档属性,但提供了一种更新某些统计内置文档属性的方法。 调用UpdateWordCount方法重新计算和更新以下属性:
创建链接到内容的新自定义属性
Aspose.Words提供AddLinkToContent方法来创建链接到内容的新自定义文档属性。 此属性返回新创建的属性对象,如果LinkSource无效,则返回null。
下面的代码示例演示如何配置指向自定义属性的链接:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "test.docx"); | |
// Retrieve a list of all custom document properties from the file. | |
CustomDocumentProperties customProperties = doc.getCustomDocumentProperties(); | |
// Add linked to content property. | |
DocumentProperty customProperty = customProperties.addLinkToContent("PropertyName", "BookmarkName"); | |
// Also, accessing the custom document property can be performed by using the | |
// property name. | |
customProperty = customProperties.get("PropertyName"); | |
// Check whether the property is linked to content. | |
boolean isLinkedToContent = customProperty.isLinkToContent(); | |
// Get the source of the property. | |
String source = customProperty.getLinkSource(); | |
// Get the value of the property. | |
String value = customProperty.getValue().toString(); |
获取文档变量
您可以使用Variables属性获取文档变量的集合。 变量名和值是字符串。
下面的代码示例演示如何枚举文档变量:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "Document.doc"); | |
for (java.util.Map.Entry entry : doc.getVariables()) { | |
String name = entry.getKey().toString(); | |
String value = entry.getValue().toString(); | |
// Do something useful. | |
System.out.println("Name: " + name + ", Value: " + value); | |
} |
从文档中删除个人信息
如果您想与其他人共享Word文档,您可能需要删除作者姓名和公司等个人信息。 为此,使用RemovePersonalInformation属性设置标志,指示Microsoft Word将在保存文档时从注释、修订和文档属性中删除所有用户信息。