OSDN Git Service

loggerを修正した
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / PDFPreview.java
1 /*\r
2  * This file is part of NixNote/NeighborNote \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.BufferedImage;\r
23 import java.io.File;\r
24 import java.io.IOException;\r
25 \r
26 import javax.imageio.ImageIO;\r
27 \r
28 import org.apache.pdfbox.pdmodel.PDDocument;\r
29 import org.apache.pdfbox.pdmodel.PDPage;\r
30 \r
31 public class PDFPreview {\r
32 \r
33         public int getPageCount(String filePath) {\r
34                 try {\r
35                 String whichOS = System.getProperty("os.name");\r
36                         if (whichOS.contains("Windows")) {\r
37                                 filePath = filePath.replace("\\","/");\r
38                         }               \r
39                 PDDocument document = null;\r
40                         document = PDDocument.load( filePath );\r
41                         return document.getNumberOfPages();\r
42                 } catch (Exception e) {\r
43                         return 0;\r
44                 }\r
45                 \r
46         }\r
47         \r
48     // Setup the preview for PDFs\r
49     public boolean setupPreview(String filePath, String appl, int pageNumber) {\r
50                 // Fix stupid Windows file separation characters\r
51         String whichOS = System.getProperty("os.name");\r
52                 if (whichOS.contains("Windows")) {\r
53                         filePath = filePath.replace("\\","/");\r
54                 }\r
55         if (appl.equals("pdf")) {\r
56                 \r
57                 PDDocument document = null;\r
58                 try {\r
59                                 document = PDDocument.load( filePath );\r
60                                 if (document.getNumberOfPages() <= pageNumber)\r
61                                         return false;\r
62                                 if (document.getDocumentCatalog().getAllPages().size() <= pageNumber)\r
63                                         return false;\r
64                                 PDPage page = (PDPage)document.getDocumentCatalog().getAllPages().get( pageNumber );\r
65                                 BufferedImage bi = page.convertToImage();\r
66                                 \r
67                                 File outputfile;\r
68                                 outputfile = new File(filePath +".png");\r
69                                 ImageIO.write(bi, "png", outputfile);\r
70                                 return true;\r
71                 \r
72                         } catch (IOException e1) {\r
73                                 return false;\r
74                         }\r
75         }\r
76         return false;\r
77 \r
78     }\r
79 }\r