We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Determine if Javascript present in a PDF file

Hi,

I am trying to determine if a pdf file contains any javascript. I am having mixed success with the below code, which I tried to model from another post in this forum that was targeting .NET code. I am using java aspose pdf 21.1. This code seems to find javascript within annotations, but not javascript embedded in other places. Do you have a different recommended approach or can you see what I am doing wrong?

thanks,
Mike

            Document pdfdoc = new Document(inStream);

            //
            // Check document actions
            //
            DocumentActionCollection actions = pdfdoc.getActions();
            if (isJavascriptAction(actions.getAfterPrinting()) || isJavascriptAction(actions.getAfterSaving()) ||
                isJavascriptAction(actions.getBeforeClosing()) || isJavascriptAction(actions.getBeforePrinting()) ||
                isJavascriptAction(actions.getBeforeSaving())) {
                String result = "Security error - Javascript embedded in document level actions for file: " + documentName;
                LOGGER.error(result);
                throw new Exception("Attachment " + documentName + " cannot be processed");
            }

            //
            // Check page level actions and page annotation level actions
            //
            for (Page page: pdfdoc.getPages()) {
                if (isJavascriptAction(page.getActions().getOnClose()) || isJavascriptAction(page.getActions().getOnOpen())) {
                    String result = "Security error - Javascript embedded in page level actions for file: " + documentName;
                    LOGGER.error(result);
                    throw new Exception("Attachment " + documentName + " cannot be processed");
                }
                for (Annotation annotation: page.getAnnotations()) {
                    PdfActionCollection annotationActions = annotation.getPdfActions();
                    for (PdfAction annotationAction: annotationActions) {
                        if (isJavascriptAction(annotationAction)) {
                            String result = "Security error - Javascript embedded in page annotation level actions for file: " + documentName;
                            LOGGER.error(result);
                            throw new Exception("Attachment " + documentName + " cannot be processed");
                        }

                    }
                }
            }

private static boolean isJavascriptAction(PdfAction action) {
boolean isJavascriptAction = false;
if (action != null) {
if (action.getClass().equals(JavascriptAction.class)) {
isJavascriptAction = true;
}
}
return isJavascriptAction;
}

@mlittle4444

You can use Document.JavaScript property to check the JavaScript of PDF. This property returns the collection of JavaScript of document level. Hope this helps you.

Document doc1 = new Document(dataDir + "AddJavascript.pdf");
IList keys = (System.Collections.IList)doc1.JavaScript.Keys;
Console.WriteLine("=============================== ");
foreach (string key in keys)
{
    Console.WriteLine(key + " ==> " + doc1.JavaScript[key]);
}

Thank you. I think that sample code was .NET, I am using Java, but used that as a model for checking document level JS in the PDF. It also seems getKeys() returns a null pointer exception if there is no javascript in the pdf file rather than gracefully returning null or an empty list. I worked around that. Here was my final code that seems to be working:

ByteArrayDataSource btDataSource = new ByteArrayDataSource(attachment, MediaType.APPLICATION_OCTET_STREAM.getType());
DataHandler dataHandler = new DataHandler(btDataSource);
InputStream inStream = dataHandler.getInputStream();
if (inStream != null) {
Document pdfdoc = new Document(inStream);

            //
            // Document level check for javascript
            //
            try {
                for (String key : pdfdoc.getJavaScript().getKeys()) {
                    String result = "Security error - Javascript embedded in document level actions for file: " + documentName;
                    LOGGER.error(result);
                    throw new Exception("Attachment " + documentName + " cannot be processed");
                }
            }
            catch (NullPointerException np) {
               // Handle aspose Bug when collection empty, its throwing a null pointer exception
            }

            //
            // Check page level actions and page annotation level actions
            //
            for (Page page: pdfdoc.getPages()) {
                if (isJavascriptAction(page.getActions().getOnClose()) || isJavascriptAction(page.getActions().getOnOpen())) {
                    String result = "Security error - Javascript embedded in page level actions for file: " + documentName;
                    LOGGER.error(result);
                    throw new Exception("Attachment " + documentName + " cannot be processed");
                }
                for (Annotation annotation: page.getAnnotations()) {
                    PdfActionCollection annotationActions = annotation.getPdfActions();
                    for (PdfAction annotationAction: annotationActions) {
                        if (isJavascriptAction(annotationAction)) {
                            String result = "Security error - Javascript embedded in page annotation level actions for file: " + documentName;
                            LOGGER.error(result);
                            throw new Exception("Attachment " + documentName + " cannot be processed");
                        }

                    }
                }
            }
        }
    }
}

private static boolean isJavascriptAction(PdfAction action) {
    boolean isJavascriptAction = false;
    if (action != null) {
        if (action.getClass().equals(JavascriptAction.class)) {
            isJavascriptAction = true;
        }
    }
    return isJavascriptAction;
}

@mlittle4444

It is nice to hear from you that your problem has been resolved. However, we have logged a ticket for your case to detect JavaScript in PDF as PDFJAVA-41407. We will inform you via this forum thread once there is an update available on it.

1 Like