Am 04.04.2017 um 15:24 schrieb Vassallo, Fabio:

Good afternoon.

I need to put a JFreeChart instance into a PDF document using PDFBox.

In iText it’s possible to use the PdfTemplate class to directly put the chart inside the document (see code below):

*PdfWriter writer = ...;*

*...*

*JFreeChart chart = ...;*

*float width  = ...;*

*float height = ...;*

*float posX = ...,*

*float posY = ...;*

*...*

*PdfTemplate template = writer.getDirectContent().createTemplate(width, height);*

*PdfGraphics2D graphics = new PdfGraphics2D(template, width, height);*

*Rectangle2D rectangle = new Rectangle2D.Double(0, 0, width, height);*

*chart.draw(graphics, rectangle);*

*graphics.dispose();*

*writer.getDirectContent().addTemplate(template, posX, posY);*

All I was able to do in PDFBox is converting the chart into an image, but this causes a big quality loss (see code below):

*PDDocument document = ..;*

*PDPageContentStream currentContentStream = ...;*

*...*

*JFreeChart chart = ...;*

*int width  = ...;*

*int height = ...;*

*float posX = ...,*

*float posY = ...;*

*...*

*BufferedImage bi = chart.createBufferedImage(width, height);*

*PDImageXObject image = LosslessFactory.createFromImage(document, bi);*

*currentContentStream.drawImage(image, posX, posY);*

Is there a way to put the chart into the document in the same way as using iText?


No but you might want to try this external project:

https://issues.apache.org/jira/browse/PDFBOX-3697

Please put any feedback in that issue, so that the developer reads about it.

Tilman


Reply via email to