You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by David VanderMolen <d_...@yahoo.com> on 2013/03/06 00:42:00 UTC

document level javascript insertion

PDFBox 1.7

I'm trying to insert some javascript into a PDF at the document level, not tied to an action.  I can't quite figure out how to do the insert and get the xref to set in the PDJavascriptNameTreeNode. 

PDDocument doc = PDDocument.load(inputStream);
PDDocumentCatalog catalog=doc.getDocumentCatalog();
PDDocumentNameDictionary names=catalog.getNames();

PDJavascriptNameTreeNode ntree=new PDJavascriptNameTreeNode();
Map<String,PDTextStream> map=new HashMap<String,PDTextStream>();
PDActionJavaScript js1= new PDActionJavaScript("omitted");

//need to insert the javascript somewhere and get its xref
String xref="17 0 R";


map.put("com.adobe.acrobat.SharedReview.Register", new PDTextStream(xref));
ntree.setNames(map);
names.setJavascript(ntree);
catalog.setNames(names);

The desired output looks like this:

6 0 obj
<< /JavaScript 11 0 R >>
endobj
11 0 obj
<< /Names [ (com.adobe.acrobat.SharedReview.Register) 17 0 R  ] >>
endobj
17 0 obj
<< /JS 26 0 R /S /JavaScript >>
endobj
26 0 obj
<< /Length 7 >>
stream
omittedendstream
endobj

Thank you for the assistance. 

Re: document level javascript insertion

Posted by David VanderMolen <d_...@yahoo.com>.
I figured it out, needed to declare the map more generically COSObjectable.

Map<String,COSBase> map=new HashMap<String,COSBase>();
PDActionJavaScript js1= new PDActionJavaScript("omitted");
//need to create the COSObjects in the document, then set the names to refer to their number
COSBase cb1=js1.getCOSObject();
       
map.put("com.adobe.acrobat.SharedReview.Register", cb1);
ntree.setNames(map);
names.setJavascript(ntree);
catalog.setNames(names);

FYI, you can invoke/control a shared review by injecting the following javascript into the PDF file (after replacing variables).
private static final String REGISTER="(function () {"+
    "if (app.viewerVersion >= 8 && (!app.viewerType.match(/Reader/) || requestPermission(permission.annot, permission.create) == permission.granted)) {"+
    "    var msg = {"+
    "        doc: this, "+
    "        initiator: (new String(\"$initiator\")), "+ //email address of initiator
    "        id: (new String(\"$hash\")), "+ //MD5 hash of filename
    "        source: (new String(\"smb:$share_path$share_file__$hash\")), "+ //path to review folder, named pdf.name + "__" + the id from previous line
    "        driver: (new String(\"urn://ns.adobe.com/Collaboration/SharedReview/SMB\")), "+ //folder-based review
    "        invitees: (new String(\"$invitees\")), "+ //semi-colon delimited string of email addresses
    "        sentDate: (new Date($sentDate)), "+ //replace
    "        deadDate: $deadDate, "+ //replace, may be null
    "        requireSave: (new Boolean(false)), "+ //was true
    "        cc: (new String(\"\")), "+
    "        distributionMethod: (new String(\"UPLOAD\")), "+
    "        versionInfo: (new Number(10)), "+
    "        accessLevel: (new Number(0))"+
    "    };"+
    "    Collab.registerReview(msg);"+
    "}"+
    "})()";




________________________________
 From: David VanderMolen <d_...@yahoo.com>
To: "users@pdfbox.apache.org" <us...@pdfbox.apache.org> 
Sent: Tuesday, March 5, 2013 6:42 PM
Subject: document level javascript insertion
 
PDFBox 1.7

I'm trying to insert some javascript into a PDF at the document level, not tied to an action.  I can't quite figure out how to do the insert and get the xref to set in the PDJavascriptNameTreeNode. 

PDDocument doc = PDDocument.load(inputStream);
PDDocumentCatalog catalog=doc.getDocumentCatalog();
PDDocumentNameDictionary names=catalog.getNames();

PDJavascriptNameTreeNode ntree=new PDJavascriptNameTreeNode();
Map<String,PDTextStream> map=new HashMap<String,PDTextStream>();
PDActionJavaScript js1= new PDActionJavaScript("omitted");

//need to insert the javascript somewhere and get its xref
String xref="17 0 R";


map.put("com.adobe.acrobat.SharedReview.Register", new PDTextStream(xref));
ntree.setNames(map);
names.setJavascript(ntree);
catalog.setNames(names);

The desired output looks like this:

6 0 obj
<< /JavaScript 11 0 R >>
endobj
11 0 obj
<< /Names [ (com.adobe.acrobat.SharedReview.Register) 17 0 R  ] >>
endobj
17 0 obj
<< /JS 26 0 R /S /JavaScript >>
endobj
26 0 obj
<< /Length 7 >>
stream
omittedendstream
endobj

Thank you for the assistance.