#1
  1. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,392
    Rep Power
    631

    Positioning Image with ItextPDF


    I have been struggling to place a JFreeChart in my PDF document. After lots of gnashing I finally have the chart but am unable to position it. I started on the jfree.org forum and they took me as far as they could. I now need some help with the itext part of my program. This is the thread that got me here. I'll re-post my code here so it is not necessary to switch between sites to see it:
    java Code:
     
           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=.7f;
              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();
              Image image=Image.getInstance(tp);
              image.setAbsolutePosition(sizeX, sizeY-200.f);
              document.add(image);
           } catch (DocumentException | IOException e1) {
              System.err.println("Unable to open " + filePath.getText() + " for writing");
              e1.printStackTrace();
           }
           document.close();

    My attempt to position the image fails because no matter what I do with 'image.setAbsolutePosition(sizeX, sizeY-200.f);' the lower left of the image is always positioned on the lower left of the page. Can someone help? TIA.
    Last edited by gw1500se; November 24th, 2017 at 11:37 AM.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,392
    Rep Power
    631
    In case anyone else runs into this problem is was a DUMB mistake. I set the 2D graphics using the byte content rather than the template. The correct code is:
    Code:
    PdfGraphics2D g2d=new PdfGraphics2D(tp,sizeX*scale+1,sizeY*scale+1);
    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo