To know about how to add hyperlinks in a PDF document instead, please consult the "Add Hyperlinks" example.
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"; // Loads a PdfDocument. using( var pdfDoc = PdfDocument.Load( HyperlinksSample.HyperlinksSampleResourcesDirectory + filename ) ) { var outputFileName = "GetHyperlinks.pdf"; var outputPath = HyperlinksSample.HyperlinksSampleOutputDirectory + outputFileName; // Creates a PdfDocument. using( var pdfoutput = PdfDocument.Create( outputPath ) ) { // Gets the first Page. var page1 = pdfoutput.Pages.First(); // Adds a Title. var textStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d ); page1.AddParagraph( "Get hyperlinks", textStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) ); // Adds red Courier text. var redTextStyle = TextStyle.WithFontAndColor( pdfoutput.Fonts.GetStandardFont( StandardFontType.Courier ), 12d, Brushes.Red ); page1.AddText( $"Printing Hyperlinks from: {filename}", new Point( 25, 60 ), redTextStyle ); var hyperlinkTextStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Courier ), 12d ); var counter = 0; // Parses all the Pages. foreach( var page in pdfDoc.Pages ) { // Gets the Hyperlinks from the loaded document's Pages. foreach( var hyperlink in page.Hyperlinks ) { // Adds an Hyperlink Url, Location and pageId. page1.AddText( $"-{hyperlink.Url}", new Point( 25, 90 + ( counter * 30 ) ), hyperlinkTextStyle ); page1.AddText( $" Location: {hyperlink.Bounds.TopLeft}, PageId: {page.Id}", new Point( 25, 105 + ( counter * 30 ) ), hyperlinkTextStyle ); counter++; } } // Saves the output document. pdfoutput.Save(); Console.WriteLine( $"Info exported to path: {outputFileName}" ); } } } | |
To know about how to add hyperlinks in a PDF document instead, please consult the "Add Hyperlinks" example.