示例#1
0
        public void TestClearLinkState()
        {
            builder.SetLinkState("this is a link");
            builder.ClearLinkState();
            builder.AppendText("some text", TextStyle.Normal);
            Assert.AreEqual(1, doc.LastSection.Elements.Count);
            Paragraph paragraph = (Paragraph)doc.LastSection.Elements[0];

            // This hyperlink is empty - arguably it shouldn't be inserted at all.
            Assert.AreEqual(2, paragraph.Elements.Count);
            Assert.True(paragraph.Elements[0] is Hyperlink);
            Assert.True(paragraph.Elements[1] is FormattedText);
        }
示例#2
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();
        }
示例#3
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);
        }
示例#4
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();
            }
        }