In This Topic
    Add hyperlinks
    In This Topic

    Introduction

    The following example demonstrates how to add an hyperlink on a specific word of a document.

    It also covers how to stylize the resulting link & add the created link to a link collection.

    Add hyperlinks on specific words of a page (C#)
    Copy Code
    public static void AddHyperlinks()
    {
      Console.WriteLine( "=== ADD HYPERLINKS ===" );
      var outputFileName = "AddHyperlinks.pdf";
      var outputPath = HyperlinksSample.HyperlinksSampleOutputDirectory + outputFileName;
    
      // Creates a PdfDocument.
      using( var pdfoutput = PdfDocument.Create( outputPath ) )
      {
        // Gets the first Page.
        var page = pdfoutput.Pages.First();
    
        // Adds a title.
        var titleStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d );
        page.AddParagraph( "Add hyperlinks", titleStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) );
    
        // Adds text.
        var textStyle = TextStyle.WithFont( titleStyle.Font, 12 );
        page.AddText( "The word XCEED points to a website.", new Point( 75, 100 ), textStyle );
    
        // Creates a new Hyperlink around the previous XCEED word and adds it to the page's HyperlinkCollection.
        var hyperlink = new Hyperlink( "www.xceed.com", new Rectangle( 127, 94, 45, 20 ) );
        hyperlink.Border.Color = Color.Orange;
        hyperlink.Border.Width = 3;
        hyperlink.Border.Style = BorderStyle.Underline;
        page.Hyperlinks.Add( hyperlink );
    
        // Saves the output document.
        pdfoutput.Save();
        Console.WriteLine( $"Created: {outputFileName}" );
      }
    }

     Learn More

    To know more about how to add text at a specific location in a PDF document, please consult the "Add Text At A Specific Location" example. 


    See Also