Add Hyperlink in a PDF File
It is possible to add hyperlinks to PDF files, either to allow readers to navigate to another part of the PDF, or to external content.
In order to add web hyperlinks to PDF documents:
- Create a Document Class object.
- Get the Page Class you want to add the link to.
- Create a
LinkAnnotationobject using the
The rectangle object is used to specify the location on the page where the link should be added.
- Set the
Actionproperty to the
GoToURIActionobject which specifies the location of the remote URI.
- To display a hyperlink text, add a text string on a location similar to where the
LinkAnnotationobject is placed.
- To add a free text:
- Instantiate an
FreeTextAnnotationobject. It also accepts
Rectangleobjects as argument, so it is possible to provide same values as specified against the
- Using the
Contentsproperty, specify the string that should be displayed in the output PDF.
- Optionally, set the border width of both the
FreeTextAnnotationobjects to 0 so that they do not appear in the PDF document.
- Instantiate an
- Once the
FreeTextAnnotationobjects have been defined, add these links to the
- Finally, save the updated PDF using the
The following code snippet shows you how to add a hyperlink to a PDF file.
Create hyperlink to pages in same PDF
Aspose.PDF for .NET provides a great feature to PDF creation as well as its manipulation. It also offers the feature to add links to PDF pages and a link can either direct to pages in another PDF file, a web URL, link to launch an Application or even link to pages in same PDF file. In order to add local hyperlinks (links to pages in same PDF file), a class named LocalHyperlink is added to Aspose.PDF namespace and this class has a property named TargetPageNumber, which is used to specify the target/destination page for hyperlink.
In order to add the local hyperlink, we need to create a TextFragment so that link can be associated with the TextFragment. The TextFragment class has a property named Hyperlink which is used to associate LocalHyperlink instance. The following code snippet shows the steps to accomplish this requirement.
Get PDF Hyperlink Destination (URL)
Links are represented as annotations in a PDF file and they can be added, updated or deleted. Aspose.PDF for .NET also supports getting the destination (URL) of the hyperlink in PDF file.
To get a link's URL:
- Create a
- Get the
Pageyou want to extract links from.
- Use the
AnnotationSelectorclass to extract all the
LinkAnnotationobjects from the specified page.
- Pass the
AnnotationSelectorobject to the
- Get all the selected link annotations into an
IListobject using the
- Finally, extract the
The following code snippet shows how to get hyperlink destinations (URL) from a PDF file.
Get Hyperlink Text
A hyperlink has two parts: the text that shows in the document, and the destination URL. In some cases, it's the text rather than the URL we need.
Text and annotations/actions in a PDF file are represented by different entities. Text on а page is just а set of words and characters, while annotations bring some interactivity such as that inherent in a hyperlink.
To find the URL content, you need to work with both annotation and text. The
Annotation object does not have itself have the text but sits under the text on the page. So to get the text, the
Annotation gives the URL's bounds, while the Text object gives the URL contents. Please see the following code snippet.
Remove Document Open Action from a PDF File
How to Specify PDF Page when Viewing Document explained how to tell a document to open on a different page than the first. When concatenating several documents, and one or more has a GoTo action set, you probably want to remove them. For example, if combining two documents and the second one has a GoTo action that takes you to the second page, the output document will open on the second page of the second document instead of the first page of the combined document. To avoid this behavior, remove the open action command.
To remove an open action:
- Set the
OpenActionproperty to null.
- Save the updated PDF using the
The following code snippet shows how to remove a document open action from the PDF file.
How to Specify PDF Page when Viewing Document
When viewing PDF files in a PDF viewer such as Adobe Reader, the files usually open on the first page. However, it is possible to set the file to open on a different page.
XYZExplicitDestination class allows you to specify a page in a PDF file that you want to open. When passing the
GoToAction object value to the
OpenAction property, the document opens at the page specified against the
XYZExplicitDestination object. The following code snippet shows how to specify a page as the document open action.