Embedding "print this page" control in PDF

I'm trying to figure out how to embed a control in my document that, when clicked, opens the print dialog to print the currently viewed page.  I can do this in Acrobat by setting the Link Properties for an element, then "Run a JavaScript" and "Execute a Menu Item".  The code is "this.print(null,this.pageNum,this.pageNum)".

What I haven't figured out yet is a) how to include this in a PDF generated using Jasper reports and iReport (4.5.0), or b) how to tie my specific element to trigger such code.

I've found the net.sf.jasperreports.export.pdf.javascript property, which seems like it's part of the solution.  However, despite weeks of searching and experimenting, I haven't been able to make this work.  Is it possible?  If so, can it be set up through iReport?



sfergus1's picture
Joined: Nov 22 2012 - 8:46am
Last seen: 10 years 1 month ago

3 Answers:

Having all the functionality available to standard report text elements would indeed be impossible to achieve given the current PDF exporter code.

But you can write a PDF handler that outputs a simple text with the Javascript action.  Attaching such a minimal implementation.

To see it in action, add the following line to jasperreports_extension.properties:


and then out a generic element with genericElementType namespace="urn:me:me" name="print" in a report.

The most important part that is missing is the text font.  JRPdfExporter.getFont() is protected unfortunately, if required you might be able to duplicate some code from there.

The text itself can be easily given as a generic element parameter.



lucianc's picture
Joined: Jul 17 2006 - 1:10am
Last seen: 12 min 8 sec ago

Thank you very much! This was exactly the push I needed. I've gotten this working and made the solution a bit more generic to allow for any kind of Javascript code attached to the generic element using parameters, just as with the text as you indicated. The modification was trivial. We'll see if it retains formatting here:

String linkText = (String)element.getParameterValue("text");
String script = (String)element.getParameterValue("script");

Phrase phrase = new Phrase();
Chunk chunk = new Chunk(linkText);
chunk.setAction(PdfAction.javaScript(script, exporterContext.getPdfWriter()));

sfergus1 - 10 years 2 months ago

I was able to further enhance the solution as you noted, copying a slimmed down version of the JrPdfExporter.getFont() method to my own class. I made a call to element.getStyle() and copied the TextAttribute values that interested me to the attribute map getFont() expects. This way I'm able to use the iReport GUI to define different styles for my link elements and still have them apply to the resulting generic object that's included in the PDF. Thank you again for your help.

sfergus1 - 10 years 2 months ago

Not possible with the current code.  Log a feature request in the trackers.

You can still achieve that by implementing a generic element PDF handler.  That would involve writing a class that implements GenericElementPdfHandler, registering the handler as an extension and including a generic element in the report.  The handler would do something similar to what JRPdfExporter.setHyperlinkInfo() does.



lucianc's picture
Joined: Jul 17 2006 - 1:10am
Last seen: 12 min 8 sec ago

Thanks for the quick reply. I'll have to take a look into it and see what I can do.

sfergus1 - 10 years 2 months ago

Do you happen to have any examples of such a generic element handler for PDFs? The demo library only shows HTML, and doesn't seem to be transferable. I've spent most of the day looking over the code and haven't figured out where to insert my logic. I see that JRPdfExporter.exportGenericElement() hands off control to my new handler. The other case in the code that implements GenericElementPdfHandler (MapElementPdfHandler) generates an image and then delegates back to JRPdfExporter.exportImage(). I don't think that approach is appropriate in my case. Yet to duplicate the path the logic goes through between JRPdfExporter.exportText() and JRPdfExporter.setHyperlinkInfo(), which goes from JRPdfExporter to AbstractTextRenderer to PdfTextRenderer back to JRPdfExporter, I need access to protected attributes of JRPdfExporter (tagHelper, for example). Making my class a subclass of JRPdfExporter seems like one possible way to overcome this, but I'm not sure that's the best (or even a correct) approach. Thanks, Steve.

sfergus1 - 10 years 2 months ago

you can have a PDF Generating & Reading Add-On,which  provides standard options to process PDF document, including:

  • Create and sort a new PDF document
  • Add, delete and duplicate page of PDF file
  • Merge, split, and append PDF document file
  • Add annotations on PDF document
  • Rotate one or more pages of PDF file
  • Extract text from PDF document
  • Print PDF document file
abigailklinton's picture
Joined: Aug 12 2013 - 1:28am
Last seen: 9 years 6 months ago

editing PDFs you should download one PDF editor software from http://www.pdfconverterformac.net see the descriptions of it and try to use.

tingjust - 8 years 9 months ago