X

Geertjan's Blog

  • May 24, 2006

Generate PDF from Java Files

Geertjan Wielenga
Product Manager
After downloading Jens Trapp's NB-Tidy module (discussed here) (by the way, I recently met Jens at NetBeans Day in San Francisco) to work out how his JFileChooser works, studying Masoud Kalali's option implementation (described here) (I used this implementation because everyone I've ever spoken to warned me against using System Options, these are going to be replaced and shouldn't be used, learnt, or taught), and getting some suggestions from NetBeans engineer Andrei Badea, I've got a lot further with the module I started yesterday. I've basically used the new Options Panel wizard (that you get by default when you use NetBeans IDE 5.5 Beta, but for which you needed to install Module Development Update 1 in order to get it in NetBeans IDE 5.0) to generate the files needed for extending the Options window. Then, using the resources pointed out above, I ended up with the following:

Then, when you choose this new item in a Java file...

...the following dialog box appears to tell you where you should go look for your file:

When you go to the specified folder, you'll either find a PDF containing the entire file or only the selected part of the file. (This was Andrei's suggestion. Initially, I had two separate menu items, one for the entire file and another for a selection. Andrei suggested that having one menu item might be more useful, because it would mean that the user would have to do less work, it means that you don't need to manually force the IDE to create one or the other, the IDE now just creates one or the other automatically depending on whether you've made a selection or not.)

And this is what my Projects window looks like, i.e., these are the files needed for integrating iText into NetBeans IDE:

So if you're reading this and are thinking to yourself: "Hmmm. I'd like to be able to generate PDF files from my Java files in the NetBeans Editor", then please drop me a line and I'll send you the modules (one for installing the iText JAR file and the other for accessing its methods and providing the functionality). A few test drives would be good. After that maybe it should be sent to http://www.nbextras.org.

I'd like to extend this module in several ways. For one thing, I'd like the PDF file to open automatically as soon as it is created. Must be some method in iText to allow me to do that. Secondly, I'd like the user to be able to set the font and color of the generated PDF file. That shouldn't be hard to do, something I could combine with the options window extension.

Join the discussion

