itext positioning text absolutely

Viewed 39708

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?

5 Answers

Hope this help you! Here is my code...

 Document document = new Document(PageSize.A4);
 PdfWriter writer = PdfWriter.getInstance(document, output);
 document.open();

 FixText("Add Your Text",400,700,writer,14);
 document.close();

Add Function:

  private static void FixText(String text, int x, int y,PdfWriter writer,int size) {
    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, size);
        cb.showText(text);
        cb.endText();
        cb.restoreState();
    } catch (DocumentException | IOException e) {
        e.printStackTrace();
    }
}
Related