Java + PDF !
Java is such a big language now, that we ofter need to handle all type of application with Java. You may end up with a situation where you want to call MS word from Java, or Open Document from Java. Now, months back, I got a situation where I need to call some PDF formats from Java and need to do some operation on its pages. At that time, I have explored the Project - PDF Renderer on java.net site. Its an awesome project and cool operations.
A complete viewer and render. API’s are strong and I have just check this code from site itself. This code put the first page of your PDF file inside PagePanel. No doubt PDF is one of the open format used worldwide across all OS. In such a case, support from Java is something like adding more flavor in sweet.
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
import com.sun.pdfview.PagePanel;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import javax.swing.*;
/**
* An example of using the PagePanel class to show PDFs. For more advanced
* usage including navigation and zooming, look ad the
* com.sun.pdfview.PDFViewer class.
*
* -AT-author joshua.marinacci@sun-DOT-com
*/
public class Main {
public static void setup() throws IOException {
//set up the frame and panel
JFrame frame = new JFrame("PDF Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PagePanel panel = new PagePanel();
frame.add(panel);
frame.pack();
frame.setVisible(true);
//load a pdf from a byte buffer
File file = new File("Amityform.pdf");
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY,
0, channel.size());
PDFFile pdffile = new PDFFile(buf);
// show the first page
PDFPage page = pdffile.getPage(0);
panel.showPage(page);
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {?
Main.setup();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}
}
Just download the jar file from project site. And then :
javac -cp PDFRenderer.jar Main.java
java -cp PDFRenderer.jar;. Main
It is pretty fast as well, because IO operation has been done by NIO and channels are superb. Thanks guys for making such a great project.











Java,JavaScript,Threading,
Optimization and more with Vaibhav 



August 14th, 2008 at 5:20 pm
Nice tip, thanks.
August 21st, 2008 at 5:15 am
Thanks Guy for appreciating the tip and I hope that it would prove to be useful to you.
September 4th, 2008 at 6:02 am
it is good but tekk me how i become a good programmer
October 1st, 2008 at 8:04 pm
Hello;
I have been building with PDFRenderer2008_07_27.jar and using the PagePanel to display on-line help. It works great in MS Windows, but I recently ran the same executable .jar build under Red Hat Linux 3, Version 7 and I get a java.lang.NoClassDefFoundError: com/sun/pdfview/PagePanel. I would like to try building with a newer version of the PDFRenderer.jar. Your blog says “just download it from the project page”. A link to the project page right there would sure be nice as I haven’t been able to Google up the project page. Just in case there is nothing newer, is there an explaination for the exception in Linux but not in Windows?
Thanks
Bob.Neal@verigy.com
October 9th, 2008 at 5:24 am
yes, it is in pre-stage. So, it will take sometime to be stable. I guess we all should join the project and let the developers know about the problems.