Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

PDF Renderer / multipage pdf



just starting to learn java; am tryng to use PDFRenderer to show pdf files (mainly multipage music scores); Use Netbeans 7 IDE; the program structure is like this: File selector to select the pdf file
Text field to enter the no. of seconds (user-defined) between the
pages of the pdf

Code to render the pdf and show it page by page with the
user-defined delay between pages

The core code to render the pdf and the delay between pages:
"filename" and "timepassed" are passed after getting these from the
file selector and textfield

public void pdfProcess(String filename, int timepassed){
String pdfFile = filename;
int time = timepassed;
int i = 0;

JFrame ShowFrame = new JFrame();
Container showpane = ShowFrame.getContentPane();
showpane.setVisible(true);
PagePanel showPanel = new PagePanel();
showpane.add(showPanel);
ShowFrame.pack();

try {
RandomAccessFile raf = new RandomAccessFile (pdfFile,"r");
FileChannel channel = raf.getChannel();
ByteBuffer buf =
channel.map(FileChannel.MapMode.READ_ONLY,0,channel.size());
PDFFile pdffile = new PDFFile (buf);

int n = pdffile.getNumPages();

do {
PDFPage page = pdffile.getPage(i+1);
int x = page.getPageNumber();
System.out.println("page "+x);

Rectangle2D r2d = page.getBBox ();
double width = r2d.getWidth ();
double height = r2d.getHeight ();
width /= 72.0;
height /= 72.0;
int res = Toolkit.getDefaultToolkit ().getScreenResolution ();
width *= res;
height *= res;
System.out.println("starting page"+x);
Image image = page.getImage((int)width,
(int)height,r2d,null,true,true);

System.out.println("finished page"+x);

ShowFrame.setSize(780,1180);
/*showpane.setSize(780,1180);
/*showPanel.setSize(780,1180);*/
ShowFrame.setVisible(true);
showPanel.setVisible(true);

System.out.println("showing page"+x);
showPanel.showPage(page);

pagedelay(time);

i++;
}while(i