Пример #1
0
        public void EnsureImageIsAdded()
        {
            renderer.Render(imageTag, pdfBuilder);

            List <Paragraph> paragraphs = document.LastSection.Elements.OfType <Paragraph>().ToList();

            Assert.AreEqual(1, paragraphs.Count);
            MigraDocImage actual = paragraphs[0].Elements.OfType <MigraDocImage>().First();

            AssertEqual(image, actual);
        }
Пример #2
0
 public void TestAppendImage()
 {
     using (Image image = new Bitmap(2, 2))
     {
         builder.AppendImage(image);
         Assert.AreEqual(1, doc.LastSection.Elements.Count);
         Paragraph paragraph = (Paragraph)doc.LastSection.Elements[0];
         Assert.AreEqual(1, paragraph.Elements.Count);
         MigraDocImage inserted = (MigraDocImage)paragraph.Elements[0];
         Assert.AreEqual(image.Width, inserted.Source.Width);
         Assert.AreEqual(image.Height, inserted.Source.Height);
     }
 }
Пример #3
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();
            }
        }
Пример #4
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);
        }
Пример #5
0
        /// <summary>
        /// Ensure that a System.Drawing.Image and a MigraDoc image are equivalent.
        /// </summary>
        /// <param name="expected">Expected image.</param>
        /// <param name="actual">Actual image.</param>
        private void AssertEqual(Image expected, MigraDocImage actual)
        {
            if (expected == null)
            {
                Assert.Null(actual);
            }
            else
            {
                Assert.NotNull(actual);
            }

            // Note: actual.Width is not the actual width (that would be too easy);
            // instead, it represents a custom user-settable width. We're more
            // interested in the width of the underlying image.
            Assert.AreEqual(expected.Width, actual.Source.Width);
            Assert.AreEqual(expected.Height, actual.Source.Height);
        }
Пример #6
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);
        }
Пример #7
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);
        }
Пример #8
0
        public void TestAppendImageSizing(double pageWidth, double pageHeight, int imageWidth, int imageHeight, int expectedWidth, int expectedHeight)
        {
            Section section = doc.AddSection();

            _ = section.Elements;
            const double pointsToPixels = 96.0 / 72;

            // When setting the page width and height, convert from px to points.
            section.PageSetup.PageWidth  = Unit.FromPoint(pageWidth / pointsToPixels);
            section.PageSetup.PageHeight = Unit.FromPoint(pageHeight / pointsToPixels);
            using (Image image = new Bitmap(imageWidth, imageHeight))
            {
                builder.AppendImage(image);
                Paragraph     paragraph = (Paragraph)doc.LastSection.Elements[0];
                MigraDocImage inserted  = (MigraDocImage)paragraph.Elements[0];

                // The image we inserted is 20x20. However, the page size is
                // 10x10. The image would have been resized to fit this.
                Assert.AreEqual(expectedWidth, inserted.Source.Width);
                Assert.AreEqual(expectedHeight, inserted.Source.Height);
            }
        }