Search in sources :

Example 1 with PDActionJavaScript

use of org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript in project tika by apache.

the class AbstractPDF2XHTML method handleDestinationOrAction.

private void handleDestinationOrAction(PDDestinationOrAction action, ActionTrigger actionTrigger) throws IOException, SAXException, TikaException {
    if (action == null || !config.getExtractActions()) {
        return;
    }
    AttributesImpl attributes = new AttributesImpl();
    String actionOrDestString = (action instanceof PDAction) ? "action" : "destination";
    addNonNullAttribute("class", actionOrDestString, attributes);
    addNonNullAttribute("type", action.getClass().getSimpleName(), attributes);
    addNonNullAttribute("trigger", actionTrigger.name(), attributes);
    if (action instanceof PDActionImportData) {
        processDoc("", ((PDActionImportData) action).getFile(), attributes);
    } else if (action instanceof PDActionLaunch) {
        PDActionLaunch pdActionLaunch = (PDActionLaunch) action;
        addNonNullAttribute("id", pdActionLaunch.getF(), attributes);
        addNonNullAttribute("defaultDirectory", pdActionLaunch.getD(), attributes);
        addNonNullAttribute("operation", pdActionLaunch.getO(), attributes);
        addNonNullAttribute("parameters", pdActionLaunch.getP(), attributes);
        processDoc(pdActionLaunch.getF(), pdActionLaunch.getFile(), attributes);
    } else if (action instanceof PDActionRemoteGoTo) {
        PDActionRemoteGoTo remoteGoTo = (PDActionRemoteGoTo) action;
        processDoc("", remoteGoTo.getFile(), attributes);
    } else if (action instanceof PDActionJavaScript) {
        PDActionJavaScript jsAction = (PDActionJavaScript) action;
        Metadata m = new Metadata();
        m.set(Metadata.CONTENT_TYPE, "application/javascript");
        m.set(Metadata.CONTENT_ENCODING, StandardCharsets.UTF_8.toString());
        m.set(PDF.ACTION_TRIGGER, actionTrigger.toString());
        m.set(TikaCoreProperties.EMBEDDED_RESOURCE_TYPE, TikaCoreProperties.EmbeddedResourceType.MACRO.name());
        String js = jsAction.getAction();
        js = (js == null) ? "" : js;
        if (embeddedDocumentExtractor.shouldParseEmbedded(m)) {
            try (InputStream is = TikaInputStream.get(js.getBytes(StandardCharsets.UTF_8))) {
                embeddedDocumentExtractor.parseEmbedded(is, xhtml, m, false);
            }
        }
        addNonNullAttribute("class", "javascript", attributes);
        addNonNullAttribute("type", jsAction.getType(), attributes);
        addNonNullAttribute("subtype", jsAction.getSubType(), attributes);
        xhtml.startElement("div", attributes);
        xhtml.endElement("div");
    } else {
        xhtml.startElement("div", attributes);
        xhtml.endElement("div");
    }
}
Also used : PDAction(org.apache.pdfbox.pdmodel.interactive.action.PDAction) PDActionImportData(org.apache.pdfbox.pdmodel.interactive.action.PDActionImportData) PDActionLaunch(org.apache.pdfbox.pdmodel.interactive.action.PDActionLaunch) AttributesImpl(org.xml.sax.helpers.AttributesImpl) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) TikaInputStream(org.apache.tika.io.TikaInputStream) InputStream(java.io.InputStream) Metadata(org.apache.tika.metadata.Metadata) PDActionJavaScript(org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript) PDActionRemoteGoTo(org.apache.pdfbox.pdmodel.interactive.action.PDActionRemoteGoTo)

Example 2 with PDActionJavaScript

use of org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript in project pdfbox by apache.

the class FDFJavaScript method setDoc.

/**
 * Sets the dictionary's "Doc" entry.
 *
 * @param map Map of named "JavaScript" dictionaries.
 */
public void setDoc(Map<String, PDActionJavaScript> map) {
    COSArray array = new COSArray();
    for (Map.Entry<String, PDActionJavaScript> entry : map.entrySet()) {
        array.add(new COSString(entry.getKey()));
        array.add(entry.getValue());
    }
    dictionary.setItem(COSName.DOC, array);
}
Also used : COSArray(org.apache.pdfbox.cos.COSArray) COSString(org.apache.pdfbox.cos.COSString) PDActionJavaScript(org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) COSString(org.apache.pdfbox.cos.COSString)

Example 3 with PDActionJavaScript

use of org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript in project pdfbox by apache.

the class AddJavascript method main.

