public void TestMixedPlainAndFormattedText(string formattedText, string plainText)
        {
            Paragraph paragraph = document.LastSection.AddParagraph();

            paragraph.AddFormattedText(formattedText);
            paragraph.AddText(plainText);
            Assert.AreEqual($"{formattedText}{plainText}", paragraph.GetRawText());
        }
Exemple #2
0
        /// <summary>
        /// Get the plain text contents of the paragraph.
        /// </summary>
        /// <remarks>
        /// This will unwrap the FormattedText elements as needed.
        /// </remarks>
        /// <param name="paragraph">The paragraph.</param>
        internal static string GetRawText(this Paragraph paragraph)
        {
            if (paragraph == null)
            {
                throw new ArgumentNullException(nameof(paragraph));
            }

            return(string.Join("", paragraph.Elements.GetTextElements()));
        }
Exemple #3
0
        /// <summary>
        /// Return true iff the paragraph is empty.
        /// </summary>
        /// <param name="paragraph">The paragraph.</param>
        internal static bool IsEmpty(this Paragraph paragraph)
        {
            if (paragraph == null)
            {
                throw new ArgumentNullException(nameof(paragraph));
            }

            if (paragraph.Elements.OfType <Image>().Any())
            {
                return(false);
            }
            IEnumerable <string> textElements = paragraph.Elements.GetTextElements();

            return(textElements.All(string.IsNullOrEmpty));
        }
        public void TestParagraphWithLinebreaks()
        {
            Paragraph paragraph = document.LastSection.AddParagraph();
            string    text0     = "before line break";
            string    text1     = "\n";
            string    text2     = "afterlinebreak";

            paragraph.AddText(text0);
            paragraph.AddText(text1);
            paragraph.AddText(text2);

            string expected = $"{text0}{text1}{text2}";

            Assert.AreEqual(expected, paragraph.GetRawText());
        }
        public void TestGetHyperlinkTextElements()
        {
            string    linkName      = "hyperlinkName";
            string    linkText      = "link text";
            string    linkFormatted = "link formatted text";
            Paragraph paragraph     = document.LastSection.AddParagraph();
            Hyperlink link          = paragraph.AddHyperlink(linkName);

            link.AddText(linkText);
            link.AddFormattedText(linkFormatted);
            List <string> textElements = paragraph.Elements.GetTextElements().ToList();

            Assert.AreEqual(2, textElements.Count);
            Assert.AreEqual(linkText, textElements[0]);
            Assert.AreEqual(linkFormatted, textElements[1]);
        }
        public void TestGetTextElementsOrder()
        {
            Paragraph paragraph = document.LastSection.AddParagraph();
            string    text0     = "formatted text 0";
            string    text1     = "plaintext 1";
            string    text2     = "formatted text 2";

            paragraph.AddFormattedText(text0);
            paragraph.AddText(text1);
            paragraph.AddFormattedText(text2);

            string[] elements = paragraph.Elements.GetTextElements().ToArray();
            Assert.AreEqual(text0, elements[0]);
            Assert.AreEqual(text1, elements[1]);
            Assert.AreEqual(text2, elements[2]);
        }
        public void TestIsEmpty(bool empty, bool formattedText, params string[] elements)
        {
            Paragraph paragraph = document.LastSection.AddParagraph();

            foreach (string text in elements)
            {
                if (formattedText)
                {
                    paragraph.AddFormattedText(text);
                }
                else
                {
                    paragraph.AddText(text);
                }
            }
            Assert.AreEqual(empty, paragraph.IsEmpty());
        }
Exemple #8
0
        public void TestInsertWithoutMarkdown()
        {
            // Add a paragraph with some italicised text.
            string rawText = "raw text";

            renderer.Render(new Paragraph(rawText), pdfBuilder);

            // Ensure that the paragraph was inserted.
            Assert.AreEqual(1, document.LastSection.Elements.Count);
            MigraDocParagraph paragraph = document.LastSection.Elements[0] as MigraDocParagraph;

            Assert.NotNull(paragraph);
            Assert.AreEqual(1, paragraph.Elements.Count);
            FormattedText insertedText = paragraph.Elements[0] as FormattedText;

            Assert.NotNull(insertedText);

            // Ensure that the inserted raw text matches and is italic.
            Assert.AreEqual(rawText, ((Text)insertedText.Elements[0]).Content);
        }
Exemple #9
0
        public void EnsureMarkdownInParagraphIsParsed()
        {
            // Add a paragraph with some italicised text.
            string rawText = "italic text";
            string italics = $"*{rawText}*";

            renderer.Render(new Paragraph(italics), pdfBuilder);

            // Ensure that the paragraph was inserted.
            Assert.AreEqual(1, document.LastSection.Elements.Count);
            MigraDocParagraph paragraph = document.LastSection.Elements[0] as MigraDocParagraph;

            Assert.NotNull(paragraph);
            Assert.AreEqual(1, paragraph.Elements.Count);
            FormattedText insertedText = paragraph.Elements[0] as FormattedText;

            Assert.NotNull(insertedText);

            // Ensure that the inserted raw text matches and is italic.
            Assert.AreEqual(rawText, ((Text)insertedText.Elements[0]).Content);
            Assert.True(document.Styles[insertedText.Style].Font.Italic);
        }
        public void TestGetTextElements(bool formatted, params string[] elements)
        {
            Paragraph paragraph = document.LastSection.AddParagraph();

            foreach (string text in elements)
            {
                if (formatted)
                {
                    paragraph.AddFormattedText(text);
                }
                else
                {
                    paragraph.AddText(text);
                }
            }
            List <string> textElements = paragraph.Elements.GetTextElements().ToList();

            Assert.AreEqual(elements.Length, textElements.Count);
            for (int i = 0; i < elements.Length; i++)
            {
                Assert.AreEqual(elements[i], textElements[i]);
            }
        }
Exemple #11
0
 /// <summary>
 /// Adds a new paragraph to the footnote.
 /// </summary>
 public void Add(Paragraph paragraph)
 {
     this.Elements.Add(paragraph);
 }