Sizing and Locating a JFreeChart with ItextPDF

A discussion forum for JFreeChart (a 2D chart library for the Java platform).

Sizing and Locating a JFreeChart with ItextPDF

Postby gw1500se » Thu Nov 23, 2017 2:53 pm

I started off with this thread to figure out how to get a chart to display properly in a PDF document using itextpdf. This may be more of an itextpdf question but while I was able to get the chart displayed, I am not able to scale and locate the chart. After some research it seemed like I needed to convert my PdfTemplate to an image so I added that code thus:
Code: Select all
   Document document = new Document(PageSize.A4.rotate());
   Font titleFont = new Font(FontFamily.TIMES_ROMAN, 40.f, Font.NORMAL, BaseColor.BLUE);
   Font subTitleFont = new Font(FontFamily.TIMES_ROMAN, 25.f, Font.NORMAL, BaseColor.RED);
   Font dateFont = new Font(FontFamily.TIMES_ROMAN, 20.f, Font.NORMAL, BaseColor.GREEN);
   PdfWriter writer=null;
   try {
      writer=PdfWriter.getInstance(document,new FileOutputStream(filePath.getText()));
      writer.setPageEvent(new Footer(document));
      document.open();
      document.add(setParagraph("Stoker Monitor Report", titleFont));
      document.add(setParagraph(name.getText(), subTitleFont));
      document.add(setParagraph(date.getText(), dateFont));
      Rectangle page=writer.getPageSize();
      float sizeY=page.getHeight();
      float sizeX=page.getWidth();
      float scale=.5f;
      PdfContentByte cb=writer.getDirectContent();
      PdfTemplate tp=cb.createTemplate(sizeX,sizeY);
      PdfGraphics2D g2d=new PdfGraphics2D(cb,sizeX,sizeY);
      tp.setWidth(sizeX);
      tp.setHeight(sizeY);
      Chart.getInstance().getChart().draw(g2d, new java.awt.geom.Rectangle2D.Float(0, 0,sizeX,sizeY));
      g2d.dispose();
      Image image=Image.getInstance(tp);
      image.scaleToFit(sizeX*scale,sizeY*scale);
      image.setAbsolutePosition(sizeX,sizeY-20.f);
      document.add(image);
   } catch (DocumentException | IOException e1) {
      System.err.println("Unable to open " + filePath.getText() + " for writing");
      e1.printStackTrace();
   }
   document.close();

Unfortunately no matter what I use for scale or Y location, the chart starts at (0,0) and covers the entire page. My page title at the top is, I assume, completely obscured since it is missing. However, my page footer does display but the x-axis and label overlay it. What am I doing wrong in order to properly size and locate the chart? TIA.
gw1500se
 
Posts: 13
Joined: Tue Nov 21, 2017 3:36 pm

Re: Sizing and Locating a JFreeChart with ItextPDF

Postby paradoxoff » Thu Nov 23, 2017 3:44 pm

gw1500se wrote:Unfortunately no matter what I use for scale or Y location, the chart starts at (0,0) and covers the entire page.

I can't reproduce that. I have changed my code from the other thread and converted the PDf Template to an image. Both setting the absolute position and scaling changed the layout of the PDF. The image.scaleToFit(float, float) method did not behave as expected (the aspect ratio was for some reason maintained even the parameters of the scaleToFit-method had a different ratio than the width and height of the image), but other transformations worked as expected.
Note that I am using an older version of itext (2.0.6) .
Last edited by paradoxoff on Fri Nov 24, 2017 11:53 am, edited 1 time in total.
paradoxoff
 
Posts: 1619
Joined: Sat Feb 17, 2007 1:51 pm

Re: Sizing and Locating a JFreeChart with ItextPDF

Postby gw1500se » Thu Nov 23, 2017 4:09 pm

Thanks. I don't know where to go from here but I guess I'll just play with the parameters until something different happens.
gw1500se
 
Posts: 13
Joined: Tue Nov 21, 2017 3:36 pm

Re: Sizing and Locating a JFreeChart with ItextPDF

Postby paradoxoff » Thu Nov 23, 2017 4:23 pm

As a first debugging step, you can check the value for "scale". If that happens to be 1.0, then the chart will cover the entire page. In that case, I could imagine that the definition of a new location is simply ignored.
paradoxoff
 
Posts: 1619
Joined: Sat Feb 17, 2007 1:51 pm


Return to JFreeChart

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 7 guests