예제 #1
0
        /// <summary>
        /// Ensure that the given paragraph contains only a heading, with
        /// the specified heading indices and text. This does not verify
        /// the heading text's style (bold/font size/heading level/etc).
        /// </summary>
        /// <param name="paragraph">The paragraph. An appropriate error will be given if this is null.</param>
        /// <param name="expectedIndices">The expected heading indices. E.g. "1.3.2 "</param>
        /// <param name="expectedHeadingText">The actual heading text not including the indices.</param>
        private void ValidateHeading(Paragraph paragraph, string expectedIndices, string expectedHeadingText)
        {
            Assert.NotNull(paragraph, "Heading was not written to a paragraph object");

            Assert.AreEqual(3, paragraph.Elements.Count);
            FormattedText indices  = paragraph.Elements[0] as FormattedText;
            FormattedText heading  = paragraph.Elements[1] as FormattedText;
            BookmarkField bookmark = paragraph.Elements[2] as BookmarkField;

            Assert.NotNull(indices, "Heading indices were not written to document");
            Assert.NotNull(heading, "Heading text was not written to document");
            Assert.NotNull(bookmark, "Heading text was not written as a bookmark");

            Assert.AreEqual(1, indices.Elements.Count, "Heading indices should be a single text element");
            Assert.AreEqual(1, heading.Elements.Count, "Heading text should be a single text element");

            Text indicesText = indices.Elements.LastObject as Text;
            Text headingText = heading.Elements.LastObject as Text;

            Assert.NotNull(indices, "Heading indices were not written");
            Assert.NotNull(heading, "Heading text was not written");

            Assert.AreEqual(expectedIndices, indicesText.Content, "Heading index is incorrect");
            Assert.AreEqual(expectedHeadingText, headingText.Content, "Heading text is incorrect");
            Assert.AreEqual($"#{expectedHeadingText}", bookmark.Name);
        }
예제 #2
0
        public void CheckFormatting()
        {
            renderer.Write(pdfBuilder, block);
            Paragraph     paragraph = (Paragraph)document.LastSection.Elements[0];
            FormattedText text      = (FormattedText)paragraph.Elements[0];

            // todo: need to check if this is really the best way.
            Assert.AreEqual("courier", document.Styles[text.Style].Font.Name);
        }
예제 #3
0
        public void EnsureChildrenIsWritten()
        {
            string text = "heading text";

            renderer.Write(pdfBuilder, CreateHeading(text));
            Assert.AreEqual(1, document.LastSection.Elements.Count);
            Paragraph paragraph = (Paragraph)document.LastSection.Elements[0];

            Assert.AreEqual($"1 {text}", paragraph.GetRawText());
        }
예제 #4
0
        public void TestSimpleHeading()
        {
            builder.AppendHeading("hello");
            Assert.AreEqual(1, doc.Sections.Count);
            Assert.AreEqual(1, doc.LastSection.Elements.Count);

            Paragraph paragraph = doc.LastSection.Elements.LastObject as Paragraph;

            ValidateHeading(paragraph, "1 ", "hello");
        }
예제 #5
0
        public void EnsureChildrenAreWritten()
        {
            string    text  = "contents";
            CodeBlock block = CreateCodeBlock(text);

            renderer.Write(pdfBuilder, block);
            Assert.AreEqual(1, document.LastSection.Elements.Count);
            Paragraph paragraph = (Paragraph)document.LastSection.Elements[0];

            Assert.AreEqual(text, paragraph.GetRawText());
        }
예제 #6
0
        public void EnsureAllGraphsAreWritten(int numGraphs)
        {
            List <IGraph>         graphs       = new List <IGraph>(numGraphs);
            Mock <IGraphExporter> mockExporter = new Mock <IGraphExporter>();
            List <Image>          images       = new List <Image>();

            for (int i = 0; i < numGraphs; i++)
            {
                // This is a little tricky because I want to have each graph
                // generate a unique image, so we need to mock out the graph,
                // the intermediary plot model, and the graph exporter as well.
                Mock <IGraph>     mockGraph  = new Mock <IGraph>();
                IGraph            graph      = mockGraph.Object;
                Image             graphImage = CreateImage(i + 1);
                Mock <IPlotModel> mockModel  = new Mock <IPlotModel>();
                IPlotModel        graphModel = mockModel.Object;
                mockExporter.Setup <IPlotModel>(e => e.ToPlotModel(graph)).Returns(() => graphModel);
                mockExporter.Setup <Image>(e => e.Export(graphModel, It.IsAny <double>(), It.IsAny <double>())).Returns(() => graphImage);
                graphs.Add(graph);
                images.Add(graphImage);
            }
            GraphPage page = new GraphPage(graphs);

            renderer = new GraphPageTagRenderer(mockExporter.Object);
            renderer.Render(page, pdfBuilder);

            if (numGraphs < 1)
            {
                // No child graphs - document should be empty.
                Assert.AreEqual(0, document.LastSection.Elements.Count);
            }
            else
            {
                // There should be a single paragraph, containing all graphs.
                Assert.AreEqual(1, document.LastSection.Elements.Count);
                Paragraph paragraph = document.LastSection.Elements[0] as Paragraph;
                Assert.NotNull(paragraph);

                // The paragraph should contain n images.
                Assert.AreEqual(numGraphs, paragraph.Elements.Count);

                // Ensure that all images have been renderered correctly.
                for (int i = 0; i < numGraphs; i++)
                {
                    MigraDocImage actual = paragraph.Elements[i] as MigraDocImage;
                    AssertEqual(images[i], actual);
                    images[i].Dispose();
                }
                images.Clear();
            }
        }
