public void PlainText()
        {
            //ExStart
            //ExFor:StructuredDocumentTag.Color
            //ExFor:StructuredDocumentTag.ContentsFont
            //ExFor:StructuredDocumentTag.EndCharacterFont
            //ExFor:StructuredDocumentTag.Id
            //ExFor:StructuredDocumentTag.Level
            //ExFor:StructuredDocumentTag.Multiline
            //ExFor:StructuredDocumentTag.Tag
            //ExFor:StructuredDocumentTag.Title
            //ExFor:StructuredDocumentTag.RemoveSelfOnly
            //ExSummary:Shows how to create a StructuredDocumentTag in the form of a plain text box and modify its appearance.
            // Create a new document
            Document doc = new Document();

            // Create a StructuredDocumentTag that will contain plain text
            StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Inline);

            // Set the title and color of the frame that appears when you mouse over it
            tag.Title = "My plain text";
            tag.Color = Color.Magenta;

            // Set a programmatic tag for this StructuredDocumentTag
            // Unlike the title, this value will not be visible in the document but will be programmatically obtainable
            // as an XML element named "tag", with the string below in its "@val" attribute
            tag.Tag = "MyPlainTextSDT";

            // Every StructuredDocumentTag gets a random unique ID
            Assert.That(tag.Id, Is.Positive);

            // Set the font for the text inside the StructuredDocumentTag
            tag.ContentsFont.Name = "Arial";

            // Set the font for the text at the end of the StructuredDocumentTag
            // Any text that's typed in the document body after moving out of the tag with arrow keys will keep this font
            tag.EndCharacterFont.Name = "Arial Black";

            // By default, this is false and pressing enter while inside a StructuredDocumentTag does nothing
            // When set to true, our StructuredDocumentTag can have multiple lines
            tag.Multiline = true;

            // Insert the StructuredDocumentTag into the document with a DocumentBuilder and save the document to a file
            DocumentBuilder builder = new DocumentBuilder(doc);

            builder.InsertNode(tag);

            // Insert a clone of our StructuredDocumentTag in a new paragraph
            StructuredDocumentTag tagClone = (StructuredDocumentTag)tag.Clone(true);

            builder.InsertParagraph();
            builder.InsertNode(tagClone);

            // We can remove the tag while keeping its contents where they were in the Paragraph by calling RemoveSelfOnly()
            tagClone.RemoveSelfOnly();

            doc.Save(ArtifactsDir + "SDT.PlainText.docx");
            //ExEnd
        }
        public void PlainText()
        {
            //ExStart
            //ExFor:StructuredDocumentTag.Color
            //ExFor:StructuredDocumentTag.ContentsFont
            //ExFor:StructuredDocumentTag.EndCharacterFont
            //ExFor:StructuredDocumentTag.Id
            //ExFor:StructuredDocumentTag.Level
            //ExFor:StructuredDocumentTag.Multiline
            //ExFor:StructuredDocumentTag.Tag
            //ExFor:StructuredDocumentTag.Title
            //ExFor:StructuredDocumentTag.RemoveSelfOnly
            //ExSummary:Shows how to create a structured document tag in a plain text box and modify its appearance.
            Document doc = new Document();

            // Create a structured document tag that will contain plain text.
            StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Inline);

            // Set the title and color of the frame that appears when you mouse over the structured document tag in Microsoft Word.
            tag.Title = "My plain text";
            tag.Color = Color.Magenta;

            // Set a tag for this structured document tag, which is obtainable
            // as an XML element named "tag", with the string below in its "@val" attribute.
            tag.Tag = "MyPlainTextSDT";

            // Every structured document tag has a random unique ID.
            Assert.That(tag.Id, Is.Positive);

            // Set the font for the text inside the structured document tag.
            tag.ContentsFont.Name = "Arial";

            // Set the font for the text at the end of the structured document tag.
            // Any text that we type in the document body after moving out of the tag with arrow keys will use this font.
            tag.EndCharacterFont.Name = "Arial Black";

            // By default, this is false and pressing enter while inside a structured document tag does nothing.
            // When set to true, our structured document tag can have multiple lines.

            // Set the "Multiline" property to "false" to only allow the contents
            // of this structured document tag to span a single line.
            // Set the "Multiline" property to "true" to allow the tag to contain multiple lines of content.
            tag.Multiline = true;

            DocumentBuilder builder = new DocumentBuilder(doc);

            builder.InsertNode(tag);

            // Insert a clone of our structured document tag in a new paragraph.
            StructuredDocumentTag tagClone = (StructuredDocumentTag)tag.Clone(true);

            builder.InsertParagraph();
            builder.InsertNode(tagClone);

            // Use the "RemoveSelfOnly" method to remove a structured document tag, while keeping its contents in the document.
            tagClone.RemoveSelfOnly();

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

            doc = new Document(ArtifactsDir + "StructuredDocumentTag.PlainText.docx");
            tag = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);

            Assert.AreEqual("My plain text", tag.Title);
            Assert.AreEqual(Color.Magenta.ToArgb(), tag.Color.ToArgb());
            Assert.AreEqual("MyPlainTextSDT", tag.Tag);
            Assert.That(tag.Id, Is.Positive);
            Assert.AreEqual("Arial", tag.ContentsFont.Name);
            Assert.AreEqual("Arial Black", tag.EndCharacterFont.Name);
            Assert.True(tag.Multiline);
        }
