i'm working with itext 7 and java and my problem is that exception when i try to open an existing pdf file. This is my code
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();
why doesn't it work?
here the stack trace
Exception in thread "AWT-EventQueue-0" com.itextpdf.io.IOException: PDF header not found. at com.itextpdf.io.source.PdfTokenizer.getHeaderOffset(PdfTokenizer.java:223) at com.itextpdf.kernel.pdf.PdfReader.getOffsetTokeniser(PdfReader.java:1075) at com.itextpdf.kernel.pdf.PdfReader.(PdfReader.java:120) at com.itextpdf.kernel.pdf.PdfReader.(PdfReader.java:154) at com.itextpdf.kernel.pdf.PdfReader.(PdfReader.java:170) at com.antonionappi.editorpdf.ParagrafoFrame.continuaButtonActionPerformed(ParagrafoFrame.java:1339) at com.antonionappi.editorpdf.ParagrafoFrame.access$2400(ParagrafoFrame.java:46) at com.antonionappi.editorpdf.ParagrafoFrame$25.actionPerformed(ParagrafoFrame.java:404) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6533) at 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) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
path
andloadPath
variables? The exception tells us that the path you provided doesn't seem to be a correct PDF file. Can you make a self-contained example of trying to load PDF file with a hard-coded path and attach the PDF? – Alexey Subachpdf = new PdfDocument(new PdfReader(loadPath), new PdfWriter(path));
inpdf1 = new PdfDocument(new PdfReader(loadPath)); pdf = new PdfDocument(new PdfWriter(path));
and now it works – thenoobdeveloper