How to: Specify Document Properties
- 3 minutes to read
The SpreadsheetControl allows you to set and edit the document properties (metadata) associated and stored with a workbook. To specify the standard document properties (such as DocumentProperties.Title, DocumentProperties.Author, DocumentProperties.Subject, DocumentProperties.Description etc.), use the IWorkbook.DocumentProperties property, which provides access to the DocumentProperties object, containing basic information about a workbook. Note that some of these properties are updated automatically when a document is created (DocumentProperties.Author, DocumentProperties.Created), last modified and saved (DocumentProperties.LastModifiedBy, DocumentProperties.Modified), or printed (DocumentProperties.Printed).
You can also create your own custom document properties by using the DocumentProperties.Custom property.
Built-In Properties
The example below demonstrates how to specify the standard document properties for a workbook using the IWorkbook.DocumentProperties property.
// Set the built-in document properties.
workbook.DocumentProperties.Title = "Spreadsheet API: document properties example";
workbook.DocumentProperties.Description = "How to manage document properties using the Spreadsheet API";
workbook.DocumentProperties.Keywords = "Spreadsheet, API, properties, OLEProps";
workbook.DocumentProperties.Company = "Developer Express Inc.";
// Display the specified built-in properties in a worksheet.
worksheet["B3"].Value = "Title";
worksheet["C3"].Value = workbook.DocumentProperties.Title;
worksheet["B4"].Value = "Description";
worksheet["C4"].Value = workbook.DocumentProperties.Description;
worksheet["B5"].Value = "Keywords";
worksheet["C5"].Value = workbook.DocumentProperties.Keywords;
worksheet["B6"].Value = "Company";
worksheet["C6"].Value = workbook.DocumentProperties.Company;
Custom Properties
The example below demonstrates how to define the custom document properties for a workbook. Use the DocumentProperties.Custom property to get access to a storage of all user-defined properties specified in a document. To set an individual custom property with the specified name, use the DocumentCustomProperties.Item property.
// Set the custom document properties.
workbook.DocumentProperties.Custom["Revision"] = 3;
workbook.DocumentProperties.Custom["Completed"] = true;
workbook.DocumentProperties.Custom["Published"] = DateTime.Now;
// Display the specified custom properties in a worksheet.
IEnumerable<string> customPropertiesNames = workbook.DocumentProperties.Custom.Names;
int rowIndex = 2;
foreach (string propertyName in customPropertiesNames)
{
worksheet[rowIndex, 1].Value = propertyName;
worksheet[rowIndex, 2].Value = workbook.DocumentProperties.Custom[propertyName];
if (worksheet[rowIndex, 2].Value.IsDateTime)
worksheet[rowIndex, 2].NumberFormat = "[$-409]m/d/yyyy h:mm AM/PM";
rowIndex++;
}
Use the DocumentCustomProperties.LinkToContent property to associate the desired custom document property to the cell or cell range content, as shown on the code snippet below.
//Define a name to the cell to be linked to the custom property
workbook.DefinedNames.Add("checked_by", "E6");
//Connect the custom property with the named cell
workbook.DocumentProperties.Custom.LinkToContent("Checked by", "checked_by");
To remove all the custom document properties from a workbook, use the DocumentCustomProperties.Clear method.
The example below demonstrates how to remove all the custom document properties from a workbook.
// Remove all custom document properties.
workbook.DocumentProperties.Custom.Clear();