Java How to close PDF from using pdf javascript?

若如初见. 提交于 2019-12-24 02:14:12

问题


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.



来源:https://stackoverflow.com/questions/16017205/java-how-to-close-pdf-from-using-pdf-javascript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!