Jump to content
We've recently updated our Privacy Statement, available here ×

Embedding "print this page" control in PDF

Go to solution Solved by lucianc,

Recommended Posts

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?



Link to comment
Share on other sites

  • Replies 8
  • Created
  • Last Reply

Top Posters In This Topic

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.



Link to comment
Share on other sites

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.
Link to comment
Share on other sites

  • Solution

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.



Link to comment
Share on other sites

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()));



Link to comment
Share on other sites

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.
Link to comment
Share on other sites

  • 8 months later...

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
Link to comment
Share on other sites

  • 8 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...