Xceed PDF Library for .NET Documentation v1.0
In This Topic
    Get hyperlinks
    In This Topic

    Introduction

    The following example demonstrates how to load an existing PDF document, get all the hyperlinks it contains & then output those into a newly created PDF document.


    Get hyperlinks from an existing PDF document (C#)
    Copy Code
    public static void GetHyperlinks()
    {
      Console.WriteLine("=== GET HYPERLINKS ===");
      string filename = "The Amazing World of Wildlife.pdf";
    
      // Load a pdf document.
      using( var pdfDoc = PdfDocument.Load( HyperlinksSample.HyperlinksSampleResourcesDirectory + filename ) )
      {
        var outputFileName = "GetHyperlinks.pdf";
        var outputPath = HyperlinksSample.HyperlinksSampleOutputDirectory + outputFileName;
    
        // Create a pdf document.
        using( var pdfoutput = PdfDocument.Create( outputPath ) )
        {
          // Get the first page.
          var page1 = pdfoutput.Pages.First();
    
          // Add Title.
          var textStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d );
          page1.AddParagraph( "Getting hyperlinks", textStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) );
    
          var exportData = new ExportInfoToPdf();
          // Add text.
          exportData.AddLine( pdfoutput, $"Printing Hyperlinks from: {filename}\n\n", Color.Red );
    
          bool hasHyperlinks = false;
          foreach( var page in pdfDoc.Pages )
          {
            // Get hyperlinks from loaded document pages.
            foreach( var hyperlink in page.Hyperlinks )
            {
              // Add Hyperlink Url.
              exportData.AddLine( pdfoutput, $"{hyperlink.Url}" );
              hasHyperlinks = true;
            }
          }
    
          if( hasHyperlinks )
          {
            // Save the output document.
            pdfoutput.Save();
            Console.WriteLine( $"Info exported to path: {outputFileName}" );
          }
          else
          {
            Console.WriteLine( "No Hyperlinks found in this document." );
          }
        }
      }
    }

     Learn More

    To know about how to add hyperlinks in a PDF document instead, please consult the "Add Hyperlinks" example.