OSDN Git Service

Add logic to display stacks in notebook tree
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / PDFPreview.java
1 /*\r
2  * This file is part of NeverNote \r
3  * Copyright 2009 Randy Baumgarte\r
4  * \r
5  * This file may be licensed under the terms of of the\r
6  * GNU General Public License Version 2 (the ``GPL'').\r
7  *\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
12  *\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
17  *\r
18 */\r
19 \r
20 package cx.fbn.nevernote.gui;\r
21 \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
29 \r
30 import javax.imageio.ImageIO;\r
31 import javax.swing.ImageIcon;\r
32 \r
33 import com.sun.pdfview.PDFFile;\r
34 import com.sun.pdfview.PDFPage;\r
35 \r
36 public class PDFPreview {\r
37 \r
38         public int getPageCount(String filePath) {\r
39                 File file = new File(filePath);\r
40                 RandomAccessFile raf;\r
41                 try {\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
48                         return 0;\r
49                 }\r
50                 \r
51         }\r
52         \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
59                 }\r
60         if (appl.equals("pdf")) {\r
61                 \r
62                 try {\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
68                 \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
75             \r
76                         //generate the image\r
77                         Image img = page.getImage(\r
78                                         rect.width, rect.height, //width & height\r
79                                         rect, // clip rect\r
80                                         null, // null for the ImageObserver\r
81                                         true, // fill background with white\r
82                                         true  // block until drawing is done\r
83                         );\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
87                         File outputfile;\r
88 //                      if (pageNumber == 0)\r
89                                 outputfile = new File(filePath +".png");\r
90 //                      else\r
91 //                              outputfile = new File(filePath+"-page-"+pageNumber+".png");\r
92                         ImageIO.write(bi, "png", outputfile);\r
93                         return true;\r
94                 } catch (Exception e) {}\r
95         }\r
96         return false;\r
97 \r
98     }\r
99 }\r