diff --git a/README b/README index 3d7947a..e9b65d9 100644 --- a/README +++ b/README @@ -27,7 +27,10 @@ Example: page5.mediaBox.getUpperRight_y() / 2 ) output.addPage(page5) - + + # add some Javascript to launch the print window on opening this PDF + output.addJS("this.print({bUI:true,bSilent:false,bShrinkToFit:true});") + # print how many pages input1 has: print "document1.pdf has %s pages." % input1.getNumPages()) diff --git a/pyPdf/pdf.py b/pyPdf/pdf.py index bf60d01..74e3b3a 100644 --- a/pyPdf/pdf.py +++ b/pyPdf/pdf.py @@ -86,18 +86,43 @@ def __init__(self): }) self._info = self._addObject(info) + + # js object + js = DictionaryObject() + js.update({ + NameObject("/Type"): NameObject("/Action"), + NameObject("/S"): NameObject("/Javascript"), + NameObject("/JS"): NameObject(u"(app.alert({cMsg: 'Hello from PDF JavaScript', cTitle: 'Testing PDF JavaScript', nIcon: 3});)") + }) + # root object root = DictionaryObject() root.update({ NameObject("/Type"): NameObject("/Catalog"), NameObject("/Pages"): self._pages, + # NameObject("/OpenAction"): self._addObject(js), }) - self._root = self._addObject(root) + # self._root = self._addObject(root) + self._root = None + self.root = root def _addObject(self, obj): self._objects.append(obj) return IndirectObject(len(self._objects), 0, self) + def addJS(self, str): + js = DictionaryObject() + js.update({ + NameObject("/Type"): NameObject("/Action"), + NameObject("/S"): NameObject("/JavaScript"), + # NameObject("/JS"): NameObject(u"(app.alert({cMsg: 'Hello from PDF JavaScript', cTitle: 'Testing PDF JavaScript', nIcon: 3});)") + NameObject("/JS"): NameObject("(" + str + ")"), + }) + + self.root.update( { + NameObject("/OpenAction"): self._addObject(js) + } ) + def getObject(self, ido): if ido.pdf != self: raise ValueError("pdf must be self") @@ -240,6 +265,9 @@ def encrypt(self, user_pwd, owner_pwd = None, use_128bit = True): def write(self, stream): import struct + if(not self._root): + self._root = self._addObject(self.root) + externalReferenceMap = {} # PDF objects sometimes have circular references to their /Page objects