Link Search Menu Expand Document

Adding links

Documents in PDF or SVG format can include links; these can be either “local” links to a different part of the document, or links to some URL on the internet.

To create a link in VectSharp, you can use the tag feature. You may have noticed that most drawing calls (e.g. FillRectangle, StrokeRectangle, FillPath, StrokePath, etc.) accept an optional tag parameter, which is a string whose default value is null. This parameter is used to identify elements in the plot. When you export the image using the SaveAsSVG or SaveAsPDF methods, you can supply an optional Dictionary<string, string> parameter (linkDestinations), which associates the tags to the corresponding link.

If a tag is associated with a link that starts with a hash (#) symbol, the link is interpreted as being an internal link to the object whose tag corresponds to the linked tag; otherwise, the link is interpreted as an external link.

The following example shows how to create an external link in an SVG image:

VectSharp

using VectSharp;
using VectSharp.SVG;

Page page = new Page(100, 25);
Graphics graphics = page.Graphics;

FontFamily family = FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica);
Font font = new Font(family, 15, true);

// Draw the text, tagging it as "link".
graphics.FillText(15, 8, "VectSharp", font, Colour.FromRgb(0, 178, 115), tag: "link");

// Dictionary containing the links.
Dictionary<string, string> links = new Dictionary<string, string>
{
    { "link", "https://github.com/arklumpus/VectSharp" }
};

page.SaveAsSVG("ExternalLink.svg", linkDestinations: links);

The following example, instead, shows how to create a PDF file with an internal link. Note that internal links are supported in SVG as well, but they are only useful if the SVG image is large enough that it does not fit on the screen. In a PDF document, instead, links can be made to point at different pages.

using VectSharp;
using VectSharp.PDF;

// Create the document.
Document doc = new Document();

Font fontNotUnderlined = new Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica),
                                  24);
Font fontUnderlined = new Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica),
                               24, true);

// Create an A4 page.
Page page1 = new Page(595, 842);

// Draw the target for the link at page 2.
page1.Graphics.FillText(50, 50, "Target for link 2", fontNotUnderlined, Colours.Black, tag: "target2");

// Draw the link to page 2.
page1.Graphics.FillText(50, 100, "Link 1", fontUnderlined, Colours.Black, tag: "link1");

// Add the page to the document.
doc.Pages.Add(page1);

// Create another A4 page.
Page page2 = new Page(595, 842);

// Draw the target for the link at page 2.
page2.Graphics.FillText(50, 50, "Target for link 1", fontNotUnderlined, Colours.Black, tag: "target1");

// Draw the link to page 2.
page2.Graphics.FillText(50, 100, "Link 2", fontUnderlined, Colours.Black, tag: "link2");

// Add the page to the document.
doc.Pages.Add(page2);

// Dictionary containing the links.
Dictionary<string, string> links = new Dictionary<string, string>
{
    { "link1", "#target1" },
    { "link2", "#target2" }
};

// Save the document as a PDF.
doc.SaveAsPDF("InternalLinks.pdf", linkDestinations: links);