Comments ( 17 )
  • Thiago HP Wednesday, May 24, 2006

    Hi, Geertjan! It's me again. :)

    This snippet demonstrates how you can set font family, size, style, and color and mix them in the sama paragraph:


    int size1 = 13;

    int size2 = 20;

    Font boldGreen = FontFactory.getFont(FontFactory.COURIER, size1, Font.BOLD, Color.GREEN);

    Font italicYellow = FontFactory.getFont(FontFactory.HELVETICA, size2, Font.ITALIC, Color.YELLOW);

    Chunk chunk1 = new Chunk("Bold green", boldGreen);

    Chunk chunk2 = new Chunk("Italic yellow", italicYellow);

    Paragraph paragraph = new Paragraph();

    paragraph.add(chunk1);

    paragraph.add(chunk2);

    If I can help in any way, you can find my e-mail at the URL above.

  • Alex Lam Wednesday, May 24, 2006

    It works for any file types opened in the editor window, with syntax highlighting and style etc. preserved, right? :-)

    I'm almost thinking about printing the entired source tree with contents and bookmarks already... :-P

    Imagincation is always quicker than action, after all.

  • Juan Ara Wednesday, May 24, 2006

    Would be possible to include the PDF option into the print dialog? I know you can have the adobe printer plugin to export directly...

    OTH, Geertjan do you know somebody experienced enough in NB editor (DnD palete, etc) to make something like Crystal intregated into NB?

    I've tried like a hundred of times but I'm afraid I lack both Netbeans and DnD knowledge (the most complex thing I made was a full DnD JTree with shadow images, replaces etc).


    JasperReports is another tool for PDF generation based on iText, but you just "write" XML files that then got parsed into iText precompiled code. Then you "fill" a "precompiled report" with your data (JRDataSource) and you have a hot PDF ready to serve.

    Not much time ago, iReport joined jasperreports and now iText has JasperReports for data reports, and iReports like a Crystall tool.

    I would be a nice feature to include that kind of functionality into NB Editors...


    Nice blog, btw, I'm addicted to it ;)
  • Geertjan Thursday, May 25, 2006
    Hi Thiago, thanks for the tips. I wrote to you today at your sourceforge e-mail to ask how I can maintain the same font coloring as the original document. Hoep you got my e-mail and, if not, now you know what my question was!

    Hi Alex, yes, since today it works for all file types. However, I don't know if it is possible to keep the original font colors. Currently, the text is all black, but I'm hoping Thiago will give advice here.

    Hi Juan, great ideas for integrating reporting tools! This is definitely a frequently asked question, in fact, every second or third person I've met that uses NetBeans has asked about having some reporting tool for NetBeans. I'm sure something along these lines will be added at some point, I just don't know when. By the way, thanks for being addicted to my blog! I'm afraid that I am a little bit too...

  • Thiago HP Thursday, May 25, 2006
    Hi, Geertjan!

    Unfortunately, my SourceForge mail alias is not working :(. I just realized that because i havent't received your mail So I ask you to send your message again to thiagohp at gmail dot .com.
  • Sandip Thursday, May 25, 2006
    For:
    "I'd like the PDF file to open automatically as soon as it is created"
    Get the URL of the generated file and in mustang use:
    http://download.java.net/jdk6/docs/api/java/awt/Desktop.html#browse(java.net.URI)
    In Tiger you could use:
    http://www.netbeans.org/download/5_5/javadoc/org-openide-awt/org/openide/awt/HtmlBrowser.URLDisplayer.html#showURL(java.net.URL)
    which AFAIK should do the right thing.
    which is based on same principle.
    BTW, Cool stuff.
    Now we should think of generating the dynamic Key Binding reference PDF card (I don;t know if you recall but we talked about a while ago) using the same API.
  • Juan Ara Thursday, May 25, 2006

    Hi again Geertjan,

    Just a note, I would bet my right hand on as soon as Creator is bundled within NB and Creator DnD editing features are included (and "stabled") into NB OpenIDE Apis will be a lot of movement about reporting tools.

    Ranging from foo to jasper, there will be a lot of tools/plugins generating foo transformers/iText/whateverelse code for you. The hard thing of any reporting tool is the DnD feature of objects arround an empty (or not) sheet.

    Time will tell, but I'm sure I'll preserve my right hand... or I'll had to code one myself ;)


    Regards,
    Juan
  • Geertjan Friday, May 26, 2006
    Hi Sandip, I've looked at that HtmlBrowser.URLDisplayer.html#showURL method, but doesn't seem to work for me -- I think it applies only to HTML files, not to PDF. Generating the dynamic Key Binding reference might be an implementation, though I'm not sure if it applies. This API only takes the raw code (or HTML tags, etc.). So far I've not been able to take a deployed HTML file, for example, and turn it into a PDF file.
  • Sandip Friday, May 26, 2006
    I was not suggesting using the HTML output generated by the Keybinding module. We could directly generate input necessary for the PDF API. No worries. I will look into it.

    BTW I have put the Key Bbinding Explorer module on my update center. I just have not blogged about it.

  • Geertjan Friday, May 26, 2006
    OK. But, personally, I find the usefulness of that PDF document quite limited, now that NetBeans users can generate their own HTML document whenever they need it (after mapping the "Export Shortcuts to HTML" action, which is in the "Other" category in the Keymap section in the Options window).
  • Sandip Friday, May 26, 2006
    I see.

    BTW it took me while to figure out where the output of "Export Shortcuts to HTML" action (silently) went. It seems it goes to userdir/config/shortcuts.html

  • Geertjan Friday, May 26, 2006
    Yup. It isn't officially supported but somehow crept into NB 5.0. It would be nice if there was some message that you get, but I'd rather have no message than not having this shortcut action. I think it is great. (One could even add one's own profile and then it is included in the generated HTML doc.)
  • guest Monday, July 17, 2006
    afasdfasdf
  • Puneeth M I Tuesday, July 10, 2007
    Can you please send the modules to generate the PDF files from JAVA files in netbeans.
  • Geertjan Tuesday, July 10, 2007
    Here they are, Puneeth: https://nbitextsupport.dev.java.net/
  • Tatiana Thursday, December 11, 2008

    HI,

    I was wondering if this tool is available for download?

    Tatiana


  • noman Monday, July 25, 2016

    Can anyone please send here Generate PDF in java . thanks for sharing :)


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.