I am migrating my app from Itext 5 to 7 and cannot find anything on how to add a JFreeChart to my document. I asked on that forum but they could not help so they sent me here. Here is my working Itext 5 code:
PdfContentByte cb=writer.getDirectContent();
PdfTemplate tp=cb.createTemplate(sizeX*scale+1,sizeY*scale+1);
PdfGraphics2D g2d=new PdfGraphics2D(cb,sizeX*scale+1,sizeY*scale+1);
tp.setWidth(sizeX*scale+1);
tp.setHeight(sizeY*scale+1);
Chart.getInstance().getChart().draw(g2d, new java.awt.geom.Rectangle2D.Float(0,0,sizeX*scale,sizeY*scale));
g2d.dispose();
cb.add(tp);
Can someone help me create the itext 7 equivalent? TIA.
As I understand it, iText has dropped the PdfGraphics2D class. One approach you could try is to used OrsonPDF for the
PDFGraphics2D
implementation (it will create a PDF version of your chart), then embed that in your PDF created by iText. I wrote a blog post about that here:https://jfree.github.io/orsonpdf/
Here is the source code (needs JFreeChart 1.5.0, OrsonPDF 1.7 or later, and iText 7 - I used 7.0.4):