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;
}