Adding links to pdf by using MigraDoc
Asked Answered
E

3

10

I use MigraDoc for creating pdf documents in the project.

Code below shows how I work with library:

        var document = new Document { Info = { Author = "title" } };
        Section section = document.AddSection();
        Paragraph paragraph = section.AddParagraph("Title");
        var renderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always) { Document = document };
        renderer.RenderDocument();

So, I'm looking for a way to adding link to web resource inside pdf.

Does someone know?)

-------------Solution-------------------

I found solution!

I tried to use AddHyperlink() for adding link, and it was the first step for this. The code below shows correct using:

        var h = paragraph.AddHyperlink("http://stackoverflow.com/",HyperlinkType.Web);
        h.AddFormattedText("http://www.stackoverflow.com/");
Ellene answered 1/10, 2013 at 16:44 Comment(0)
E
15

To add a link use AddHyperlink():

    var h = paragraph.AddHyperlink("http://stackoverflow.com/",HyperlinkType.Web);
    h.AddFormattedText("http://www.stackoverflow.com/");

So the idea that you should add some text for a link to make link visible.

Ellene answered 2/10, 2013 at 7:39 Comment(0)
L
2

Use paragraph.AddHyperlink() for that purpose. You will need HyperlinkType.Web.

Lowry answered 1/10, 2013 at 21:50 Comment(3)
ThomasH, I tried to use it before, but there is no links in a document! (} var paragraph = section.AddParagraph("test link"); paragraph.AddHyperlink("stackoverflow.com", HyperlinkType.Web);Ellene
@Ellene The AddXxx functions usually return a newly created object, so it's generally a good idea to check the return type. The MigraDoc samples also show how to use AddHyperlink, just see the samples site: pdfsharp.net/wiki/HelloMigraDoc-sample.ashx?HL=addhyperlinkLowry
@PDFsharp Team Yes, I found the documentation) Thanks! But I think I done it without googling if there were some comments to a method args, and why do not add separate constructor with initializing some content like a text?Ellene
S
-1

This solution worked for me where declaring a new var of type Hyperlink threw a "'Rectangle' must not be null here." exception.

YourParagraph.AddHyperlink("patriots.win", HyperlinkType.Url).AddFormattedText("Red Pill", _hyperLink);
Scherman answered 28/12, 2023 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.