You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Martin Lynch <M....@kainos.com> on 2017/06/07 17:19:54 UTC

Checking if javascript is used in a pdf document

Hi

I have to check a pdf document to see if there is any javascript used in it anywhere.

How can I do this with PDFBox?

Thanks for any help
Martin

Martin Lynch | Senior Software Engineer | Kainos | DD: +44 (0)28 9057 1351 | Fax: +44 (0)28 9057 1101 | m.lynch@kainos.com<ma...@kainos.com>


[Sunday Times]<https://www.kainos.com/kainos-cements-position-sunday-times-top-100-best-companies-work/>

Follow the Kainos buzz on: Twitter<https://twitter.com/kainossoftware> Facebook<https://www.facebook.com/KainosSoftware> Linkedin<http://www.linkedin.com/company/kainos> Youtube<http://www.youtube.com/user/KainosSoftware> Kainos cements position sunday times top 100 best companies work<https://www.kainos.com/kainos-cements-position-sunday-times-top-100-best-companies-work/>

This e-mail is for the intended addressee only and is strictly confidential; if you receive it in error please destroy the message and all copies. Any opinion or information in this email or its attachments that does not relate to Kainos business is personal to the sender and is not endorsed by Kainos. This email has been scanned for viruses but is not guaranteed to be virus free. "Kainos" is the trading name of the Kainos Group of companies; click the link for further information https://www.kainos.com/corporate-information/. Further terms and conditions may be found on our website www.kainos.com

Re: Checking if javascript is used in a pdf document

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 07.06.2017 um 19:19 schrieb Martin Lynch:
> I have to check a pdf document to see if there is any javascript used in it anywhere.


public class PrintJavaScriptFields
{

     /**
      * This will print all the fields from the document.
      *
      * @param pdfDocument The PDF to get the fields from.
      *
      * @throws IOException If there is an error getting the fields.
      */
     public void printFields(PDDocument pdfDocument) throws IOException
     {
         PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
         PDAcroForm acroForm = docCatalog.getAcroForm();
         List<PDField> fields = acroForm.getFields();

         //System.out.println(fields.size() + " top-level fields were 
found on the form");
         for (PDField field : fields)
         {
             processField(field, "|--", field.getPartialName());
         }
     }

     private void processField(PDField field, String sLevel, String 
sParent) throws IOException
     {
         String partialName = field.getPartialName();

         if (field instanceof PDTerminalField)
         {
             PDTerminalField termField = (PDTerminalField) field;
             PDFormFieldAdditionalActions fieldActions = field.getActions();
             if (fieldActions != null)
             {
                 System.out.println(field.getFullyQualifiedName() + ": " 
+ fieldActions.getClass().getSimpleName() + " js field actionS:\n" + 
fieldActions.getCOSObject());
                 printPossibleJS(fieldActions.getK());
                 printPossibleJS(fieldActions.getC());
                 printPossibleJS(fieldActions.getF());
                 printPossibleJS(fieldActions.getV());
             }
             for (PDAnnotationWidget widgetAction : termField.getWidgets())
             {
                 PDAction action = widgetAction.getAction();
                 if (action instanceof PDActionJavaScript)
                 {
System.out.println(field.getFullyQualifiedName() + ": " + 
action.getClass().getSimpleName() + " js widget action:\n" + 
action.getCOSObject());
                     printPossibleJS(action);
                 }
             }
         }

         if (field instanceof PDNonTerminalField)
         {
             if (!sParent.equals(field.getPartialName()))
             {
                 if (partialName != null)
                 {
                     sParent = sParent + "." + partialName;
                 }
             }
             //System.out.println(sLevel + sParent);

             for (PDField child : ((PDNonTerminalField) 
field).getChildren())
             {
                 processField(child, "|  " + sLevel, sParent);
             }
         }
         else
         {
             String fieldValue = field.getValueAsString();
             StringBuilder outputString = new StringBuilder(sLevel);
             outputString.append(sParent);
             if (partialName != null)
             {
                 outputString.append(".").append(partialName);
             }
             outputString.append(" = ").append(fieldValue);
             outputString.append(", 
type=").append(field.getClass().getName());
             //System.out.println(outputString);
         }
     }

     private void printPossibleJS(PDAction kAction)
     {
         if (kAction instanceof PDActionJavaScript)
         {
             PDActionJavaScript jsAction = (PDActionJavaScript) kAction;
             String jsString = jsAction.getAction();
             if (!jsString.contains("\n"))
             {
                 // Sonst erscheint in Netbeans nichts?!
                 jsString = jsString.replaceAll("\r", 
"\n").replaceAll("\n\n", "\n");
             }
             System.out.println(jsString);
             System.out.println();
         }
     }

     /**
      * This will read a PDF file and print out the form elements. <br />
      * see usage() for commandline
      *
      * @param args command line arguments
      *
      * @throws IOException If there is an error importing the FDF document.
      */
     public static void main(String[] args) throws IOException
     {
         PDDocument pdf = null;
         try
         {
             pdf = PDDocument.load(new File(XXXXXXX));
             PrintJavaScriptFields exporter = new PrintJavaScriptFields();
             exporter.printFields(pdf);
         }
         finally
         {
             if (pdf != null)
             {
                 pdf.close();
             }
         }
     }

}


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org