Java How to close PDF from using pdf javascript?

2019-07-09 17:39发布

问题:

I'm using a servlet to send a pdf stream to a browser.

There is a request that the pdf must open the print dialog when I show it to the user. I was successful doing this using iText api. Something like this:

stamper.setPageAction(PdfWriter.PAGE_OPEN, new PdfAction(PdfAction.PRINTDIALOG), 1);

Now I do need to close the pdf file after print. I tried using pdfactions, but I'm can't get it. What I tried is:

writer.setAdditionalAction(PdfWriter.DID_PRINT, PdfAction.javaScript("app.execMenuItem('Close');", writer));

or

writer.setAdditionalAction(PdfWriter.DID_PRINT, PdfAction.javaScript("app.close();", writer));

I don't necessarily need to use pdfActions, but I don't see how to get it after user sent the pdf to printer.

Do you have any ideas?

回答1:

There's a reason why app.execMenuItem('Close'); and app.close() don't work. These methods are designed to close the standalone version of Adobe Reader/Acrobat. I guess you're viewing the PDF in a browser, in which case you use Adobe Reader as a plug-in, Chrome's PDF viewer, pdf.js in Firefox, or any other PDF viewer.

Problem #1: you need to close the browser window from a PDF document. PDFs don't have the power to control your browser. Suppose they would: wouldn't that be a serious security issue?

Problem #2: you embed the PDF inside a HTML page (e.g. using an <object> tag) and establish a communication between the JavaScript in the PDF and the JavaScript in the HTML. I've described how to do this in my book, but: it won't work with all browsers on all OSs. For instance: Chrome's PDF viewer and Firefox's pdf.js will completely ignore your commands.

You are asking a solution using our iText library, but you're asking something that can't be done with any software.



标签: java pdf itext