Bekerja dengan tindakan PDF di Java
Aspose.PDF for Java memungkinkan Anda menetapkan aksi pada tombol, dokumen, dan halaman untuk membuat file PDF menjadi interaktif.
Menambahkan tindakan cetak bernama
Gunakan contoh ini ketika tombol pada halaman harus memicu perintah cetak.
- Buka PDF sumber
Documentdan pilih halaman target. - Buat
ButtonFielddan tetapkan sebuahNamedActionuntuk mencetak. - Tambahkan tombol ke formulir dan simpan dokumen.
public static void addNamedActionPrint(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
Page page = document.getPages().get_Item(1);
Rectangle rect = new Rectangle(10, 10, 100, 40, true);
ButtonField printButton = new ButtonField(page, rect);
printButton.setPartialName("printButton");
printButton.setValue("Print");
printButton.getAnnotationActions().setOnReleaseMouseBtn(
new NamedAction(PredefinedAction.File_Print));
Border border = new Border(printButton);
border.setWidth(1);
printButton.setBorder(border);
document.getForm().add(printButton, 1);
document.save(outputFile.toString());
}
}
Menambahkan aksi sembunyikan
Gunakan contoh ini ketika tombol harus menampilkan atau menyembunyikan sekumpulan bidang formulir, seperti kotak centang.
- Buka PDF sumber
Documentdan kumpulkan widget formulir target. - Buat sebuah tombol dan tetapkan sebuah
HideActionke itu. - Tambahkan tombol ke formulir dan simpan dokumen yang diperbarui.
public static void addNamedActionHide(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
List<WidgetAnnotation> checkboxes = new ArrayList<>();
for (WidgetAnnotation field : document.getForm()) {
if (field instanceof CheckboxField) {
checkboxes.add(field);
}
}
Rectangle rect = new Rectangle(10, 410, 140, 440, true);
ButtonField hideButton = new ButtonField(document.getPages().get_Item(1), rect);
hideButton.setPartialName("HideButton");
hideButton.setValue("Hide Checkboxes");
hideButton.getAnnotationActions().setOnReleaseMouseBtn(
new HideAction(checkboxes.toArray(new WidgetAnnotation[0]), true));
document.getForm().add(hideButton, 1);
document.save(outputFile.toString());
}
}
Menambahkan tombol navigasi halaman
Contoh ini membuat tombol halaman pertama, sebelumnya, berikutnya, dan terakhir di seluruh dokumen.
- Buka PDF sumber
Document. - Buat tombol navigasi untuk setiap halaman dan tetapkan aksi bawaan yang cocok.
- Tambahkan tombol ke formulir dan simpan dokumen.
public static void addNavigationButtons(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
int totalPages = document.getPages().size();
for (Page page : document.getPages()) {
ButtonField firstPageButton = new ButtonField(page, new Rectangle(10, 10, 110, 40, true));
firstPageButton.setPartialName("First Page");
firstPageButton.setValue("First Page");
firstPageButton.getCharacteristics().setBorder(com.aspose.pdf.Color.getRed());
firstPageButton.getCharacteristics().setBackground(com.aspose.pdf.Color.getOrange().toRgb());
firstPageButton.setReadOnly(document.getPages().indexOf(page) == 1);
firstPageButton.getAnnotationActions().setOnReleaseMouseBtn(
new NamedAction(PredefinedAction.FirstPage));
document.getForm().add(firstPageButton);
ButtonField previousPageButton = new ButtonField(page, new Rectangle(120, 10, 220, 40, true));
previousPageButton.setPartialName("Previous Page");
previousPageButton.setValue("Previous Page");
previousPageButton.getCharacteristics().setBorder(com.aspose.pdf.Color.getRed());
previousPageButton.getCharacteristics().setBackground(com.aspose.pdf.Color.getOrange().toRgb());
previousPageButton.setReadOnly(document.getPages().indexOf(page) == 1);
previousPageButton.getAnnotationActions().setOnReleaseMouseBtn(
new NamedAction(PredefinedAction.PrevPage));
document.getForm().add(previousPageButton);
ButtonField nextPageButton = new ButtonField(page, new Rectangle(230, 10, 330, 40, true));
nextPageButton.setPartialName("Next Page");
nextPageButton.setValue("Next Page");
nextPageButton.getCharacteristics().setBorder(com.aspose.pdf.Color.getRed());
nextPageButton.getCharacteristics().setBackground(com.aspose.pdf.Color.getOrange().toRgb());
nextPageButton.setReadOnly(document.getPages().indexOf(page) == totalPages);
nextPageButton.getAnnotationActions().setOnReleaseMouseBtn(
new NamedAction(PredefinedAction.NextPage));
document.getForm().add(nextPageButton);
ButtonField lastPageButton = new ButtonField(page, new Rectangle(340, 10, 440, 40, true));
lastPageButton.setPartialName("Last Page");
lastPageButton.setValue("Last Page");
lastPageButton.getCharacteristics().setBorder(com.aspose.pdf.Color.getRed());
lastPageButton.getCharacteristics().setBackground(com.aspose.pdf.Color.getOrange().toRgb());
lastPageButton.setReadOnly(document.getPages().indexOf(page) == totalPages);
lastPageButton.getAnnotationActions().setOnReleaseMouseBtn(
new NamedAction(PredefinedAction.LastPage));
document.getForm().add(lastPageButton);
}
document.save(outputFile.toString());
}
}
Menambahkan aksi submit
Gunakan contoh ini ketika tombol harus mengirim data formulir ke URL.
- Buka PDF sumber
Document. - Buat
SubmitFormActiondengan URL target dan flag. - Tetapkan aksi ke bidang tombol dan simpan dokumen.
public static void addSubmitAction(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
SubmitFormAction submitAction = new SubmitFormAction();
FileSpecification submitUrl = new FileSpecification();
submitUrl.setFileSystem("URL");
submitUrl.setName("http://localhost:3000/submit");
submitAction.setUrl(submitUrl);
submitAction.setFlags(SubmitFormAction.EXPORT_FORMAT | SubmitFormAction.SUBMIT_COORDINATES);
Rectangle rect = new Rectangle(10, 10, 100, 40, true);
ButtonField submitButton = new ButtonField(document.getPages().get_Item(1), rect);
submitButton.setPartialName("SubmitButton");
submitButton.setValue("Submit");
submitButton.getAnnotationActions().setOnReleaseMouseBtn(submitAction);
document.getForm().add(submitButton, 1);
document.save(outputFile.toString());
}
}
Menambahkan tindakan peluncuran tingkat dokumen
Contoh ini menetapkan aksi JavaScript yang dijalankan ketika dokumen dibuka, disimpan, atau dicetak.
- Buka PDF sumber
Document. - Buat yang diperlukan objek
JavascriptActionuntuk peristiwa dokumen. - Tetapkan tindakan dan simpan dokumen.
public static void addLaunchActions(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
document.setOpenAction(new JavascriptAction("app.launchURL('http://localhost:3000/open');"));
document.getActions().setBeforeSaving(
new JavascriptAction("app.launchURL('http://localhost:3000/save');"));
document.getActions().setBeforePrinting(
new JavascriptAction("app.launchURL('http://localhost:3000/print');"));
document.save(outputFile.toString());
}
}
Menambahkan aksi buka dan tutup halaman
Gunakan contoh ini ketika halaman tertentu harus memicu aksi saat dibuka dan ditutup.
- Buka PDF sumber
Documentdan pastikan halaman target ada. - Buat navigasi halaman dan aksi JavaScript.
- Tetapkan aksi halaman dan simpan dokumen.
public static void addPageActions(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
if (document.getPages().size() < 3) {
System.out.println("Error: The document does not have at least 3 pages.");
return;
}
Page page = document.getPages().get_Item(3);
GoToAction action = new GoToAction(page);
action.setDestination(new XYZExplicitDestination(page, 0, page.getPageInfo().getHeight(), 1));
page.getActions().setOnOpen(action);
page.getActions().setOnClose(
new JavascriptAction("app.launchURL('http://localhost:3000/page/3');"));
document.save(outputFile.toString());
}
}
Menghapus tindakan halaman
Gunakan pendekatan ini ketika aksi buka dan tutup yang sebelumnya ditetapkan harus dihapus dari halaman.
- Buka PDF sumber
Documentdan pastikan halaman target ada. - Hapus semua aksi dari halaman tersebut.
- Simpan dokumen yang diperbarui.
public static void removePageActions(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
if (document.getPages().size() < 3) {
System.out.println("Error: The document does not have at least 3 pages.");
return;
}
Page page = document.getPages().get_Item(3);
page.getActions().removeActions();
document.save(outputFile.toString());
}
}