Source Code Cross Referenced for PDFDebugger.java in  » PDF » PDFBox-0.7.3 » org » pdfbox » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » PDF » PDFBox 0.7.3 » org.pdfbox 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Copyright (c) 2004-2006, www.pdfbox.org
003:         * All rights reserved.
004:         *
005:         * Redistribution and use in source and binary forms, with or without
006:         * modification, are permitted provided that the following conditions are met:
007:         *
008:         * 1. Redistributions of source code must retain the above copyright notice,
009:         *    this list of conditions and the following disclaimer.
010:         * 2. Redistributions in binary form must reproduce the above copyright notice,
011:         *    this list of conditions and the following disclaimer in the documentation
012:         *    and/or other materials provided with the distribution.
013:         * 3. Neither the name of pdfbox; nor the names of its
014:         *    contributors may be used to endorse or promote products derived from this
015:         *    software without specific prior written permission.
016:         *
017:         * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018:         * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019:         * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020:         * DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021:         * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022:         * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023:         * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024:         * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025:         * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026:         * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027:         *
028:         * http://www.pdfbox.org
029:         *
030:         */package org.pdfbox;
031:
032:        import org.pdfbox.exceptions.InvalidPasswordException;
033:
034:        import org.pdfbox.pdfviewer.PDFTreeModel;
035:        import org.pdfbox.pdfviewer.PDFTreeCellRenderer;
036:        import org.pdfbox.pdfviewer.ArrayEntry;
037:        import org.pdfbox.pdfviewer.MapEntry;
038:
039:        import org.pdfbox.pdmodel.PDDocument;
040:
041:        import org.pdfbox.util.ExtensionFileFilter;
042:
043:        import org.pdfbox.cos.COSBoolean;
044:        import org.pdfbox.cos.COSFloat;
045:        import org.pdfbox.cos.COSInteger;
046:        import org.pdfbox.cos.COSName;
047:        import org.pdfbox.cos.COSNull;
048:        import org.pdfbox.cos.COSStream;
049:        import org.pdfbox.cos.COSString;
050:
051:        //import javax.swing.tree.*;
052:        import javax.swing.tree.TreeModel;
053:        import javax.swing.tree.TreePath;
054:        import javax.swing.JFileChooser;
055:        import javax.swing.JScrollPane;
056:        import javax.swing.JPanel;
057:
058:        import java.io.ByteArrayOutputStream;
059:        import java.io.File;
060:        import java.io.FileInputStream;
061:        import java.io.InputStream;
062:        import java.io.IOException;
063:
064:        /**
065:         *
066:         * @author  wurtz
067:         * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
068:         * @version $Revision: 1.3 $
069:         */
070:        public class PDFDebugger extends javax.swing.JFrame {
071:            private File currentDir = new File(".");
072:
073:            /**
074:             * Constructor.
075:             */
076:            public PDFDebugger() {
077:                initComponents();
078:            }
079:
080:            /**
081:             * This method is called from within the constructor to
082:             * initialize the form.
083:             * WARNING: Do NOT modify this code. The content of this method is
084:             * always regenerated by the Form Editor.
085:             */
086:            private void initComponents() {
087:                jSplitPane1 = new javax.swing.JSplitPane();
088:                jScrollPane1 = new javax.swing.JScrollPane();
089:                jTree1 = new javax.swing.JTree();
090:                jScrollPane2 = new javax.swing.JScrollPane();
091:                jTextPane1 = new javax.swing.JTextPane();
092:                menuBar = new javax.swing.JMenuBar();
093:                fileMenu = new javax.swing.JMenu();
094:                openMenuItem = new javax.swing.JMenuItem();
095:                saveMenuItem = new javax.swing.JMenuItem();
096:                saveAsMenuItem = new javax.swing.JMenuItem();
097:                exitMenuItem = new javax.swing.JMenuItem();
098:                editMenu = new javax.swing.JMenu();
099:                cutMenuItem = new javax.swing.JMenuItem();
100:                copyMenuItem = new javax.swing.JMenuItem();
101:                pasteMenuItem = new javax.swing.JMenuItem();
102:                deleteMenuItem = new javax.swing.JMenuItem();
103:                helpMenu = new javax.swing.JMenu();
104:                contentsMenuItem = new javax.swing.JMenuItem();
105:                aboutMenuItem = new javax.swing.JMenuItem();
106:
107:                jTree1.setCellRenderer(new PDFTreeCellRenderer());
108:                jTree1.setModel(null);
109:
110:                setTitle("PDFBox - PDF Viewer");
111:                addWindowListener(new java.awt.event.WindowAdapter() {
112:                    public void windowClosing(java.awt.event.WindowEvent evt) {
113:                        exitForm(evt);
114:                    }
115:                });
116:
117:                jScrollPane1.setBorder(new javax.swing.border.BevelBorder(
118:                        javax.swing.border.BevelBorder.RAISED));
119:                jScrollPane1.setPreferredSize(new java.awt.Dimension(300, 500));
120:                jTree1
121:                        .addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
122:                            public void valueChanged(
123:                                    javax.swing.event.TreeSelectionEvent evt) {
124:                                jTree1ValueChanged(evt);
125:                            }
126:                        });
127:
128:                jScrollPane1.setViewportView(jTree1);
129:
130:                jSplitPane1.setRightComponent(jScrollPane2);
131:
132:                jScrollPane2.setPreferredSize(new java.awt.Dimension(300, 500));
133:                jScrollPane2.setViewportView(jTextPane1);
134:
135:                jSplitPane1.setLeftComponent(jScrollPane1);
136:
137:                JScrollPane documentScroller = new JScrollPane();
138:                //documentScroller.setPreferredSize( new Dimension( 300, 500 ) );
139:                documentScroller.setViewportView(documentPanel);
140:
141:                getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER);
142:
143:                fileMenu.setText("File");
144:                openMenuItem.setText("Open");
145:                openMenuItem.setToolTipText("Open PDF file");
146:                openMenuItem
147:                        .addActionListener(new java.awt.event.ActionListener() {
148:                            public void actionPerformed(
149:                                    java.awt.event.ActionEvent evt) {
150:                                openMenuItemActionPerformed(evt);
151:                            }
152:                        });
153:
154:                fileMenu.add(openMenuItem);
155:
156:                saveMenuItem.setText("Save");
157:                //fileMenu.add(saveMenuItem);
158:
159:                saveAsMenuItem.setText("Save As ...");
160:                //fileMenu.add(saveAsMenuItem);
161:
162:                exitMenuItem.setText("Exit");
163:                exitMenuItem
164:                        .addActionListener(new java.awt.event.ActionListener() {
165:                            public void actionPerformed(
166:                                    java.awt.event.ActionEvent evt) {
167:                                exitMenuItemActionPerformed(evt);
168:                            }
169:                        });
170:
171:                fileMenu.add(exitMenuItem);
172:
173:                menuBar.add(fileMenu);
174:
175:                editMenu.setText("Edit");
176:                cutMenuItem.setText("Cut");
177:                editMenu.add(cutMenuItem);
178:
179:                copyMenuItem.setText("Copy");
180:                editMenu.add(copyMenuItem);
181:
182:                pasteMenuItem.setText("Paste");
183:                editMenu.add(pasteMenuItem);
184:
185:                deleteMenuItem.setText("Delete");
186:                editMenu.add(deleteMenuItem);
187:
188:                //menuBar.add(editMenu);
189:
190:                helpMenu.setText("Help");
191:                contentsMenuItem.setText("Contents");
192:                helpMenu.add(contentsMenuItem);
193:
194:                aboutMenuItem.setText("About");
195:                helpMenu.add(aboutMenuItem);
196:
197:                //menuBar.add(helpMenu);
198:
199:                setJMenuBar(menuBar);
200:
201:                java.awt.Dimension screenSize = java.awt.Toolkit
202:                        .getDefaultToolkit().getScreenSize();
203:                setBounds((screenSize.width - 700) / 2,
204:                        (screenSize.height - 600) / 2, 700, 600);
205:            }//GEN-END:initComponents
206:
207:            private void openMenuItemActionPerformed(
208:                    java.awt.event.ActionEvent evt) {
209:                JFileChooser chooser = new JFileChooser();
210:                chooser.setCurrentDirectory(currentDir);
211:
212:                ExtensionFileFilter pdfFilter = new ExtensionFileFilter(
213:                        new String[] { "pdf", "PDF" }, "PDF Files");
214:                chooser.setFileFilter(pdfFilter);
215:                int result = chooser.showOpenDialog(PDFDebugger.this );
216:                if (result == JFileChooser.APPROVE_OPTION) {
217:                    String name = chooser.getSelectedFile().getPath();
218:                    currentDir = new File(name).getParentFile();
219:                    try {
220:                        readPDFFile(name);
221:                    } catch (Exception e) {
222:                        e.printStackTrace();
223:                    }
224:                }
225:            }//GEN-LAST:event_openMenuItemActionPerformed
226:
227:            private void jTree1ValueChanged(
228:                    javax.swing.event.TreeSelectionEvent evt) {
229:                TreePath path = jTree1.getSelectionPath();
230:                if (path != null) {
231:                    try {
232:                        Object selectedNode = path.getLastPathComponent();
233:                        String data = convertToString(selectedNode);
234:
235:                        if (data != null) {
236:                            jTextPane1.setText(data);
237:                        } else {
238:                            jTextPane1.setText("");
239:                        }
240:                    } catch (Exception e) {
241:                        e.printStackTrace();
242:                    }
243:                }
244:            }//GEN-LAST:event_jTree1ValueChanged
245:
246:            private String convertToString(Object selectedNode) {
247:                String data = null;
248:                if (selectedNode instanceof  COSBoolean) {
249:                    data = "" + ((COSBoolean) selectedNode).getValue();
250:                } else if (selectedNode instanceof  COSFloat) {
251:                    data = "" + ((COSFloat) selectedNode).floatValue();
252:                } else if (selectedNode instanceof  COSNull) {
253:                    data = "null";
254:                } else if (selectedNode instanceof  COSInteger) {
255:                    data = "" + ((COSInteger) selectedNode).intValue();
256:                } else if (selectedNode instanceof  COSName) {
257:                    data = "" + ((COSName) selectedNode).getName();
258:                } else if (selectedNode instanceof  COSString) {
259:                    data = "" + ((COSString) selectedNode).getString();
260:                } else if (selectedNode instanceof  COSStream) {
261:                    try {
262:                        COSStream stream = (COSStream) selectedNode;
263:                        InputStream ioStream = stream.getUnfilteredStream();
264:                        ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
265:                        byte[] buffer = new byte[1024];
266:                        int amountRead = 0;
267:                        while ((amountRead = ioStream.read(buffer, 0,
268:                                buffer.length)) != -1) {
269:                            byteArray.write(buffer, 0, amountRead);
270:                        }
271:                        data = byteArray.toString();
272:                    } catch (IOException e) {
273:                        e.printStackTrace();
274:                    }
275:                } else if (selectedNode instanceof  MapEntry) {
276:                    data = convertToString(((MapEntry) selectedNode).getValue());
277:                } else if (selectedNode instanceof  ArrayEntry) {
278:                    data = convertToString(((ArrayEntry) selectedNode)
279:                            .getValue());
280:                }
281:                return data;
282:            }
283:
284:            private void exitMenuItemActionPerformed(
285:                    java.awt.event.ActionEvent evt) {
286:                System.exit(0);
287:            }
288:
289:            /**
290:             * Exit the Application.
291:             */
292:            private void exitForm(java.awt.event.WindowEvent evt) {
293:                System.exit(0);
294:            }
295:
296:            /**
297:             * @param args the command line arguments
298:             *
299:             * @throws Exception If anything goes wrong.
300:             */
301:            public static void main(String[] args) throws Exception {
302:                PDFDebugger viewer = new PDFDebugger();
303:                if (args.length > 0) {
304:                    viewer.readPDFFile(args[0]);
305:                }
306:                viewer.show();
307:            }
308:
309:            private void readPDFFile(String file) throws Exception {
310:                InputStream input = null;
311:                File f = new File(file);
312:                input = new FileInputStream(f);
313:                PDDocument document = parseDocument(input);
314:                TreeModel model = new PDFTreeModel(document);
315:                jTree1.setModel(model);
316:                setTitle("PDFBox - " + f.getAbsolutePath());
317:                /*
318:                List pages = document.getDocumentCatalog().getAllPages();
319:                for( int i=0; i<pages.size(); i++ )
320:                {
321:                    PageWrapper wrapper = new PageWrapper();
322:                    wrapper.displayPage( (PDPage)pages.get(i) );
323:                    documentPanel.add( wrapper.getPanel() );
324:                }*/
325:            }
326:
327:            /**
328:             * This will parse a document.
329:             *
330:             * @param input The input stream for the document.
331:             *
332:             * @return The document.
333:             *
334:             * @throws IOException If there is an error parsing the document.
335:             */
336:            private static PDDocument parseDocument(InputStream input)
337:                    throws IOException {
338:                PDDocument document = PDDocument.load(input);
339:                if (document.isEncrypted()) {
340:                    try {
341:                        document.decrypt("");
342:                    } catch (InvalidPasswordException e) {
343:                        System.err.println("Error: The document is encrypted.");
344:                    } catch (org.pdfbox.exceptions.CryptographyException e) {
345:                        e.printStackTrace();
346:                    }
347:                }
348:
349:                return document;
350:            }
351:
352:            // Variables declaration - do not modify//GEN-BEGIN:variables
353:            private javax.swing.JMenuItem aboutMenuItem;
354:            private javax.swing.JMenuItem contentsMenuItem;
355:            private javax.swing.JMenuItem copyMenuItem;
356:            private javax.swing.JMenuItem cutMenuItem;
357:            private javax.swing.JMenuItem deleteMenuItem;
358:            private javax.swing.JMenu editMenu;
359:            private javax.swing.JMenuItem exitMenuItem;
360:            private javax.swing.JMenu fileMenu;
361:            private javax.swing.JMenu helpMenu;
362:            private javax.swing.JScrollPane jScrollPane1;
363:            private javax.swing.JScrollPane jScrollPane2;
364:            private javax.swing.JSplitPane jSplitPane1;
365:            private javax.swing.JTextPane jTextPane1;
366:            private javax.swing.JTree jTree1;
367:            private javax.swing.JMenuBar menuBar;
368:            private javax.swing.JMenuItem openMenuItem;
369:            private javax.swing.JMenuItem pasteMenuItem;
370:            private javax.swing.JMenuItem saveAsMenuItem;
371:            private javax.swing.JMenuItem saveMenuItem;
372:            private JPanel documentPanel = new JPanel();
373:            // End of variables declaration//GEN-END:variables
374:
375:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.