public static void main(String[] args) throws IOException { // Load the PDF document created by SimpleForm.java try (PDDocument document = PDDocument.load(new File("target/SimpleForm.pdf"))) { // Note that the JavaScript will depend on the reader application. // The classes and methods available to Adobe Reader and Adobe Acrobat // are documented in the Acrobat SDK. String javaScript = "var now = util.printd('yyyy-mm-dd', new Date());" + "var oField = this.getField('SampleField');" + "oField.value = now;"; // Create an action as JavaScript action PDActionJavaScript jsAction = new PDActionJavaScript(); jsAction.setAction(javaScript); // Set the action to be executed when the document is opened document.getDocumentCatalog().setOpenAction(jsAction); document.save("target/UpdateFieldOnDocumentOpen.pdf"); } } }
/** * This will print the documents data. * * @param args The command line arguments. * * @throws IOException If there is an error parsing the document. */ public static void main( String[] args ) throws IOException { if( args.length != 2 ) { usage(); } else { try (PDDocument document = PDDocument.load(new File(args[0]))) { PDActionJavaScript javascript = new PDActionJavaScript( "app.alert( {cMsg: 'PDFBox rocks!', nIcon: 3, nType: 0, cTitle: 'PDFBox Javascript example' } );"); document.getDocumentCatalog().setOpenAction( javascript ); if( document.isEncrypted() ) { throw new IOException( "Encrypted documents are not supported for this example" ); } document.save( args[1] ); } } }
PDActionGoTo action = new PDActionGoTo(); action.setDestination(dest); document.getDocumentCatalog().setOpenAction(action);
void sanitizeOpenAction(PDDocumentCatalog docCatalog) throws IOException { LOGGER.trace("Checking OpenAction..."); PDDestinationOrAction openAction = docCatalog.getOpenAction(); if (openAction == null) { return; } LOGGER.debug("Found a JavaScript OpenAction, removed. Was {}", openAction); docCatalog.setOpenAction(null); pdfBleachSession.recordJavascriptThreat("Document Catalog", "OpenAction"); }
destCatalog.setOpenAction(openAction);
public static void main(String[] args) throws IOException { // Load the PDF document created by SimpleForm.java PDDocument document = PDDocument.load(new File("target/SimpleForm.pdf")); // Note that the JavaScript will depend on the reader application. // The classes and methods available to Adobe Reader and Adobe Acrobat // are documented in the Acrobat SDK. String javaScript = "var now = util.printd('yyyy-mm-dd', new Date());" + "var oField = this.getField('SampleField');" + "oField.value = now;"; // Create an action as JavaScript action PDActionJavaScript jsAction = new PDActionJavaScript(); jsAction.setAction(javaScript); // Set the action to be executed when the document is opened document.getDocumentCatalog().setOpenAction(jsAction); document.save("target/UpdateFieldOnDocumentOpen.pdf"); document.close(); } }
PDActionJavaScript javascript = new PDActionJavaScript( "app.alert( {cMsg: 'PDFBox rocks!', nIcon: 3, nType: 0, cTitle: 'PDFBox Javascript example' } );"); document.getDocumentCatalog().setOpenAction( javascript ); if( document.isEncrypted() )
PDActionGoTo action = new PDActionGoTo(); action.setDestination(dest); document.getDocumentCatalog().setOpenAction(action);
PDDocumentCatalog catalog = doc.getDocumentCatalog(); PDPage page = doc.getPage(0); // zero-based; you can also put another number to jump to a specific existing page PDPageXYZDestination dest = new PDPageXYZDestination(); dest.setPage(page); dest.setZoom(0.75f); dest.setLeft((int) page.getCropBox().getLowerLeftX()); dest.setTop((int) page.getCropBox().getUpperRightY()); PDActionGoTo action = new PDActionGoTo(); action.setDestination(dest); catalog.setActions(null); catalog.setOpenAction(action); doc.save(...);
destCatalog.setOpenAction(openAction);
destCatalog.setOpenAction(srcCatalog.getOpenAction());