/**
 * 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]);
        }
    }
}
Also used : PDDocument(org.apache.pdfbox.pdmodel.PDDocument) PDActionJavaScript(org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript) IOException(java.io.IOException) File(java.io.File)

Example 4 with PDActionJavaScript

use of org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript in project pdfbox by apache.

the class UpdateFieldOnDocumentOpen method main.

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");
    }
}
Also used : PDDocument(org.apache.pdfbox.pdmodel.PDDocument) PDActionJavaScript(org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript) File(java.io.File)

Example 5 with PDActionJavaScript

use of org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript in project pdfbox by apache.

the class FieldTriggers method main.

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"))) {
        PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
        // Get the field and the widget associated to it.
        // Note: there might be multiple widgets
        PDField field = acroForm.getField("SampleField");
        PDAnnotationWidget widget = field.getWidgets().get(0);
        // Some of the actions are available to the widget, some are available to the form field.
        // See Table 8.44 and Table 8.46 in the PDF 1.7 specification
        // Actions for the widget
        PDAnnotationAdditionalActions annotationActions = new PDAnnotationAdditionalActions();
        // Create an action when entering the annotations active area
        PDActionJavaScript jsEnterAction = new PDActionJavaScript();
        jsEnterAction.setAction("app.alert(\"On 'enter' action\")");
        annotationActions.setE(jsEnterAction);
        // Create an action when exiting the annotations active area
        PDActionJavaScript jsExitAction = new PDActionJavaScript();
        jsExitAction.setAction("app.alert(\"On 'exit' action\")");
        annotationActions.setX(jsExitAction);
        // Create an action when the mouse button is pressed inside the annotations active area
        PDActionJavaScript jsMouseDownAction = new PDActionJavaScript();
        jsMouseDownAction.setAction("app.alert(\"On 'mouse down' action\")");
        annotationActions.setD(jsMouseDownAction);
        // Create an action when the mouse button is released inside the annotations active area
        PDActionJavaScript jsMouseUpAction = new PDActionJavaScript();
        jsMouseUpAction.setAction("app.alert(\"On 'mouse up' action\")");
        annotationActions.setU(jsMouseUpAction);
        // Create an action when the annotation gets the input focus
        PDActionJavaScript jsFocusAction = new PDActionJavaScript();
        jsFocusAction.setAction("app.alert(\"On 'focus' action\")");
        annotationActions.setFo(jsFocusAction);
        // Create an action when the annotation loses the input focus
        PDActionJavaScript jsBlurredAction = new PDActionJavaScript();
        jsBlurredAction.setAction("app.alert(\"On 'blurred' action\")");
        annotationActions.setBl(jsBlurredAction);
        widget.setActions(annotationActions);
        // Actions for the field
        PDFormFieldAdditionalActions fieldActions = new PDFormFieldAdditionalActions();
        // Create an action when the user types a keystroke in the field
        PDActionJavaScript jsKeystrokeAction = new PDActionJavaScript();
        jsKeystrokeAction.setAction("app.alert(\"On 'keystroke' action\")");
        fieldActions.setK(jsKeystrokeAction);
        // Create an action when the field is formatted to display the current value
        PDActionJavaScript jsFormattedAction = new PDActionJavaScript();
        jsFormattedAction.setAction("app.alert(\"On 'formatted' action\")");
        fieldActions.setF(jsFormattedAction);
        // Create an action when the field value changes
        PDActionJavaScript jsChangedAction = new PDActionJavaScript();
        jsChangedAction.setAction("app.alert(\"On 'change' action\")");
        // fieldActions.setV(jsChangedAction);
        // Create an action when the field value changes
        PDActionJavaScript jsRecalculateAction = new PDActionJavaScript();
        jsRecalculateAction.setAction("app.alert(\"On 'recalculate' action\")");
        fieldActions.setC(jsRecalculateAction);
        // Set the Additional Actions entry for the field
        // Note: this is a workaround as if there is only one widget the widget
        // and the form field may share the same dictionary. Now setting the
        // fields Additional Actions entry directly will overwrite the settings done for
        // the widget.
        // https://issues.apache.org/jira/browse/PDFBOX-3036
        field.getActions().getCOSObject().addAll(fieldActions.getCOSObject());
        document.save("target/FieldTriggers.pdf");
    }
}
Also used : PDField(org.apache.pdfbox.pdmodel.interactive.form.PDField) PDFormFieldAdditionalActions(org.apache.pdfbox.pdmodel.interactive.action.PDFormFieldAdditionalActions) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) PDAnnotationWidget(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget) PDAnnotationAdditionalActions(org.apache.pdfbox.pdmodel.interactive.action.PDAnnotationAdditionalActions) PDAcroForm(org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm) PDActionJavaScript(org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript) File(java.io.File)

Aggregations

PDActionJavaScript (org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript)7 File (java.io.File)3 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)3 LinkedHashMap (java.util.LinkedHashMap)2 COSArray (org.apache.pdfbox.cos.COSArray)2 COSString (org.apache.pdfbox.cos.COSString)2 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Map (java.util.Map)1 PDAction (org.apache.pdfbox.pdmodel.interactive.action.PDAction)1 PDActionImportData (org.apache.pdfbox.pdmodel.interactive.action.PDActionImportData)1 PDActionLaunch (org.apache.pdfbox.pdmodel.interactive.action.PDActionLaunch)1 PDActionRemoteGoTo (org.apache.pdfbox.pdmodel.interactive.action.PDActionRemoteGoTo)1 PDActionURI (org.apache.pdfbox.pdmodel.interactive.action.PDActionURI)1 PDAnnotationAdditionalActions (org.apache.pdfbox.pdmodel.interactive.action.PDAnnotationAdditionalActions)1 PDFormFieldAdditionalActions (org.apache.pdfbox.pdmodel.interactive.action.PDFormFieldAdditionalActions)1 PDAnnotationWidget (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget)1 PDAcroForm (org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm)1