You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Roberto Nibali <rn...@gmail.com> on 2015/07/28 23:22:40 UTC

pdfbox-app-2.0.0-SNAPSHOT.jar: Missing root object specification in trailer

Hi

After digging through some information regarding document object level
Javascript injecting, I created what I believe to be the shortest
PDF-compliant PDF file that works with Adobe and prints out a "Hello
World!" upon opening it:

$ cat ../pdftools/smallJShack.pdf
%PDF-1.
trailer <</Root<<
/Pages <<>>
/OpenAction <<
/S /JavaScript
/JS (app.alert({cMsg: 'Hello World!'});)>>>>>>

Running the following fails:
$ java -jar app/target/pdfbox-app-2.0.0-SNAPSHOT.jar PrintPDF
../pdftools/smallJShack.pdf
Exception in thread "main" java.io.IOException: Missing root object
specification in trailer.
    at
org.apache.pdfbox.pdfparser.COSParser.parseTrailerValuesDynamically(COSParser.java:2004)
    at
org.apache.pdfbox.pdfparser.PDFParser.initialParse(PDFParser.java:235)
    at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:267)
    at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:915)
    at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:844)
    at org.apache.pdfbox.tools.PrintPDF.main(PrintPDF.java:98)
    at org.apache.pdfbox.tools.PDFBox.main(PDFBox.java:67)

While this file opens correctly in Adobe Acrobat and prints out the "Hello
World!", is this a case that should be implemented in the PDFBox parser, or
is it too esoteric?

The background is that in my project, I'm trying to "infiltrate" the
Doc/Open Event to execute all existing javascripts inside the PDF I'm
working on when opened within Adobe Acrobat. This would allow me to
short-circuit the manual labour of implementing each and every javascript's
functionality in Java using PDFBox, since PDFBox does not currently have a
JS or Event handler.

Most of what my PDFs do is check if a certain field is set and if so, set
some fields visible and certain others invisible. The form fields whose
visibility are toggled are basically gray boxes which upon visibility patch
over a predefined geographic section of the PDF's form fields.

Best regards
Roberto

Re: pdfbox-app-2.0.0-SNAPSHOT.jar: Missing root object specification in trailer

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 29.07.2015 um 01:05 schrieb Roberto Nibali:
> This one works well and does not show the Error (14). Also, the JavaScript
> function works. Now, I only have to figure out how to put all the JS stream
> entries that match a certain criteria (you have sent me some code already
> for JS extraction) and put them into OpenActions.
>
>
>> >
>> >But you should be able to to insert that OpenAction by using
>> >PDDocumentCatalog.setOpenAction(). Or do it with catalog.getCOSObject() and
>> >insert it in the dictionary that you get.

There is a simple example in AddJavascript.java:


public class AddJavascript
{
     private AddJavascript()
     {
         //static class, should not be instantiated.
     }

     /**
      * This will print the documents data.
      *
      * @param args The command line arguments.
      *
      * @throws Exception If there is an error parsing the document.
      */
     public static void main( String[] args ) throws Exception
     {
         if( args.length != 2 )
         {
             usage();
         }
         else
         {
             PDDocument document = null;
             try
             {
                 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] );
             }
             finally
             {
                 if( document != null )
                 {
                     document.close();
                 }
             }
         }
     }

     /**
      * This will print the usage for this document.
      */
     private static void usage()
     {
         System.err.println( "Usage: java 
org.apache.pdfbox.examples.pdmodel.AddJavascript <input-pdf> 
<output-pdf>" );
     }
}


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


Re: pdfbox-app-2.0.0-SNAPSHOT.jar: Missing root object specification in trailer

Posted by Roberto Nibali <rn...@gmail.com>.
Hi Tilman

Yeah, it was a rather dumb example.

On Tue, Jul 28, 2015 at 11:53 PM, Tilman Hausherr <TH...@t-online.de>
wrote:

>  Am 28.07.2015 um 23:22 schrieb Roberto Nibali:
>
> The background is that in my project, I'm trying to "infiltrate" the
> Doc/Open Event to execute all existing javascripts inside the PDF I'm
> working on when opened within Adobe Acrobat. This would allow me to
> short-circuit the manual labour of implementing each and every javascript's
> functionality in Java using PDFBox, since PDFBox does not currently have a
> JS or Event handler.
>
>
> It's too late at night to check that "short" PDF of yours (that is
> incorrect for a different reason, AR shows an error message after the Hello:
>
>
My Acrobat now also shows the error (14), after waiting for a while ex post
opening the PDF document. I have written a much better and also PDFBox
compliant PDF example:

%PDF-1.5
%<a5><b1><eb>
1 0 obj
  << /Type /Catalog
     /Pages 2 0 R
     /OpenAction
     <<
      /S /JavaScript
      /JS
       (
         app.alert({cMsg: 'Hello World!'});
       )
     >>
  >>
endobj
2 0 obj
  << /Type /Pages
     /Kids [3 0 R]
     /Count 1
     /MediaBox [0 0 300 144]
  >>
endobj
3 0 obj
  <<  /Type /Page
      /Parent 2 0 R
      /Resources
       << /Font
           << /F1
               << /Type /Font
                  /Subtype /Type1
                  /BaseFont /Times-Roman
               >>
           >>
       >>
      /Contents 4 0 R
  >>
endobj
4 0 obj
  << /Length 55 >>
stream
  BT
    /F1 18 Tf
    0 0 Td
    (Hello World) Tj
  ET
endstream
endobj
trailer
  <<  /Root 1 0 R
      /Size 5
  >>
%%EOF

This one works well and does not show the Error (14). Also, the JavaScript
function works. Now, I only have to figure out how to put all the JS stream
entries that match a certain criteria (you have sent me some code already
for JS extraction) and put them into OpenActions.


>
> But you should be able to to insert that OpenAction by using
> PDDocumentCatalog.setOpenAction(). Or do it with catalog.getCOSObject() and
> insert it in the dictionary that you get.
>
>
Thanks. I will try that.

Best regards
Roberto

Re: pdfbox-app-2.0.0-SNAPSHOT.jar: Missing root object specification in trailer

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 28.07.2015 um 23:22 schrieb Roberto Nibali:
> The background is that in my project, I'm trying to "infiltrate" the
> Doc/Open Event to execute all existing javascripts inside the PDF I'm
> working on when opened within Adobe Acrobat. This would allow me to
> short-circuit the manual labour of implementing each and every javascript's
> functionality in Java using PDFBox, since PDFBox does not currently have a
> JS or Event handler.

It's too late at night to check that "short" PDF of yours (that is 
incorrect for a different reason, AR shows an error message after the Hello:
)

But you should be able to to insert that OpenAction by using 
PDDocumentCatalog.setOpenAction(). Or do it with catalog.getCOSObject() 
and insert it in the dictionary that you get.

Tilman