Membuat tautan PDF di Java
Contents
[
Hide
]
Aspose.PDF for Java menggunakan LinkAnnotation bersama dengan objek aksi untuk mendefinisikan perilaku tautan.
Membuat tautan aksi peluncuran
Gunakan contoh ini ketika anotasi tautan harus meluncurkan file eksternal atau target.
- Buka PDF sumber
Documentdan pilih halaman target. - Buat sebuah
LinkAnnotationdan mengonfigurasi batas serta warnanya. - Tetapkan sebuah
LaunchActiondan simpan dokumen.
public static void createLinkAnnotationLaunchAction(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
Page page = document.getPages().get_Item(1);
LinkAnnotation link = new LinkAnnotation(page, new Rectangle(10, 580, 120, 600, true));
Border border = new Border(link);
border.setWidth(5);
border.setDash(new Dash(1, 1));
link.setBorder(border);
link.setColor(Color.getGreen());
link.setAction(new LaunchAction(document, inputFile.toString()));
page.getAnnotations().add(link);
document.save(outputFile.toString());
}
}
Membuat tautan go-to remote
Gunakan contoh ini ketika tautan harus membuka halaman di dokumen PDF lain.
- Buka PDF sumber
Document. - Buat sebuah
LinkAnnotationpada halaman target. - Tetapkan sebuah
GoToRemoteActiondan simpan file output.
public static void createLinkAnnotationGoToRemoteAction(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
Page page = document.getPages().get_Item(1);
LinkAnnotation link = new LinkAnnotation(page, new Rectangle(10, 580, 120, 600, true));
link.setColor(Color.getGreen());
link.setAction(new GoToRemoteAction(inputFile.toString(), 1));
page.getAnnotations().add(link);
document.save(outputFile.toString());
}
}
Membuat tautan go-to internal
Gunakan contoh ini ketika tautan harus menavigasi ke halaman lain di dalam dokumen PDF yang sama.
- Buka PDF sumber
Document. - Buat sebuah
LinkAnnotationdan konfigurasikan tampilannya. - Tetapkan sebuah
GoToActionke halaman tujuan dan simpan dokumen.
public static void createLinkAnnotationGoToAction(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
Page page = document.getPages().get_Item(1);
LinkAnnotation link = new LinkAnnotation(page, new Rectangle(10, 580, 120, 600, true));
Border border = new Border(link);
border.setWidth(5);
border.setDash(new Dash(1, 1));
link.setBorder(border);
link.setColor(Color.getGreen());
if (document.getPages().size() >= 4) {
link.setAction(new GoToAction(document.getPages().get_Item(4)));
} else {
link.setAction(new GoToAction(document.getPages().get_Item(document.getPages().size())));
}
page.getAnnotations().add(link);
document.save(outputFile.toString());
}
}
Membuat tautan URI
Gunakan contoh ini ketika tautan harus membuka sumber daya web melalui aksi URI.
- Buka PDF sumber
Document. - Buat sebuah
LinkAnnotationpada halaman. - Tetapkan sebuah
GoToURIActiondan simpan file output.
public static void createLinkAnnotationGoToUriAction(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
Page page = document.getPages().get_Item(1);
LinkAnnotation link = new LinkAnnotation(page, new Rectangle(10, 580, 120, 600, true));
link.setColor(Color.getGreen());
link.setAction(new GoToURIAction("https://docs.aspose.com/pdf/python"));
page.getAnnotations().add(link);
document.save(outputFile.toString());
}
}