使用文档属性

文档属性允许存储有关文档的一些有用信息。这些属性可以分为两组:

  • 包含文档标题、作者姓名、文档统计信息等值的系统或内置。
  • 用户定义或自定义,以名称-值对的形式提供,用户可以在其中定义名称和值。

知道有关 API 和版本号的信息直接写入输出文档是很有用的。例如,将文档转换为 PDF 时,Aspose.Words 在"应用程序"字段中填写"Aspose.Words",在"PDF 制作者"字段中填写"Aspose.Words for .NET YY.MN",其中 YY.M.N 是用于转换的 Aspose.Words 版本。有关更多详细信息,请参阅 输出文档中包含的生成者或生产者名称

访问文档属性

要访问 Aspose.Words 中的文档属性,请使用:

BuiltInDocumentPropertiesCustomDocumentPropertiesDocumentProperty 对象的集合。这些对象可以通过索引器属性按名称或索引获取。

BuiltInDocumentProperties还通过一组返回适当类型值的输入属性提供对文档属性的访问。 CustomDocumentProperties 使您能够在文档中添加或删除文档属性。

DocumentProperty 类允许您获取文档属性的名称、值和类型。 Value 返回一个对象,但有一组方法允许您将属性值转换为特定类型。了解属性的类型后,可以使用 DocumentProperty.ToXXX 方法之一(例如 DocumentProperty.ToStringDocumentProperty.ToInt)来获取适当类型的值。

以下代码示例演示如何枚举文档中的所有内置和自定义属性:

// 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);

在 Microsoft Word 中,您可以使用"文件 → 属性"菜单访问文档属性。

工作与文档属性-1.png

添加或删除文档属性

您无法使用 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-.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);
}

以下代码示例演示如何删除自定义文档属性:

// 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.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-.NET
Document doc = new Document(dataDir + "test.docx");
// Retrieve a list of all custom document properties from the file.
CustomDocumentProperties customProperties = doc.CustomDocumentProperties;
// 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["PropertyName"];
// Check whether the property is linked to content.
bool isLinkedToContent = customProperty.IsLinkToContent;
// Get the source of the property.
string source = customProperty.LinkSource;
// Get the value of the property.
string value = customProperty.Value.ToString();

获取文档变量

您可以使用 Variables 属性获取文档变量的集合。变量名称和值是字符串。

以下代码示例展示了如何枚举文档变量:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocument();
// Load the template document.
Document doc = new Document(dataDir + "TestFile.doc");
string variables = "";
foreach (KeyValuePair<string, string> entry in doc.Variables)
{
string name = entry.Key.ToString();
string value = entry.Value.ToString();
if (variables == "")
{
// Do something useful.
variables = "Name: " + name + "," + "Value: {1}" + value;
}
else
{
variables = variables + "Name: " + name + "," + "Value: {1}" + value;
}
}

从文档中删除个人信息

如果您想与其他人共享 Word 文档,您可能需要删除作者姓名和公司等个人信息。为此,请使用 RemovePersonalInformation 属性设置标志,指示 Microsoft Word 将在保存文档时从注释、修订和文档属性中删除所有用户信息。

以下代码示例展示了如何删除个人信息:

// 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.RemovePersonalInformation = true;
dataDir = dataDir + "RemovePersonalInformation_out.docx";
doc.Save(dataDir);