Functions for operations with document and content
        public void InsertFieldAfterTextInParagraph()
        {
            string date = DateTime.Today.ToString("d");

            Document doc = DocumentHelper.CreateDocumentFillWithDummyText();

            InsertFieldUsingFieldCode(doc, " DATE ", null, true, 1);

            Assert.AreEqual(String.Format("Hello World!\u0013 DATE \u0014{0}\u0015\r", date), DocumentHelper.GetParagraphText(doc, 1));
        }
        /// <summary>
        /// Insert field into the first paragraph of the current document using field type
        /// </summary>
        private static void InsertFieldUsingFieldType(Document doc, FieldType fieldType, bool updateField, Node refNode, bool isAfter, int paraIndex)
        {
            Paragraph para = DocumentHelper.GetParagraph(doc, paraIndex);

            para.InsertField(fieldType, updateField, refNode, isAfter);
        }
        /// <summary>
        /// Insert field into the first paragraph of the current document using field code and field string
        /// </summary>
        private static void InsertFieldUsingFieldCodeFieldString(Document doc, string fieldCode, string fieldValue, Node refNode, bool isAfter, int paraIndex)
        {
            Paragraph para = DocumentHelper.GetParagraph(doc, paraIndex);

            para.InsertField(fieldCode, fieldValue, refNode, isAfter);
        }
        public void InsertFieldBeforeRunText()
        {
            Document doc = DocumentHelper.CreateDocumentFillWithDummyText();

            //Add some text into the paragraph
            Run run = DocumentHelper.InsertNewRun(doc, " Hello World!", 1);

            InsertFieldUsingFieldCodeFieldString(doc, " AUTHOR ", "Test Field Value", run, false, 1);

            Assert.AreEqual("Hello World!\u0013 AUTHOR \u0014Test Field Value\u0015 Hello World!\r", DocumentHelper.GetParagraphText(doc, 1));
        }
