Create Links in PDF file
Contents
[
Hide
]
Create Links
Aspose.PDF for Java allows you to add a link to an external PDF file so that you can link several documents together. By adding a link to an application into a document, it is possible to link to applications from a document. This is useful when you want readers to take a certain action at a specific point in a tutorial, for example, or to create a feature-rich document. To create an application link:
- Create a Document object.
- Get the Page you want to add link to.
- Create a LinkAnnotation object using the Page and Rectangle objects.
- Set the link attributes using the LinkAnnotation object.
- Also, set the to LaunchAction object’s and call setAction(..) method.
- When creating the LaunchAction object, specify the application you want to launch.
- Add the link to the Page object’s Annotations collection.
- Finally, save the updated PDF using the Document object’s Save method.
The following code snippet shows how to create a link to an application in a PDF file.
package com.aspose.pdf.examples;
import com.aspose.pdf.*;
public class ExampleLinks {
private static String _dataDir = "/home/aspose/pdf-examples/Samples/";
private static String GetDataDir() {
String os = System.getProperty("os.name");
if (os.startsWith("Windows"))
_dataDir = "C:\\Samples\\Links-Actions";
return _dataDir;
}
public static void CreateLink() {
// Open document
Document document = new Document(GetDataDir() + "CreateApplicationLink.pdf");
// Create link
Page page = document.getPages().get_Item(1);
LinkAnnotation link = new LinkAnnotation(page, new Rectangle(100, 200, 300, 300));
link.setColor(Color.getGreen());
link.setAction(new LaunchAction(document, _dataDir + "sample.pdf"));
page.getAnnotations().add(link);
// Save updated document
document.save(_dataDir + "CreateApplicationLink_out.pdf");
}
Create PDF Document Link in a PDF File
Aspose.PDF for Java allows you to add a link to an external PDF file so that you can link several documents together. To create a PDF document link:
- First, create a Document object.
- Then, get the particular Page you want to add the link to.
- Create a LinkAnnotation object using the [Page](https://reference.aspose.com/pdf/java/com.aspose.pdf/Page and Rectangle objects.
- Set the link attributes using the LinkAnnotation object.
- Call setAction(..) method and pass GoToRemoteAction object.
- While creating the GoToRemoteAction object, specify the PDF file that should launch, as well as the page number it should open on.
- Add the link to the Page object’s Annotations collection.
- Finally, save the updated PDF using the Document object’s Save method.
The following code snippet shows how to create PDF document link in a PDF file.
public static void CreatePDFDocumentLink() {
// Open document
Document document = new Document(_dataDir + "CreateDocumentLink.pdf");
// Create link
Page page = document.getPages().get_Item(1);
LinkAnnotation link = new LinkAnnotation(page, new Rectangle(100, 200, 300, 300));
link.setColor(Color.getGreen());
link.setAction(new GoToRemoteAction(_dataDir + "sample.pdf", 1));
page.getAnnotations().add(link);
// Save updated document
document.save(_dataDir + "CreateDocumentLink_out.pdf");
}