我正在使用itext 7和java,当我尝试打开现有的pdf文件时,我的问题是异常。这是我的代码
JFileChooser load = new JFileChooser();
load.setFileFilter(new PdfFilter());
load.showOpenDialog(this);
String loadPath = load.getSelectedFile().getAbsolutePath();
//some stuff here....
path = path.concat(".pdf");//path is declared above
try {
pdf = new PdfDocument(new PdfReader(loadPath), new PdfWriter(path));
} catch (IOException ex) {
Logger.getLogger(ParagrafoFrame.class.getName()).log(Level.SEVERE, null, ex);
}
doc = new Document(pdf);
doc.add(new Paragraph("test"));
doc.close();
为什么它不起作用?
此处是堆栈跟踪
线程中的异常" AWT-EventQueue-0" com.itextpdf.io.IOException:找不到PDF标题。 在com.itextpdf.io.source.PdfTokenizer.getHeaderOffset(PdfTokenizer.java:223) at com.itextpdf.kernel.pdf.PdfReader.getOffsetTokeniser(PdfReader.java:1075) 在com.itextpdf.kernel.pdf.PdfReader。(PdfReader.java:120) 在com.itextpdf.kernel.pdf.PdfReader。(PdfReader.java:154) 在com.itextpdf.kernel.pdf.PdfReader。(PdfReader.java:170) 在com.antonionappi.editorpdf.ParagrafoFrame.continuaButtonActionPerformed(ParagrafoFrame.java:1339) 在com.antonionappi.editorpdf.ParagrafoFrame.access $ 2400(ParagrafoFrame.java:46) 在com.antonionappi.editorpdf.ParagrafoFrame $ 25.actionPerformed(ParagrafoFrame.java:404) 在javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) 在javax.swing.AbstractButton $ Handler.actionPerformed(AbstractButton.java:2348) 在javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) 在javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) 在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6533) 在javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6298) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466) at java.awt.Container.dispatchEventImpl(Container.java:2280) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access $ 500(EventQueue.java:97) at java.awt.EventQueue $ 3.run(EventQueue.java:709) at java.awt.EventQueue $ 3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90) at java.awt.EventQueue $ 4.run(EventQueue.java:731) at java.awt.EventQueue $ 4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) 在java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
答案 0 :(得分:0)
我的情况下,我忘记了关闭文档:
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
Document doc = new Document(pdfDoc);
//...
doc.close(); //HAVE YOU CLOSE?
pdfDoc.close(); //HAVE YOU CLOSE?