示例#5
0
        public void ParagraphSpacingAndIndents()
        {
            //ExStart
            //ExFor:ParagraphFormat.CharacterUnitLeftIndent
            //ExFor:ParagraphFormat.CharacterUnitRightIndent
            //ExFor:ParagraphFormat.CharacterUnitFirstLineIndent
            //ExFor:ParagraphFormat.LineUnitBefore
            //ExFor:ParagraphFormat.LineUnitAfter
            //ExSummary:Shows how to change paragraph spacing and indents.
            Document        doc     = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
            ParagraphFormat format  = doc.FirstSection.Body.FirstParagraph.ParagraphFormat;

            // Below are five different spacing options, along with the properties that their configuration indirectly affects.
            // 1 -  Left indent:
            Assert.AreEqual(format.LeftIndent, 0.0d);

            format.CharacterUnitLeftIndent = 10.0;

            Assert.AreEqual(format.LeftIndent, 120.0d);

            // 2 -  Right indent:
            Assert.AreEqual(format.RightIndent, 0.0d);

            format.CharacterUnitRightIndent = -5.5;

            Assert.AreEqual(format.RightIndent, -66.0d);

            // 3 -  Hanging indent:
            Assert.AreEqual(format.FirstLineIndent, 0.0d);

            format.CharacterUnitFirstLineIndent = 20.3;

            Assert.AreEqual(format.FirstLineIndent, 243.59d, 0.1d);

            // 4 -  Line spacing before paragraphs:
            Assert.AreEqual(format.SpaceBefore, 0.0d);

            format.LineUnitBefore = 5.1;

            Assert.AreEqual(format.SpaceBefore, 61.1d, 0.1d);

            // 5 -  Line spacing after paragraphs:
            Assert.AreEqual(format.SpaceAfter, 0.0d);

            format.LineUnitAfter = 10.9;

            Assert.AreEqual(format.SpaceAfter, 130.8d, 0.1d);

            builder.Writeln("Lorem ipsum dolor sit amet, consectetur adipiscing elit, " +
                            "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.");
            builder.Write("测试文档测试文档测试文档测试文档测试文档测试文档测试文档测试文档测试" +
                          "文档测试文档测试文档测试文档测试文档测试文档测试文档测试文档测试文档测试文档");
            //ExEnd

            doc    = DocumentHelper.SaveOpen(doc);
            format = doc.FirstSection.Body.FirstParagraph.ParagraphFormat;

            Assert.AreEqual(format.CharacterUnitLeftIndent, 10.0d);
            Assert.AreEqual(format.LeftIndent, 120.0d);

            Assert.AreEqual(format.CharacterUnitRightIndent, -5.5d);
            Assert.AreEqual(format.RightIndent, -66.0d);

            Assert.AreEqual(format.CharacterUnitFirstLineIndent, 20.3d);
            Assert.AreEqual(format.FirstLineIndent, 243.59d, 0.1d);

            Assert.AreEqual(format.LineUnitBefore, 5.1d, 0.1d);
            Assert.AreEqual(format.SpaceBefore, 61.1d, 0.1d);

            Assert.AreEqual(format.LineUnitAfter, 10.9d);
            Assert.AreEqual(format.SpaceAfter, 130.8d, 0.1d);
        }
        public void CreatingCustomXml()
        {
            //ExStart
            //ExFor:CustomXmlPart
            //ExFor:CustomXmlPart.Clone
            //ExFor:CustomXmlPart.Data
            //ExFor:CustomXmlPart.Id
            //ExFor:CustomXmlPart.Schemas
            //ExFor:CustomXmlPartCollection
            //ExFor:CustomXmlPartCollection.Add(CustomXmlPart)
            //ExFor:CustomXmlPartCollection.Add(String, String)
            //ExFor:CustomXmlPartCollection.Clear
            //ExFor:CustomXmlPartCollection.Clone
            //ExFor:CustomXmlPartCollection.Count
            //ExFor:CustomXmlPartCollection.GetById(String)
            //ExFor:CustomXmlPartCollection.GetEnumerator
            //ExFor:CustomXmlPartCollection.Item(Int32)
            //ExFor:CustomXmlPartCollection.RemoveAt(Int32)
            //ExFor:Document.CustomXmlParts
            //ExFor:StructuredDocumentTag.XmlMapping
            //ExFor:XmlMapping.SetMapping(CustomXmlPart, String, String)
            //ExSummary:Shows how to create structured document tag with a custom XML data.
            Document doc = new Document();

            // Construct an XML part that contains data and add it to the document's collection
            // Once the "Developer" tab in Mircosoft Word is enabled,
            // we can find elements from this collection as well as a couple defaults in the "XML Mapping Pane"
            string        xmlPartId      = Guid.NewGuid().ToString("B");
            string        xmlPartContent = "<root><text>Hello, World!</text></root>";
            CustomXmlPart xmlPart        = doc.CustomXmlParts.Add(xmlPartId, xmlPartContent);

            // The data we entered resides in these variables
            Assert.AreEqual(xmlPartContent.ToCharArray(), xmlPart.Data);
            Assert.AreEqual(xmlPartId, xmlPart.Id);

            // XML parts can be referenced by collection index or GUID
            Assert.AreEqual(xmlPart, doc.CustomXmlParts[0]);
            Assert.AreEqual(xmlPart, doc.CustomXmlParts.GetById(xmlPartId));

            // Once the part is created, we can add XML schema associations like this
            xmlPart.Schemas.Add("http://www.w3.org/2001/XMLSchema");

            // We can also clone parts and insert them into the collection directly
            CustomXmlPart xmlPartClone = xmlPart.Clone();

            xmlPartClone.Id = Guid.NewGuid().ToString("B");
            doc.CustomXmlParts.Add(xmlPartClone);

            Assert.AreEqual(2, doc.CustomXmlParts.Count);

            // Iterate through collection with an enumerator and print the contents of each part
            using (IEnumerator <CustomXmlPart> enumerator = doc.CustomXmlParts.GetEnumerator())
            {
                int index = 0;
                while (enumerator.MoveNext())
                {
                    Console.WriteLine($"XML part index {index}, ID: {enumerator.Current.Id}");
                    Console.WriteLine($"\tContent: {Encoding.UTF8.GetString(enumerator.Current.Data)}");
                    index++;
                }
            }

            // XML parts can be removed by index
            doc.CustomXmlParts.RemoveAt(1);

            Assert.AreEqual(1, doc.CustomXmlParts.Count);

            // The XML part collection itself can be cloned also
            CustomXmlPartCollection customXmlParts = doc.CustomXmlParts.Clone();

            // And all elements can be cleared like this
            customXmlParts.Clear();

            // Create a StructuredDocumentTag that will display the contents of our part,
            // insert it into the document and save the document
            StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Block);

            sdt.XmlMapping.SetMapping(xmlPart, "/root[1]/text[1]", "");

            doc.FirstSection.Body.AppendChild(sdt);

            doc.Save(ArtifactsDir + "SDT.CustomXml.docx");
            //ExEnd

            Assert.IsTrue(DocumentHelper.CompareDocs(ArtifactsDir + "SDT.CustomXml.docx", GoldsDir + "SDT.CustomXml Gold.docx"));
        }
        public void CreatingCustomXml()
        {
            //ExStart
            //ExFor:CustomXmlPart
            //ExFor:CustomXmlPart.Clone
            //ExFor:CustomXmlPart.Data
            //ExFor:CustomXmlPart.Id
            //ExFor:CustomXmlPart.Schemas
            //ExFor:CustomXmlPartCollection
            //ExFor:CustomXmlPartCollection.Add(CustomXmlPart)
            //ExFor:CustomXmlPartCollection.Add(String, String)
            //ExFor:CustomXmlPartCollection.Clear
            //ExFor:CustomXmlPartCollection.Clone
            //ExFor:CustomXmlPartCollection.Count
            //ExFor:CustomXmlPartCollection.GetById(String)
            //ExFor:CustomXmlPartCollection.GetEnumerator
            //ExFor:CustomXmlPartCollection.Item(Int32)
            //ExFor:CustomXmlPartCollection.RemoveAt(Int32)
            //ExFor:Document.CustomXmlParts
            //ExFor:StructuredDocumentTag.XmlMapping
            //ExFor:XmlMapping.SetMapping(CustomXmlPart, String, String)
            //ExSummary:Shows how to create a structured document tag with custom XML data.
            Document doc = new Document();

            // Construct an XML part that contains data and add it to the document's collection.
            // If we enable the "Developer" tab in Microsoft Word,
            // we can find elements from this collection in the "XML Mapping Pane", along with a few default elements.
            string        xmlPartId      = Guid.NewGuid().ToString("B");
            string        xmlPartContent = "<root><text>Hello world!</text></root>";
            CustomXmlPart xmlPart        = doc.CustomXmlParts.Add(xmlPartId, xmlPartContent);

            Assert.AreEqual(Encoding.ASCII.GetBytes(xmlPartContent), xmlPart.Data);
            Assert.AreEqual(xmlPartId, xmlPart.Id);

            // Below are two ways to refer to XML parts.
            // 1 -  By an index in the custom XML part collection:
            Assert.AreEqual(xmlPart, doc.CustomXmlParts[0]);

            // 2 -  By GUID:
            Assert.AreEqual(xmlPart, doc.CustomXmlParts.GetById(xmlPartId));

            // Add an XML schema association.
            xmlPart.Schemas.Add("http://www.w3.org/2001/XMLSchema");

            // Clone a part, and then insert it into the collection.
            CustomXmlPart xmlPartClone = xmlPart.Clone();

            xmlPartClone.Id = Guid.NewGuid().ToString("B");
            doc.CustomXmlParts.Add(xmlPartClone);

            Assert.AreEqual(2, doc.CustomXmlParts.Count);

            // Iterate through the collection and print the contents of each part.
            using (IEnumerator <CustomXmlPart> enumerator = doc.CustomXmlParts.GetEnumerator())
            {
                int index = 0;
                while (enumerator.MoveNext())
                {
                    Console.WriteLine($"XML part index {index}, ID: {enumerator.Current.Id}");
                    Console.WriteLine($"\tContent: {Encoding.UTF8.GetString(enumerator.Current.Data)}");
                    index++;
                }
            }

            // Use the "RemoveAt" method to remove the cloned part by index.
            doc.CustomXmlParts.RemoveAt(1);

            Assert.AreEqual(1, doc.CustomXmlParts.Count);

            // Clone the XML parts collection, and then use the "Clear" method to remove all its elements at once.
            CustomXmlPartCollection customXmlParts = doc.CustomXmlParts.Clone();

            customXmlParts.Clear();

            // Create a structured document tag that will display our part's contents and insert it into the document body.
            StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Block);

            tag.XmlMapping.SetMapping(xmlPart, "/root[1]/text[1]", string.Empty);

            doc.FirstSection.Body.AppendChild(tag);

            doc.Save(ArtifactsDir + "StructuredDocumentTag.CustomXml.docx");
            //ExEnd

            Assert.IsTrue(DocumentHelper.CompareDocs(ArtifactsDir + "StructuredDocumentTag.CustomXml.docx", GoldsDir + "StructuredDocumentTag.CustomXml Gold.docx"));

            doc     = new Document(ArtifactsDir + "StructuredDocumentTag.CustomXml.docx");
            xmlPart = doc.CustomXmlParts[0];

            Assert.True(Guid.TryParse(xmlPart.Id, out Guid temp));
            Assert.AreEqual("<root><text>Hello world!</text></root>", Encoding.UTF8.GetString(xmlPart.Data));
            Assert.AreEqual("http://www.w3.org/2001/XMLSchema", xmlPart.Schemas[0]);

            tag = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
            Assert.AreEqual("Hello world!", tag.GetText().Trim());
            Assert.AreEqual("/root[1]/text[1]", tag.XmlMapping.XPath);
            Assert.AreEqual(string.Empty, tag.XmlMapping.PrefixMappings);
        }
