2 * This file is part of NeverNote
\r
3 * Copyright 2009 Randy Baumgarte
\r
5 * This file may be licensed under the terms of of the
\r
6 * GNU General Public License Version 2 (the ``GPL'').
\r
8 * Software distributed under the License is distributed
\r
9 * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
\r
10 * express or implied. See the GPL for the specific language
\r
11 * governing rights and limitations.
\r
13 * You should have received a copy of the GPL along with this
\r
14 * program. If not, go to http://www.gnu.org/licenses/gpl.html
\r
15 * or write to the Free Software Foundation, Inc.,
\r
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
\r
20 package cx.fbn.nevernote.gui;
\r
22 import java.awt.Image;
\r
23 import java.awt.Rectangle;
\r
24 import java.awt.image.BufferedImage;
\r
25 import java.io.File;
\r
26 import java.io.RandomAccessFile;
\r
27 import java.nio.ByteBuffer;
\r
28 import java.nio.channels.FileChannel;
\r
30 import javax.imageio.ImageIO;
\r
31 import javax.swing.ImageIcon;
\r
33 import com.sun.pdfview.PDFFile;
\r
34 import com.sun.pdfview.PDFPage;
\r
36 public class PDFPreview {
\r
38 public int getPageCount(String filePath) {
\r
39 File file = new File(filePath);
\r
40 RandomAccessFile raf;
\r
42 raf = new RandomAccessFile(file, "r");
\r
43 FileChannel channel = raf.getChannel();
\r
44 ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
\r
45 PDFFile pdffile = new PDFFile(buf);
\r
46 return pdffile.getNumPages();
\r
47 } catch (Exception e) {
\r
53 // Setup the preview for PDFs
\r
54 public boolean setupPreview(String filePath, String appl, int pageNumber) {
\r
55 // Fix stupid Windows file separation characters
\r
56 String whichOS = System.getProperty("os.name");
\r
57 if (whichOS.contains("Windows")) {
\r
58 filePath = filePath.replace("\\","/");
\r
60 if (appl.equals("pdf")) {
\r
63 File file = new File(filePath);
\r
64 RandomAccessFile raf = new RandomAccessFile(file, "r");
\r
65 FileChannel channel = raf.getChannel();
\r
66 ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
\r
67 PDFFile pdffile = new PDFFile(buf);
\r
69 // draw the first page to an image
\r
70 PDFPage page = pdffile.getPage(pageNumber);
\r
71 //get the width and height for the doc at the default zoom
\r
72 Rectangle rect = new Rectangle(0,0,
\r
73 (int)page.getBBox().getWidth(),
\r
74 (int)page.getBBox().getHeight());
\r
76 //generate the image
\r
77 Image img = page.getImage(
\r
78 rect.width, rect.height, //width & height
\r
80 null, // null for the ImageObserver
\r
81 true, // fill background with white
\r
82 true // block until drawing is done
\r
84 ImageIcon icon = new ImageIcon(img);
\r
85 BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
\r
86 bi.getGraphics().drawImage(icon.getImage(), 0, 0, null);
\r
88 // if (pageNumber == 0)
\r
89 outputfile = new File(filePath +".png");
\r
91 // outputfile = new File(filePath+"-page-"+pageNumber+".png");
\r
92 ImageIO.write(bi, "png", outputfile);
\r
94 } catch (Exception e) {}
\r