Search code examples
javaexceptionitextjfreechart

itextpdf exception when closing document with JFreeChart added


I am trying to add a JFreeChart to my PDF file and am getting a exception deep in the bowels of itextpdf. I have no clue what I am doing wrong or what the exception is really telling me. Here is my code segment:

try {
            PdfWriter writer=PdfWriter.getInstance(document,new FileOutputStream(filePath.getText()));
            document.open();
            document.add(setParagraph("Stoker Monitor Report",titleFont));
            document.add(setParagraph(name.getText(),subTitleFont));
            document.add(setParagraph(date.getText(),dateFont));
            PdfContentByte cb=writer.getDirectContent();
            Float height=7.f;
            Float width=11.69f;
            PdfTemplate tp=cb.createTemplate(width,height);
            Graphics2D g2D=new PdfGraphics2D(cb,width,height);
            Rectangle2D r2D=new Rectangle2D.Double(0,0,width,height);
            Chart.getInstance().getChart().draw(g2D,r2D);
            tp.addTemplate(tp, 0, 0);
        } catch (FileNotFoundException | DocumentException e1) {
            System.err.println("Unable to open "+filePath.getText()+" for writing");
            e1.printStackTrace();
        }
        document.close();

The exception is on the close. Here is the stacktrace:

Exception in thread "AWT-EventQueue-0" com.itextpdf.text.exceptions.IllegalPdfSyntaxException: Unbalanced save/restore state operators.
at com.itextpdf.text.pdf.PdfContentByte.sanityCheck(PdfContentByte.java:4193)
at com.itextpdf.text.pdf.PdfContentByte.reset(PdfContentByte.java:1813)
at com.itextpdf.text.pdf.PdfContentByte.reset(PdfContentByte.java:1801)
at com.itextpdf.text.pdf.PdfWriter.resetContent(PdfWriter.java:746)
at com.itextpdf.text.pdf.PdfDocument.endPage(PdfDocument.java:1061)
at com.itextpdf.text.pdf.PdfDocument.close(PdfDocument.java:882)
at com.itextpdf.text.Document.close(Document.java:415)
at stokerMonitor.CreateReport$CreateButtonListener.actionPerformed(CreateReport.java:152)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

I have no idea what this means since it involves none of my code. I assume that there is something in the parameters that I am passing that is causing this but I have no idea what to look at. Can some please interpret this exception for me and point me in the right direction? TIA.


Solution

  • You forgot one line when you copied your code from who knows where:

    PdfTemplate tp=cb.createTemplate(width,height);
    Graphics2D g2D=new PdfGraphics2D(cb,width,height);
    Rectangle2D r2D=new Rectangle2D.Double(0,0,width,height);
    Chart.getInstance().getChart().draw(g2D,r2D);
    g2D.dispose();
    cb.addTemplate(tp, 0, 0);
    

    Without the dispose(), you can get all kinds of weird errors.

    Also tp.addTemplate(tp, 0, 0); should be cb.addTemplate(tp, 0, 0);