Print PDF Created in iTextSharp and then Return Co

2019-07-11 13:02发布

问题:

This is somewhat of a return to my 2012 question - Is it possible to fire a keypress event for an iframe that has been used to load a PDF file, I think I may have been mistaken accepting the answer there.

The current situation is this -

  • The document of the main page has an onkeypress event attached to it which will load a PDF into an object when the user scans a particular barcode.
  • The PDF is created using iTextSharp and has the open action set - writer.SetOpenAction(new PdfAction(PdfAction.PRINTDIALOG)) (writer is a PdfWriter).

So a user will scan the barcode which automatically loads a PDF file which opens it's print dialog. The user will then choose the printer and print the PDF. The problem comes when the user wants to scan the next unit they need to click somewhere within the html body (but outside of the PDF object) and then scan the barcode for the new PDF to be loaded.

I had a look at this and saw that it is possible to setup some kind of communication between the PDF plugin and the browser but is is possible to tell the plugin to surrender control to the browser/javascript

EDIT Some extra background information -

  • I've tried using both objects and and iframes.
  • Using the debugger, if I type document.activeElement.id gives the ID of an element an appropriate element however (in the case of text inputs) any key presses do not appear.
  • Using the debugger I can use blur the active element bring focus to another element even change the value of an input but no user input is seen by the HTML/javascript when the user types/scans.
  • This is a legacy application runs in Internet Explorer compatibility mode.

Just to sum up - I want to scan a barcode which will open a PDF (already done) which opens it's print dialog automatically (already done) the user prints the PDF and then without any further human interaction with the page (i.e. the user does not need to use the mouse or keyboard after clicking the print button in the print dialog) the user should be able to scan another barcode which will open a new PDF.

回答1:

Rather than render the PDF inside an iframe/object, why not take advantage of modern HTML5 and Mozilla's hard work and render it using https://github.com/mozilla/pdf.js/ - it is a fully HTML5/javascript PDF renderer and does not require any plugins.

There is a nice online demo at http://mozilla.github.io/pdf.js/web/viewer.html

Then you would be on the same page with JavaScript all the way down and could avoid having to pass focus/events etc between the two environments, so you could catch events anywhere on the page easily enough.