ratlan
ratlan

Reputation: 351

Open print dialog automatically when PDF opened, using iText7

I use iText7 to generate a PDF and then open it in a new tab.

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/pdf"));
String filename = "C:\\temp\\first-output2.pdf";
headers.add("content-disposition", "inline;filename=" + filename);
headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");

The intent of the user is always to print, so displaying the PDF and letting him click the print button is inconvenient. That's why I want to open the print dialog immediately when the PDF opens.

This solution did not work

Open print dialog automatically when PDF opened, using iText

because PdfAction.PRINTDIALOG does not exist in iText7.

I also tried various JavaScript options that didn't work, like

PdfAction action = PdfAction.createJavaScript("this.print(true);\\r"); 

How can I open the print dialog directly after page load?

Upvotes: 1

Views: 1883

Answers (1)

Alexey Subach
Alexey Subach

Reputation: 12302

To open print dialog on document open, you need to use this.print(true); JavaScript code.

You can add such an action in iText7 in the following way:

PdfAction printAction = new PdfAction();
printAction.put(PdfName.S, PdfName.JavaScript);
printAction.put(PdfName.JS, new PdfString("this.print(true);\r"));
pdfDocument.getCatalog().setOpenAction(printAction);

Upvotes: 5

Related Questions