Java 关闭添加了JFreeChart的文档时出现itextpdf异常

Java 关闭添加了JFreeChart的文档时出现itextpdf异常,java,exception,itext,jfreechart,Java,Exception,Itext,Jfreechart,我正试图将JFreeChart添加到我的PDF文件中,但在itextpdf的内部却出现了一个异常。我不知道我做错了什么,也不知道例外情况到底告诉了我什么。这是我的代码段: try { PdfWriter writer=PdfWriter.getInstance(document,new FileOutputStream(filePath.getText())); document.open(); document.add(s

我正试图将JFreeChart添加到我的PDF文件中,但在itextpdf的内部却出现了一个异常。我不知道我做错了什么,也不知道例外情况到底告诉了我什么。这是我的代码段:

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();
例外情况即将结束。以下是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)

我不知道这意味着什么,因为它不涉及我的代码。我假设我正在传递的参数中有某种东西导致了这种情况,但我不知道该看什么。有人能为我解释一下这个例外,并为我指出正确的方向吗?TIA.

当您从不知从何处复制代码时,忘记了一行:

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);
如果没有
dispose()
,您可能会遇到各种奇怪的错误


tp.addTemplate(tp,0,0)应该是
cb.addTemplate(tp,0,0)

谢谢,这样就消除了错误,但我没有得到任何输出。回到绘图板上。无论如何,我没有完全忘记,我只是没有意识到这是必要的。我估计当线程结束时,该对象将通过正常的垃圾收集释放。当您创建
PdfGraphics2D
对象时,将调用
saveState()
方法来保存图形状态,然后再根据绘图说明对其进行更改。
dispose()
方法调用
restoreState()
方法将状态恢复到开始绘图之前的状态。每次保存状态操作都需要还原状态,否则PDF语法错误。它与垃圾收集无关,但对于PDF语法的一致性是必需的。经过第二次检查,我明白了为什么什么都看不见。我会更新我的答案。谢谢。我猜出来了。虽然我正在为横向旋转页面,但我猜我必须切换高度和宽度,这是错误的。使用正确的值,我将在新页面上获得一个局部图表。我想我现在需要调整自己的体型和位置,以达到我想要的位置。再次感谢。