使用文档属性
文档属性允许存储有关文档的一些有用信息。 这些属性可以分为两组:
- 包含文档标题、作者姓名、文档统计信息等值的系统或内置。
- 用户定义或自定义,作为名称-值对提供,用户可以在其中定义名称和值。
知道有关API和版本号的信息直接写入输出文档非常有用。 例如,将文档转换为PDF时,Aspose.Words用"Aspose.Words"填充"应用程序"字段,用"PDF生产者"字段填充"Aspose.Words对于C++ YY.M.N",其中YY.M.N是用于转换的Aspose.Words的版本。 有关更多详细信息,请参阅 输出文档中包含的生成器或生产者名称.
访问文档属性
要访问Aspose.Words中的文档属性,请使用:
-
BuiltInDocumentProperties获取内置属性。
-
CustomDocumentProperties获取自定义属性。
BuiltInDocumentProperties
BuiltInDocumentProperties
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-C | |
System::String fileName = inputDataDir + u"Properties.doc"; | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(fileName); | |
std::cout << "1. Document name: " << fileName.ToUtf8String() << std::endl; | |
std::cout << "2. Built-in Properties" << std::endl; | |
for (System::SharedPtr<DocumentProperty> prop : System::IterateOver(doc->get_BuiltInDocumentProperties())) | |
{ | |
std::cout << prop->get_Name().ToUtf8String() << " : " << prop->get_Value()->ToString().ToUtf8String() << std::endl; | |
} | |
std::cout << "3. Custom Properties" << std::endl; | |
for (System::SharedPtr<DocumentProperty> prop : System::IterateOver(doc->get_CustomDocumentProperties())) | |
{ | |
std::cout << prop->get_Name().ToUtf8String() << " : " << prop->get_Value()->ToString().ToUtf8String() << std::endl; | |
} |
在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-C | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Properties.doc"); | |
System::SharedPtr<CustomDocumentProperties> props = doc->get_CustomDocumentProperties(); | |
if (props->idx_get(u"Authorized") == nullptr) | |
{ | |
props->Add(u"Authorized", true); | |
props->Add(u"Authorized By", System::String(u"John Smith")); | |
props->Add(u"Authorized Date", System::DateTime::get_Today()); | |
props->Add(u"Authorized Revision", doc->get_BuiltInDocumentProperties()->get_RevisionNumber()); | |
props->Add(u"Authorized Amount", 123.45); | |
} |
下面的代码示例演示如何删除自定义文档属性:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Properties.doc"); | |
doc->get_CustomDocumentProperties()->Remove(u"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-C | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"test.docx"); | |
// Retrieve a list of all custom document properties from the file. | |
System::SharedPtr<CustomDocumentProperties> customProperties = doc->get_CustomDocumentProperties(); | |
// Add linked to content property. | |
System::SharedPtr<DocumentProperty> customProperty = customProperties->AddLinkToContent(u"PropertyName", u"BookmarkName"); | |
// Also, accessing the custom document property can be performed by using the property name. | |
customProperty = customProperties->idx_get(u"PropertyName"); | |
// Check whether the property is linked to content. | |
bool isLinkedToContent = customProperty->get_IsLinkToContent(); | |
// Get the source of the property. | |
System::String source = customProperty->get_LinkSource(); | |
// Get the value of the property. | |
System::String value = customProperty->get_Value()->ToString(); |
获取文档变量
您可以使用Variables属性获取文档变量的集合。 变量名和值是字符串。
下面的代码示例演示如何枚举文档变量:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
// The path to the documents directory. | |
System::String inputDataDir = GetInputDataDir_WorkingWithDocument(); | |
// Load the template document. | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"TestFile.doc"); | |
System::String variables = u""; | |
for (System::Collections::Generic::KeyValuePair<System::String, System::String> entry : System::IterateOver(doc->get_Variables())) | |
{ | |
System::String name = entry.get_Key(); | |
System::String value = entry.get_Value(); | |
if (variables == u"") | |
{ | |
// Do something useful. | |
variables = System::String(u"Name: ") + name + u"," + u"Value: " + value; | |
} | |
else | |
{ | |
variables = variables + u"Name: " + name + u"," + u"Value: " + value; | |
} | |
} |
从文档中删除个人信息
如果要与其他人共享Word文档,则可能需要删除作者姓名和公司等个人信息。 为此,使用RemovePersonalInformation属性设置标志,指示Microsoft Word将在保存文档时从注释、修订和文档属性中删除所有用户信息。
下面的代码示例演示如何删除个人信息:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Properties.doc"); | |
doc->set_RemovePersonalInformation(true); | |
System::String outputPath = outputDataDir + u"DocProperties.RemovePersonalInformation.docx"; | |
doc->Save(outputPath); |