Beispiel #1
0
        public void DocumentPropertyCollection()
        {
            //ExStart
            //ExFor:CustomDocumentProperties.Add(String,String)
            //ExFor:CustomDocumentProperties.Add(String,Boolean)
            //ExFor:CustomDocumentProperties.Add(String,int)
            //ExFor:CustomDocumentProperties.Add(String,DateTime)
            //ExFor:CustomDocumentProperties.Add(String,Double)
            //ExFor:Properties.DocumentPropertyCollection
            //ExFor:Properties.DocumentPropertyCollection.Clear
            //ExFor:Properties.DocumentPropertyCollection.Contains(System.String)
            //ExFor:Properties.DocumentPropertyCollection.GetEnumerator
            //ExFor:Properties.DocumentPropertyCollection.IndexOf(System.String)
            //ExFor:Properties.DocumentPropertyCollection.RemoveAt(System.Int32)
            //ExFor:Properties.DocumentPropertyCollection.Remove
            //ExSummary:Shows how to add custom properties to a document.
            // 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);
            //ExEnd
        }
Beispiel #2
0
        public void DocumentPropertyCollection()
        {
            //ExStart
            //ExFor:CustomDocumentProperties.Add(String,String)
            //ExFor:CustomDocumentProperties.Add(String,Boolean)
            //ExFor:CustomDocumentProperties.Add(String,int)
            //ExFor:CustomDocumentProperties.Add(String,DateTime)
            //ExFor:CustomDocumentProperties.Add(String,Double)
            //ExFor:DocumentProperty.Type
            //ExFor:Properties.DocumentPropertyCollection
            //ExFor:Properties.DocumentPropertyCollection.Clear
            //ExFor:Properties.DocumentPropertyCollection.Contains(System.String)
            //ExFor:Properties.DocumentPropertyCollection.GetEnumerator
            //ExFor:Properties.DocumentPropertyCollection.IndexOf(System.String)
            //ExFor:Properties.DocumentPropertyCollection.RemoveAt(System.Int32)
            //ExFor:Properties.DocumentPropertyCollection.Remove
            //ExFor:PropertyType
            //ExSummary:Shows how to work with a document's custom properties.
            Document doc = new Document();
            CustomDocumentProperties properties = doc.CustomDocumentProperties;

            Assert.AreEqual(0, properties.Count);

            // Custom document properties are key-value pairs that we can add to the document.
            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);

            // The collection sorts the custom properties in alphabetic order.
            Assert.AreEqual(1, properties.IndexOf("Authorized Amount"));
            Assert.AreEqual(5, properties.Count);

            // Print every custom property in the document.
            using (IEnumerator <DocumentProperty> enumerator = properties.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    Console.WriteLine($"Name: \"{enumerator.Current.Name}\"\n\tType: \"{enumerator.Current.Type}\"\n\tValue: \"{enumerator.Current.Value}\"");
                }
            }

            // Display the value of a custom property using a DOCPROPERTY field.
            DocumentBuilder  builder = new DocumentBuilder(doc);
            FieldDocProperty field   = (FieldDocProperty)builder.InsertField(" DOCPROPERTY \"Authorized By\"");

            field.Update();

            Assert.AreEqual("John Doe", field.Result);

            // We can find these custom properties in Microsoft Word via "File" -> "Properties" > "Advanced Properties" > "Custom".
            doc.Save(ArtifactsDir + "DocumentProperties.DocumentPropertyCollection.docx");

            // Below are three ways or removing custom properties from a document.
            // 1 -  Remove by index:
            properties.RemoveAt(1);

            Assert.False(properties.Contains("Authorized Amount"));
            Assert.AreEqual(4, properties.Count);

            // 2 -  Remove by name:
            properties.Remove("Authorized Revision");

            Assert.False(properties.Contains("Authorized Revision"));
            Assert.AreEqual(3, properties.Count);

            // 3 -  Empty the entire collection at once:
            properties.Clear();

            Assert.AreEqual(0, properties.Count);
            //ExEnd
        }