DocumentPropertyCollectionRemove Method |
Namespace: Aspose.Words.Properties
// Create a blank document and get its custom property collection Document doc = new Document(); CustomDocumentProperties properties = doc.CustomDocumentProperties; // The collection will be empty by default Assert.AreEqual(0, properties.Count); // We can populate it with key/value pairs with a variety of value types properties.Add("Authorized", true); properties.Add("Authorized By", "John Doe"); properties.Add("Authorized Date", DateTime.Today); properties.Add("Authorized Revision", doc.BuiltInDocumentProperties.RevisionNumber); properties.Add("Authorized Amount", 123.45); // Custom properties are automatically sorted in alphabetic order Assert.AreEqual(1, properties.IndexOf("Authorized Amount")); Assert.AreEqual(5, properties.Count); // Enumerate and print all custom properties using (IEnumerator<DocumentProperty> enumerator = properties.GetEnumerator()) { while (enumerator.MoveNext()) { Console.WriteLine($"Name: \"{enumerator.Current.Name}\", Type: \"{enumerator.Current.Type}\", Value: \"{enumerator.Current.Value}\""); } } // We can view/edit custom properties by opening the document and looking in File > Properties > Advanced Properties > Custom doc.Save(ArtifactsDir + "Properties.DocumentPropertyCollection.docx"); // We can remove elements from the property collection by index or by name properties.RemoveAt(1); Assert.False(properties.Contains("Authorized Amount")); Assert.AreEqual(4, properties.Count); properties.Remove("Authorized Revision"); Assert.False(properties.Contains("Authorized Revision")); Assert.AreEqual(3, properties.Count); // We can also empty the entire custom property collection at once properties.Clear(); Assert.AreEqual(0, properties.Count);