By Date: <-- -->
By Thread: <-- -->

Adding javascript to existing pdf to print



Hey Guys,
 
I understand that this question has been asked before, but something is not working for me. I have a PDF doc (as a byte array). I want to add _javascript_ to it, so that it prints out on the client. Pasted below is my code
 
When I do a document.close(), I get an error "The document has no pages". I am not sure, what I am doing wrong.
I tried to add some content to the doc, but then, it only prints the new content.
I also tried to add _javascript_ to the document object, instead of the writer, but that doesn't work either.
 
Can someone help me with this.  Thank you so much guys

 byte[] pdfData = dctxt.getDocData();  // getting the pdf data
 ByteArrayOutputStream bos = new ByteArrayOutputStream(pdfData.length);
 bos.write(pdfData);
 Document document = new Document();
 PdfWriter writer = PdfWriter.getInstance(document, bos);
 writer.setViewerPreferences( PdfWriter.HideMenubar | PdfWriter.HideToolbar | PdfWriter.HideWindowUI );
 document.open();
 writer.addJavaScript(
   "this.print({bUI: false,bSilent: false,bShrinkToFit: true});" +
   "\r\n" +
   "this.closeDoc();"
   );
 document.close();
 response.setHeader("Content-Type", "application/pdf");
 response.setContentLength(bos.size()); 
 OutputStream os = response.getOutputStream();
 bos.writeTo(os);
 os.flush();
 os.close();