OSDN Git Service

Migrate from PDFRenderer to PDFBox to generate PNG images for inline viewing.
authorRandy Baumgarte <randy@fbn.cx>
Mon, 27 Dec 2010 11:18:42 +0000 (06:18 -0500)
committerRandy Baumgarte <randy@fbn.cx>
Sat, 1 Jan 2011 23:40:12 +0000 (18:40 -0500)
src/cx/fbn/nevernote/gui/PDFPreview.java

index 6180722..7f09ea0 100644 (file)
 \r
 package cx.fbn.nevernote.gui;\r
 \r
-import java.awt.Image;\r
-import java.awt.Rectangle;\r
 import java.awt.image.BufferedImage;\r
 import java.io.File;\r
-import java.io.RandomAccessFile;\r
-import java.nio.ByteBuffer;\r
-import java.nio.channels.FileChannel;\r
+import java.io.IOException;\r
 \r
 import javax.imageio.ImageIO;\r
-import javax.swing.ImageIcon;\r
 \r
-import com.sun.pdfview.PDFFile;\r
-import com.sun.pdfview.PDFPage;\r
+import org.apache.pdfbox.pdmodel.PDDocument;\r
+import org.apache.pdfbox.pdmodel.PDPage;\r
 \r
 public class PDFPreview {\r
 \r
        public int getPageCount(String filePath) {\r
-               File file = new File(filePath);\r
-               RandomAccessFile raf;\r
                try {\r
-                       raf = new RandomAccessFile(file, "r");\r
-                       FileChannel channel = raf.getChannel();\r
-                       ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());\r
-                       PDFFile pdffile = new PDFFile(buf);\r
-                       return pdffile.getNumPages();\r
+               String whichOS = System.getProperty("os.name");\r
+                       if (whichOS.contains("Windows")) {\r
+                               filePath = filePath.replace("\\","/");\r
+                       }               \r
+               PDDocument document = null;\r
+                       document = PDDocument.load( filePath );\r
+                       return document.getNumberOfPages();\r
                } catch (Exception e) {\r
                        return 0;\r
                }\r
@@ -59,39 +54,23 @@ public class PDFPreview {
                }\r
        if (appl.equals("pdf")) {\r
                \r
+               PDDocument document = null;\r
                try {\r
-                       File file = new File(filePath);\r
-                       RandomAccessFile raf = new RandomAccessFile(file, "r");\r
-                       FileChannel channel = raf.getChannel();\r
-                       ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());\r
-                       PDFFile pdffile = new PDFFile(buf);\r
+                               document = PDDocument.load( filePath );\r
+                               if (document.getNumberOfPages() < pageNumber)\r
+                                       return false;\r
+                               \r
+                               PDPage page = (PDPage)document.getDocumentCatalog().getAllPages().get( pageNumber );\r
+                               BufferedImage bi = page.convertToImage();\r
+                               \r
+                               File outputfile;\r
+                               outputfile = new File(filePath +".png");\r
+                               ImageIO.write(bi, "png", outputfile);\r
+                               return true;\r
                \r
-                       // draw the first page to an image\r
-                       PDFPage page = pdffile.getPage(pageNumber);\r
-                       //get the width and height for the doc at the default zoom \r
-                       Rectangle rect = new Rectangle(0,0,\r
-                    (int)page.getBBox().getWidth(),\r
-                    (int)page.getBBox().getHeight()); \r
-            \r
-                       //generate the image\r
-                       Image img = page.getImage(\r
-                                       rect.width, rect.height, //width & height\r
-                                       rect, // clip rect\r
-                                       null, // null for the ImageObserver\r
-                                       true, // fill background with white\r
-                                       true  // block until drawing is done\r
-                       );\r
-                       ImageIcon icon = new ImageIcon(img);\r
-                       BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);\r
-                       bi.getGraphics().drawImage(icon.getImage(), 0, 0, null);\r
-                       File outputfile;\r
-//                     if (pageNumber == 0)\r
-                               outputfile = new File(filePath +".png");\r
-//                     else\r
-//                             outputfile = new File(filePath+"-page-"+pageNumber+".png");\r
-                       ImageIO.write(bi, "png", outputfile);\r
-                       return true;\r
-               } catch (Exception e) {}\r
+                       } catch (IOException e1) {\r
+                               return false;\r
+                       }\r
        }\r
        return false;\r
 \r