예제 #7
0
        public void EnsureHeadingStyleNotAppliedToSubsequentInsertions()
        {
            renderer.Write(pdfBuilder, CreateHeading("sample heading"));
            pdfBuilder.AppendText("a new paragraph", TextStyle.Normal);

            Paragraph     headingParagraph = (Paragraph)document.LastSection.Elements[0];
            FormattedText headingText      = (FormattedText)headingParagraph.Elements[0];
            double        headingTextSize  = document.Styles[headingText.Style].Font.Size.Point;

            Paragraph     plainParagraph = (Paragraph)document.LastSection.Elements[1];
            FormattedText plainText      = (FormattedText)plainParagraph.Elements[0];
            double        plainTextSize  = document.Styles[plainText.Style].Font.Size.Point;

            Assert.Greater(headingTextSize, plainTextSize);
        }
예제 #8
0
        public void EnsureImageIsAddedToNewParagraph()
        {
            // Write a non-empty paragraph to the document.
            document.LastSection.AddParagraph("paragraph text");

            // Write the image.
            renderer.Render(imageTag, pdfBuilder);

            // Ensure that the image was not written to the paragraph.
            Assert.AreEqual(2, document.LastSection.Elements.Count);
            Paragraph     paragraph = (Paragraph)document.LastSection.Elements[1];
            MigraDocImage actual    = paragraph.Elements[0] as MigraDocImage;

            AssertEqual(image, actual);
        }
예제 #9
0
        public void TestSingleNestedHeading()
        {
            builder.AppendHeading("top heading");

            builder.PushSubHeading();
            builder.AppendHeading("nested heading");
            builder.PopSubHeading();

            Assert.AreEqual(2, doc.LastSection.Elements.Count, "Section should have two paragraphs");
            Paragraph top    = doc.LastSection.Elements[0] as Paragraph;
            Paragraph nested = doc.LastSection.Elements[1] as Paragraph;

            ValidateHeading(top, "1 ", "top heading");
            ValidateHeading(nested, "1.1 ", "nested heading");
        }
예제 #10
0
        public void TestOutlineLevelRelativeHeadings()
        {
            builder.AppendHeading("Toplevel heading");

            builder.PushSubHeading();
            builder.SetHeadingLevel(1);
            builder.AppendText("Nested heading", TextStyle.Normal);
            builder.ClearHeadingLevel();
            builder.PopSubHeading();

            Assert.AreEqual(2, doc.LastSection.Elements.Count, "Section has incorrect # paragraphs");
            Paragraph paragraph0 = (Paragraph)doc.LastSection.Elements[0];
            Paragraph paragraph1 = (Paragraph)doc.LastSection.Elements[1];

            Assert.AreEqual(OutlineLevel.Level1, paragraph0.Format.OutlineLevel);
            Assert.AreEqual(OutlineLevel.Level2, paragraph1.Format.OutlineLevel);
        }
예제 #11
0
        public void CheckFormattingOfSubsequentContent()
        {
            // Write a code block, and then some plain text.
            renderer.Write(pdfBuilder, block);
            pdfBuilder.AppendText("a new paragraph after the code block", TextStyle.Normal);

            // Ensure that the contents of the two paragraphs have different fonts.
            Paragraph     codeParagraph  = (Paragraph)document.LastSection.Elements[0];
            FormattedText codeText       = (FormattedText)codeParagraph.Elements[0];
            Paragraph     plainParagraph = (Paragraph)document.LastSection.Elements[1];
            FormattedText plainText      = (FormattedText)plainParagraph.Elements[0];

            string codeFont  = document.Styles[codeText.Style].Font.Name;
            string plainFont = document.Styles[plainText.Style].Font.Name;

            Assert.AreNotEqual(codeFont, plainFont);
        }
