Example #1
0
        public void EnsureListGoesIntoNewParagraph()
        {
            // Setup the document with an existing paragraph.
            pdfBuilder.AppendText("existing p/graph", TextStyle.Normal);

            // Now write the list block.
            renderer.Write(pdfBuilder, sampleBlock);

            // Document should contain 2 paragraphs.
            Assert.AreEqual(2, document.LastSection.Elements.Count);
        }
        public void EnsureHeadingGoesIntoNewParagraph()
        {
            // Append text to the document - this will cause a new paragraph to be created.
            pdfBuilder.AppendText("some paragraph", TextStyle.Normal);

            // Write the heading block.
            renderer.Write(pdfBuilder, CreateHeading("contents"));

            // There should be two paragraphs in the document.
            Assert.AreEqual(2, document.LastSection.Elements.Count);
        }
Example #3
0
        public void TestAppendValidReferenceCorrectParagraph()
        {
            builder.AppendText("some text", TextStyle.Normal);
            builder.StartNewParagraph();

            string reference = "a reference";

            AddCitation(reference, "in-text citation");

            builder.AppendReference(reference, TextStyle.Normal);
            Assert.AreEqual(2, doc.LastSection.Elements.Count);
            Assert.AreEqual(typeof(Paragraph), doc.LastSection.Elements[0].GetType());
            Assert.AreEqual(typeof(Paragraph), doc.LastSection.Elements[1].GetType());
        }
Example #4
0
        public void TestLinkStyle()
        {
            builder.SetLinkState("link uri");
            builder.AppendText("link text", TextStyle.Normal);
            builder.ClearLinkState();

            Paragraph     paragraph = (Paragraph)doc.LastSection.Elements[0];
            Hyperlink     link      = (Hyperlink)paragraph.Elements[0];
            FormattedText formatted = (FormattedText)link.Elements[0];
            Style         style     = doc.Styles[formatted.Style];

            // Links should not have the "Normal" font colour - they should be blue.
            // I'm not going to test the exact shade. As long as it's not the
            // default font colour then we're good.
            Assert.AreNotEqual(doc.Styles.Normal.Font.Color, style.Font.Color);
        }
Example #5
0
        public static Document CreateStandardDocument(bool vertical = true)
        {
            Document document = new Document();
            var      section  = document.AddSection();

            PageSetup pageSetup = document.DefaultPageSetup.Clone();

            pageSetup.LeftMargin   = Unit.FromCentimeter(1);
            pageSetup.RightMargin  = Unit.FromCentimeter(1);
            pageSetup.TopMargin    = Unit.FromCentimeter(1);
            pageSetup.BottomMargin = Unit.FromCentimeter(1);
            pageSetup.Orientation  = vertical ? Orientation.Portrait : Orientation.Landscape;
            section.PageSetup      = pageSetup;

            document.Styles.Normal.ParagraphFormat.SpaceAfter = Unit.FromPoint(10);

            section.AddParagraph();

            PdfBuilder builder = new PdfBuilder(document, PdfOptions.Default);

            builder.AppendImage(Image.LoadFromResource("AIBanner.png"));
            document.LastSection.AddParagraph();
            builder.AppendText($"Document generated with APSIM v{Models.Core.Simulations.ApsimVersion}", Markdown.TextStyle.Normal);
            document.LastSection.AddParagraph();

            return(document);
        }
Example #6
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);
        }
Example #7
0
        /// <summary>
        /// Render the given LineBreakInline object to the PDF document.
        /// </summary>
        /// <param name="renderer">The PDF renderer.</param>
        /// <param name="obj">The LineBreakInline object to be renderered.</param>
        protected override void Write(PdfBuilder renderer, LineBreakInline obj)
        {
            // Html version of this class has an option to render soft line breaks
            // as a hard line break. We could implement something similar.
            string textToInsert = obj.IsHard ? Environment.NewLine : " ";

            renderer.AppendText(textToInsert, TextStyle.Normal);
        }
Example #8
0
        /// <summary>
        /// Render the given autolink inline object to the PDF document.
        /// </summary>
        /// <param name="renderer">The PDF renderer.</param>
        /// <param name="obj">The autolink inline object to be renderered.</param>
        protected override void Write(PdfBuilder renderer, AutolinkInline obj)
        {
            string prefix = obj.IsEmail ? "mailto:" : "";
            string uri    = $"{prefix}{obj.Url}";

            renderer.SetLinkState(uri);
            renderer.AppendText(uri, TextStyle.Normal);
            renderer.ClearLinkState();
        }
Example #9
0
        /// <summary>
        /// Write the lines of a leaf block to a PDF document.
        /// </summary>
        /// <param name="leaf">The leaf block to be written.</param>
        internal static void WriteLeafInlines(this PdfBuilder builder, LeafBlock leaf)
        {
            if (leaf == null)
            {
                // Throwing here may be a bit harsh(?).
                throw new NullReferenceException("Unable to write null leaf block");
            }
            if (leaf.Lines.Lines == null)
            {
                return;
            }

            for (int i = 0; i < leaf.Lines.Count; i++)
            {
                builder.AppendText(leaf.Lines.Lines[i].Slice.ToString(), TextStyle.Normal);
                if (i < leaf.Lines.Count - 1)
                {
                    builder.AppendText(Environment.NewLine, TextStyle.Normal);
                }
            }
        }
Example #10
0
        /// <summary>
        /// Render the given LinkInline object to the PDF document.
        /// </summary>
        /// <param name="renderer">The PDF renderer.</param>
        /// <param name="link">The link object to be renderered.</param>
        protected override void Write(PdfBuilder renderer, LinkInline link)
        {
            string uri = link.GetDynamicUrl != null?link.GetDynamicUrl() ?? link.Url : link.Url;

            if (link.IsImage)
            {
                Image image = GetImage(uri);
                // Technically, the image should be written to the same paragraph as any existing content.
                // However, if the image is too large, I'm going to add it to its own paragraph.
                // I'm defining "too large" as "taller than page height * 0.9".
                renderer.GetPageSize(out _, out double height);
                if (image.Height > 0.9 * height)
                {
                    renderer.StartNewParagraph();
                }

                renderer.AppendImage(image);

                // The assumption here is that any children of the image are the image's caption.
                renderer.StartNewParagraph();

                // Increment heading count, iff the link has a caption.
                if (link.Any())
                {
                    renderer.IncrementFigureNumber();
                    renderer.AppendText($"Figure {renderer.FigureNumber}: ", TextStyle.Strong);
                    renderer.WriteChildren(link);
                    renderer.StartNewParagraph();
                }
            }
            else
            {
                // Clunky bookmark detection. This could be improved.
                if (uri.StartsWith("#"))
                {
                    renderer.StartBookmark(uri);
                }
                else
                {
                    renderer.SetLinkState(uri);
                }
                renderer.WriteChildren(link);
                renderer.ClearLinkState();
            }
        }
Example #11
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);
        }
Example #12
0
 public void EnsureSubsequentAdditionsInSameParagraph()
 {
     renderer.Write(pdfBuilder, inline);
     pdfBuilder.AppendText("subsequent addition", TextStyle.Normal);
     Assert.AreEqual(1, document.LastSection.Elements.Count);
 }
Example #13
0
        public void TestSetLinkState()
        {
            string linkUri  = "linkuri";
            string linkText = "this is a hyperlink";

            builder.SetLinkState(linkUri);
            builder.AppendText(linkText, TextStyle.Normal);
            Assert.AreEqual(1, doc.LastSection.Elements.Count);
            Paragraph paragraph = (Paragraph)doc.LastSection.Elements[0];

            Assert.AreEqual(1, paragraph.Elements.Count);
            Hyperlink link = (Hyperlink)paragraph.Elements[0];

            Assert.AreEqual(linkUri, link.Name);
            Assert.AreEqual(1, link.Elements.Count);
            FormattedText text = (FormattedText)link.Elements[0];

            Assert.AreEqual(1, text.Elements.Count);
            Text rawText = (Text)text.Elements[0];

            Assert.AreEqual(linkText, rawText.Content);
        }
Example #14
0
 /// <summary>
 /// Render the given literal inline object to the PDF document.
 /// </summary>
 /// <param name="renderer">The PDF renderer.</param>
 /// <param name="obj">The literal inline object to be renderered.</param>
 protected override void Write(PdfBuilder renderer, LiteralInline obj)
 {
     renderer.AppendText(obj.Content.ToString(), TextStyle.Normal);
 }
Example #15
0
        public void TestDocWithNoSections()
        {
            string msg = "Builder threw an exception on a document with no sections";

            Assert.DoesNotThrow(() => builder.AppendText("x", TextStyle.Normal), msg);
        }
Example #16
0
        public void TestTableSizePartitioning()
        {
            string longString = new string('x', 50);

            // Good luck.
            builder.StartTable(1);

            builder.StartTableRow(false);
            builder.StartTableCell();
            builder.AppendText(longString, TextStyle.Normal);
            builder.FinishTableCell();

            builder.FinishTable();

            double width = ((Table)doc.LastSection.Elements[0]).Columns[0].Width.Point;

            // fixme
            Assert.GreaterOrEqual(width, 250);
        }
Example #17
0
 public void EnsureContentGoesInExistingParagraph()
 {
     pdfBuilder.AppendText("some previous content in this paragraph", TextStyle.Normal);
     renderer.Write(pdfBuilder, inline);
     Assert.AreEqual(1, document.LastSection.Elements.Count);
 }
Example #18
0
 /// <summary>
 /// Render the given HtmlEntityInline object to the PDF document.
 /// </summary>
 /// <param name="renderer">The PDF renderer.</param>
 /// <param name="obj">The HtmlEntityInline object to be renderered.</param>
 protected override void Write(PdfBuilder renderer, HtmlEntityInline obj)
 {
     renderer.AppendText(obj.Transcoded.Text, TextStyle.Normal);
 }
Example #19
0
 public void DontWriteToExistingParagraph()
 {
     pdfBuilder.AppendText("existing paragraph", TextStyle.Normal);
     renderer.Write(pdfBuilder, paragraph);
     Assert.AreEqual(2, document.LastSection.Elements.Count);
 }
Example #20
0
 /// <summary>
 /// Render the given HtmlInline object to the PDF document.
 /// </summary>
 /// <param name="renderer">The PDF renderer.</param>
 /// <param name="obj">The HtmlInline object to be renderered.</param>
 protected override void Write(PdfBuilder renderer, HtmlInline obj)
 {
     // todo - this should probably render the HTML tags to the PDF somehow.
     renderer.AppendText(obj.Tag, TextStyle.Normal);
 }
Example #21
0
 /// <summary>
 /// Render the given code inline object to the PDF document.
 /// </summary>
 /// <param name="renderer">The PDF renderer.</param>
 /// <param name="obj">The code inline object to be renderered.</param>
 protected override void Write(PdfBuilder renderer, CodeInline obj)
 {
     // todo: what is a code inline, and how should it be rendered?
     // For now, treat it like any other inline, but with code style.
     renderer.AppendText(obj.Content, TextStyle.Code);
 }