You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by "Vassallo, Fabio" <Fa...@wuerth-phoenix.com> on 2017/04/04 13:24:27 UTC

JFreeChart instance into a PDF (PDFBox vs. iText)

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?

Thank you in advance,
Fabio Vassallo


Fabio Vassallo
Product Development (CIS)

[cid:image001.png@01D18B3E.92F52E70]
Würth Phoenix S.r.l.
via Kravogl 4, 39100 Bolzano
T: +39 0471 564 116
F: +39 0471 564 122
Website<http://www.wuerth-phoenix.com/> | e-Mail<ma...@wuerth-phoenix.com> | Map<https://www.google.de/maps/place/Wuerth+Phoenix+S.R.L./@46.474192,11.33141,15z/data=!4m2!3m1!1s0x0:0x98a5db69edb2a02?hl=en>

[twitter-2]<https://twitter.com/WuerthPhoenix>  [wordpress-2] <http://www.neteye-blog.com/>   [linkedin] <https://www.linkedin.com/company/wuerth-phoenix>   [youtube] <https://www.youtube.com/user/WuerthPhoenix>   [http://feng-shui-web.net/blog2/wp-content/uploads/2016/02/xing-icon.png] <https://www.xing.com/companies/w%C3%BCrthphoenixsrl>   [facebook] <https://www.facebook.com/wuerthphoenix>


Re: JFreeChart instance into a PDF (PDFBox vs. iText)

Posted by Tilman Hausherr <TH...@t-online.de>.
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\u2019s 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