itext positioning text absolutely

前端 未结 5 1370
温柔的废话
温柔的废话 2020-12-03 17:00

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条回答
  •  悲&欢浪女
    2020-12-03 17:46

    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();
        }
    }
    

提交回复
热议问题