Geertjan's Blog

  • May 23, 2006

Writing PDF Files for Selected Java Code in NetBeans IDE

Geertjan Wielenga
Product Manager
If you go to O'Reilly's Open Source Java Directory, you'll find a long list of open source Java APIs. Many of them provide a great opportunity for you if you want to create NetBeans modules (either for NetBeans IDE or for any other application based on the NetBeans Platform) quickly and easily. After all, why reinvent the wheel? So, let's create a PDF writer, using iText (click here to go to the iText homepage):

  1. Download the iText JAR file, from the site specified above. In the IDE, create a module suite project and then a library wrapper module project. Wrap the JAR file that you downloaded into the library wrapper module project.

  2. Now create a module project and add it to the module suite. Declare a dependency on the iText library wrapper module project. Now you can use the code provided by the iText JAR file. For example, the code I used for this example is the "Hello World" code in this iText example page. For example, use the Action wizard to create an action that is aware of EditorCookie (rather than the default DataObject). Then use this snippet from the FAQ and send the selection to the iText API.

  3. Install the library wrapper module project and the module project that provides the functionality and then use the new menu item you added for Java files.

If you want to see the performAction that I described above in step 2, click this link, which takes you to a PDF file that I created using my new module.

By the way, since this is the code that I use to get selected text...

EditorCookie c = (EditorCookie) activatedNodes[0].getCookie(EditorCookie.class);
if (c != null) {
JEditorPane[] panes = c.getOpenedPanes();
if (panes.length > 0) {
String selection = panes[0].getSelectedText();

...you don't need to be very smart to work out that this is the code you need to get ALL of the current file:

EditorCookie c = (EditorCookie) activatedNodes[0].getCookie(EditorCookie.class);
if (c != null) {
JEditorPane[] panes = c.getOpenedPanes();
if (panes.length > 0) {
String selection = panes[0].getText();

This means I could very easily create two menu items—one for creating a PDF file for the current Java file open in the Source Editor and another for creating a PDF file for the currently selected code.

Join the discussion

Comments ( 4 )
  • Thiago HP Tuesday, May 23, 2006
    What about using a monospaced font to generate PDF for source files?
    final private static FONT = FontFactory.getFont(

    FontFactory.COURIER, 12, Font.NORMAL);
    then . . .
    Paragraph p = new Paragraph(text, FONT);
  • Geertjan Tuesday, May 23, 2006
    Wow. Very nice. Just tried it. Looks great!
  • Thiago HP Tuesday, May 23, 2006
    You could update the sample PDF so. :)
    The next step would be syntax coloring of the source code. You can add colored content to a Paragraph through Chunk:
    Paragraph p;
    p = new Paragraph("Roses are ");
    p.add(new Chunk("red", red));
    This example is given in the iText documentation.
    What do your think?
    PS. How do I format comments?
  • Geertjan Tuesday, May 23, 2006
    Hi Thiago, thanks for the additional comments. I haven't tried to change the color yet, but have updated the link so that you get a new sample page. So now if you click "this link" in the blog entry above, you'll find the performAction method, but this time with courier font. (You can add formatted comments by using P tags and BR tags, like in normal HTML.) Thanks for your comments here, they're great.
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.