Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

itext positioning text absolutely

Tags:

In itext I have a chunk/phrase/paragraph (I dont mind which) and I want to position some where else on the page e.g. at 300 x 200. How would I do this?

like image 870
Jack Avatar asked Oct 26 '09 15:10

Jack


3 Answers

In the end I wrote my own method to do it.

private void PlaceChunck(String text, int x, int y) {
        PdfContentByte cb = writer.DirectContent;
        BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        cb.SaveState();
        cb.BeginText();
        cb.MoveText(x, y);
        cb.SetFontAndSize(bf, 12);
        cb.ShowText(text);
        cb.EndText();
        cb.RestoreState();
    }
like image 162
Jack Avatar answered Sep 30 '22 14:09

Jack


Here's a version with all the correct casing and try/catch block:

  private static void absText(String text, int x, int y) {
    try {
      PdfContentByte cb = writer.getDirectContent();
      BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
      cb.saveState();
      cb.beginText();
      cb.moveText(x, y);
      cb.setFontAndSize(bf, 12);
      cb.showText(text);
      cb.endText();
      cb.restoreState();
    } catch (DocumentException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
like image 24
aaronbartell Avatar answered Sep 30 '22 12:09

aaronbartell


I did something along these lines, maybe it will help others:

ColumnText ct = new ColumnText(writer.getDirectContent());
ct.setSimpleColumn(left,bottom,right,top);
ct.setText(new Phrase("String"));
ct.go();
like image 34
JstnPwll Avatar answered Sep 30 '22 14:09

JstnPwll