Add hyperlink to PDF

Add hyperlink to PDF

This code samples draws a hyperlink on an existing PDF page. The link is added as a link annotation. A URI action is associated with the mouse up event.

<![CDATA[using (FileStream fileIn = new FileStream("in.pdf", FileMode.Open, FileAccess.Read))
{
    Document pdf = new Document(fileIn);

    Page page = pdf.Pages[0];

    Link link = new Link();
    page.Links.Add(link);

    // position link at the top-right corner
    link.Height = 40;
    link.Width = 100;
    link.Left = page.Width - link.Width;
    link.Bottom = page.Height - link.Height;

    // add a UriAction to the mouse up event
    link.MouseUpActions.Add(new UriAction("http://www.tallcomponents.com"));

    using (FileStream fileOut = new FileStream("out.pdf", FileMode.Create, FileAccess.Write))
    {
        pdf.Write(fileOut);
    }                
}
Using fileIn As New FileStream("..\..\..\inputDocuments\BlueWater.pdf", FileMode.Open, FileAccess.Read)
    Dim pdf As New Document(fileIn)

    Dim page As Page = pdf.Pages(0)

    Dim link As New Link()
    page.Links.Add(link)

    ' position link at the top-right corner
    link.Height = 40
    link.Width = 100
    link.Left = page.Width - link.Width
    link.Bottom = page.Height - link.Height

    ' add a UriAction to the mouse up event
    link.MouseUpActions.Add(New UriAction("http://www.tallcomponents.com"))

    Using fileOut As New FileStream("..\..\out.pdf", FileMode.Create, FileAccess.Write)
        pdf.Write(fileOut)
    End Using
End Using