Ejemplo n.º 3
0
        //public List<string> GetDocumentTags(byte[] buffer)
        //{
        //    if (buffer == null) throw new ArgumentNullException(nameof(buffer));

        //    Document document;
        //    using (var ms = new MemoryStream(buffer))
        //    {
        //        document = new Document(ms);
        //    }

        //    return document
        //        .GetChildNodes(NodeType.StructuredDocumentTag, true)
        //        .ToArray()
        //        .Select(x => ((StructuredDocumentTag)x).Title)
        //        .ToList();
        //}

        private void FillUserInputControl(StructuredDocumentTag std,
                                          Dictionary <string, string> userInputFieldsDictionaty, Document document,
                                          DocumentBuilder builder, string defaultValue)
        {
            //std.RemoveAllChildren();

            Paragraph paragraph;
            var       html = userInputFieldsDictionaty[std.Title.Trim()];

            if (string.IsNullOrEmpty(html))
            {
                return;
            }
            html = TrimParagraphTag(html);
            html = ResizeBase64Images(html);

            //была проблема при вставке html если тег находился в параграфе
            //если Content Control находится не в Body, а во вложенном теге
            if (std.ParentNode.NodeType != NodeType.Body)
            {
                //проверяем текущее поле на простой текст без форматирования
                if (std.Title.Trim().EndsWith("_UserInput"))
                {
                    if (std.ParentNode.NodeType == NodeType.Paragraph)
                    {
                        var run = new Run(document, html);

                        // set font
                        Inline inlineElement = GetFirstInline(std.ChildNodes);
                        if (inlineElement != null)
                        {
                            CopyFont(run.Font, inlineElement.Font);
                        }

                        std.RemoveAllChildren();
                        std.AppendChild(run);
                        std.RemoveSelfOnly();
                    }
                    else
                    {
                        paragraph = (Paragraph)std.ParentNode.InsertAfter(new Paragraph(document), std);

                        // set font
                        Paragraph paragraphSource = (Paragraph)std.FirstChild;
                        Inline    inlineElement   = GetFirstInline(paragraphSource.ChildNodes);
                        var       run             = new Run(document, html);
                        if (inlineElement != null)
                        {
                            CopyFont(run.Font, inlineElement.Font);
                        }

                        paragraph.AppendChild(run);
                        std.Remove();
                    }
                }
                //если поле со значение по умолчанию
                else if (std.Title.Trim().EndsWith("_UserInputDefault"))
                {
                    html = html.Equals(string.Empty) ? defaultValue : html;
                    if (std.ParentNode.NodeType == NodeType.Paragraph)
                    {
                        var run = new Run(document, html);

                        // set font
                        Inline inlineElement = GetFirstInline(std.ChildNodes);
                        if (inlineElement != null)
                        {
                            CopyFont(run.Font, inlineElement.Font);
                        }

                        std.RemoveAllChildren();
                        std.AppendChild(run);
                        std.RemoveSelfOnly();
                    }
                    else
                    {
                        paragraph = (Paragraph)std.ParentNode.InsertAfter(new Paragraph(document), std);

                        // set font
                        Paragraph paragraphSource = (Paragraph)std.FirstChild;
                        Inline    inlineElement   = GetFirstInline(paragraphSource.ChildNodes);
                        var       run             = new Run(document, html);
                        if (inlineElement != null)
                        {
                            CopyFont(run.Font, inlineElement.Font);
                        }

                        paragraph.AppendChild(run);
                        std.Remove();
                    }
                }
                //если текст из wysiwyg редактора
                else
                {
                    std.RemoveAllChildren();

                    if (std.ParentNode.NodeType == NodeType.Paragraph)
                    {
                        builder.MoveTo(std);
                        builder.InsertHtml(html, true);
                        std.RemoveSelfOnly();
                    }
                    else
                    {
                        paragraph = (Paragraph)std.ParentNode.InsertAfter(new Paragraph(document), std);
                        builder.MoveTo(paragraph);
                        builder.InsertHtml(html, true);

                        std.AppendChild(paragraph);
                        std.RemoveSelfOnly();
                    }
                }
            }
            //если Content Control находится в корне тега Body
            else
            {
                if (std.Title.Trim().EndsWith("_UserInput"))
                {
                    paragraph = (Paragraph)std.ParentNode.InsertAfter(new Paragraph(document), std);

                    // set font
                    Paragraph paragraphSource = (Paragraph)std.FirstChild;
                    Inline    inlineElement   = GetFirstInline(paragraphSource.ChildNodes);
                    var       run             = new Run(document, html);
                    if (inlineElement != null)
                    {
                        CopyFont(run.Font, inlineElement.Font);
                    }

                    paragraph.AppendChild(run);
                    std.Remove();
                }
                else if (std.Title.Trim().EndsWith("_UserInputDefault"))
                {
                    html      = html.Equals(string.Empty) ? defaultValue : html;
                    paragraph = (Paragraph)std.ParentNode.InsertAfter(new Paragraph(document), std);

                    // set font
                    Paragraph paragraphSource = (Paragraph)std.FirstChild;
                    Inline    inlineElement   = GetFirstInline(paragraphSource.ChildNodes);
                    var       run             = new Run(document, html);
                    if (inlineElement != null)
                    {
                        CopyFont(run.Font, inlineElement.Font);
                    }

                    paragraph.AppendChild(run);
                    std.Remove();
                }
                //если текст из wysiwyg редактора
                else
                {
                    paragraph = (Paragraph)std.ParentNode.InsertAfter(new Paragraph(document), std);

                    builder.MoveTo(paragraph);
                    builder.InsertHtml(html, true);
                    std.Remove();
                }
            }
        }