예제 #12
0
        public void EnsureSubsequentContentGoesToNewParagraph()
        {
            // Render the graph page.
            GraphPage page = new GraphPage(new[] { graph });

            renderer.Render(page, pdfBuilder);

            // Create a paragraph with some text.
            pdfBuilder.AppendText("paragraph content", TextStyle.Normal);

            // There should be two paragraphs.
            Assert.AreEqual(2, document.LastSection.Elements.Count);

            // Let's also double check that the image was added correctly.
            Paragraph     graphsParagraph = (Paragraph)document.LastSection.Elements[0];
            MigraDocImage actual          = (MigraDocImage)graphsParagraph.Elements[0];

            AssertEqual(image, actual);
        }
예제 #13
0
        public void EnsureGraphsAreWrittenToNewParagraph()
        {
            // Create a paragraph with some text.
            document.LastSection.AddParagraph("paragraph content");

            // Render the graph page - should not go into previous paragraph.
            GraphPage page = new GraphPage(new[] { graph });

            renderer.Render(page, pdfBuilder);

            // There should be two paragraphs.
            Assert.AreEqual(2, document.LastSection.Elements.Count);

            // Let's also double check that the image was added correctly.
            Paragraph     graphsParagraph = (Paragraph)document.LastSection.Elements[1];
            MigraDocImage actual          = (MigraDocImage)graphsParagraph.Elements[0];

            AssertEqual(image, actual);
        }
예제 #14
0
        public void EnsureHeadingLevelIsRespected()
        {
            renderer.Write(pdfBuilder, CreateHeading("heading level 1", 1));
            renderer.Write(pdfBuilder, CreateHeading("heading level 2", 2));

            Assert.AreEqual(2, document.LastSection.Elements.Count);

            Paragraph paragraph0 = (Paragraph)document.LastSection.Elements[0];
            Paragraph paragraph1 = (Paragraph)document.LastSection.Elements[1];

            FormattedText text0 = (FormattedText)paragraph0.Elements[0];
            FormattedText text1 = (FormattedText)paragraph1.Elements[0];

            double fontSize0 = document.Styles[text0.Style].Font.Size.Point;
            double fontSize1 = document.Styles[text1.Style].Font.Size.Point;

            // heading0 is heading level 1, so should haved larger font size than
            // heading1, which is a level 2 heading.
            Assert.Greater(fontSize0, fontSize1);
        }
예제 #15
0
        public void TestMultipleNestedHeadings()
        {
            builder.AppendHeading("toplevel heading");

            builder.PushSubHeading();
            builder.AppendHeading("middle heading");
            builder.PushSubHeading();
            builder.AppendHeading("lowest level heading");
            builder.PopSubHeading();
            builder.PopSubHeading();

            Assert.AreEqual(3, doc.LastSection.Elements.Count, "Section should have three paragraphs");
            Paragraph top    = doc.LastSection.Elements[0] as Paragraph;
            Paragraph middle = doc.LastSection.Elements[1] as Paragraph;
            Paragraph lowest = doc.LastSection.Elements[2] as Paragraph;

            ValidateHeading(top, "1 ", "toplevel heading");
            ValidateHeading(middle, "1.1 ", "middle heading");
            ValidateHeading(lowest, "1.1.1 ", "lowest level heading");
        }
예제 #16
0
        public void TestOutlineLevelNormalHeading()
        {
            builder.AppendHeading("Heading level 1");
            Paragraph paragraph = (Paragraph)doc.LastSection.Elements.LastObject;

            Assert.AreEqual(OutlineLevel.Level1, paragraph.Format.OutlineLevel);

            builder.PushSubHeading();

            builder.AppendHeading("Heading level 2");
            paragraph = (Paragraph)doc.LastSection.Elements.LastObject;
            Assert.AreEqual(OutlineLevel.Level2, paragraph.Format.OutlineLevel);

            builder.PushSubHeading();

            builder.AppendHeading("Heading level 3");
            paragraph = (Paragraph)doc.LastSection.Elements.LastObject;
            Assert.AreEqual(OutlineLevel.Level3, paragraph.Format.OutlineLevel);

            builder.PushSubHeading();

            builder.AppendHeading("Heading level 4");
            paragraph = (Paragraph)doc.LastSection.Elements.LastObject;
            Assert.AreEqual(OutlineLevel.Level4, paragraph.Format.OutlineLevel);

            builder.PushSubHeading();

            builder.AppendHeading("Heading level 5");
            paragraph = (Paragraph)doc.LastSection.Elements.LastObject;
            Assert.AreEqual(OutlineLevel.Level5, paragraph.Format.OutlineLevel);

            builder.PushSubHeading();

            builder.AppendHeading("Heading level 6");
            paragraph = (Paragraph)doc.LastSection.Elements.LastObject;
            Assert.AreEqual(OutlineLevel.Level6, paragraph.Format.OutlineLevel);
        }