示例#8
0
        public void DropDownItemCollection()
        {
            //ExStart
            //ExFor:Fields.DropDownItemCollection
            //ExFor:Fields.DropDownItemCollection.Add(String)
            //ExFor:Fields.DropDownItemCollection.Clear
            //ExFor:Fields.DropDownItemCollection.Contains(String)
            //ExFor:Fields.DropDownItemCollection.Count
            //ExFor:Fields.DropDownItemCollection.GetEnumerator
            //ExFor:Fields.DropDownItemCollection.IndexOf(String)
            //ExFor:Fields.DropDownItemCollection.Insert(Int32, String)
            //ExFor:Fields.DropDownItemCollection.Item(Int32)
            //ExFor:Fields.DropDownItemCollection.Remove(String)
            //ExFor:Fields.DropDownItemCollection.RemoveAt(Int32)
            //ExSummary:Shows how to insert a combo box field, and edit the elements in its item collection.
            Document        doc     = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            // Insert a combo box, and then verify its collection of drop-down items.
            // In Microsoft Word, the user will click the combo box,
            // and then choose one of the items of text in the collection to display.
            string[]  items         = { "One", "Two", "Three" };
            FormField comboBoxField = builder.InsertComboBox("DropDown", items, 0);
            DropDownItemCollection dropDownItems = comboBoxField.DropDownItems;

            Assert.AreEqual(3, dropDownItems.Count);
            Assert.AreEqual("One", dropDownItems[0]);
            Assert.AreEqual(1, dropDownItems.IndexOf("Two"));
            Assert.IsTrue(dropDownItems.Contains("Three"));

            // There are two ways of adding a new item to an existing collection of drop-down box items.
            // 1 -  Append an item to the end of the collection:
            dropDownItems.Add("Four");

            // 2 -  Insert an item before another item at a specified index:
            dropDownItems.Insert(3, "Three and a half");

            Assert.AreEqual(5, dropDownItems.Count);

            // Iterate over the collection and print every element.
            using (IEnumerator <string> dropDownCollectionEnumerator = dropDownItems.GetEnumerator())
                while (dropDownCollectionEnumerator.MoveNext())
                {
                    Console.WriteLine(dropDownCollectionEnumerator.Current);
                }

            // There are two ways of removing elements from a collection of drop-down items.
            // 1 -  Remove an item with contents equal to the passed string:
            dropDownItems.Remove("Four");

            // 2 -  Remove an item at an index:
            dropDownItems.RemoveAt(3);

            Assert.AreEqual(3, dropDownItems.Count);
            Assert.IsFalse(dropDownItems.Contains("Three and a half"));
            Assert.IsFalse(dropDownItems.Contains("Four"));

            doc.Save(ArtifactsDir + "FormFields.DropDownItemCollection.html");

            // Empty the whole collection of drop-down items.
            dropDownItems.Clear();
            //ExEnd

            doc           = DocumentHelper.SaveOpen(doc);
            dropDownItems = doc.Range.FormFields[0].DropDownItems;

            Assert.AreEqual(0, dropDownItems.Count);

            doc           = new Document(ArtifactsDir + "FormFields.DropDownItemCollection.html");
            dropDownItems = doc.Range.FormFields[0].DropDownItems;

            Assert.AreEqual(3, dropDownItems.Count);
            Assert.AreEqual("One", dropDownItems[0]);
            Assert.AreEqual("Two", dropDownItems[1]);
            Assert.AreEqual("Three", dropDownItems[2]);
        }