itext positioning text absolutely

2019-01-12 02:26发布

问题:

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?

回答1:

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


回答2:

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


回答3:

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


回答4:

In my case only this solution worked fine.

PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(FILE));

Add method

private void addTextData(Document document, String text) {
        PdfContentByte cb = pdfWriter.getDirectContent();
        cb.beginText();
        try {
            BaseFont f_cn = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            cb.setFontAndSize(f_cn, 40);

            float requiredX = 50;
            float requiredY = 50;
            Paint paint = new Paint();
            paint.setTextSize(40);
            Typeface typeface=Typeface.createFromAsset(getAssets(), "Helvetica.ttf");
            paint.setTypeface(typeface);
            paint.setColor(Color.BLACK);
            paint.setStyle(Paint.Style.FILL);

            Rect result = new Rect();
            paint.getTextBounds(text, 0, text.length(), result);
            Log.i("Text dimensions", "Width: "+result.width()+"-Height: "+result.height());
            float calculatedY = document.getPageSize().getHeight() - result.height() - requiredY;
            cb.setTextMatrix(requiredX, calculatedY);
            cb.showText(text);
            cb.endText();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
}

This will help you for sure.



回答5:

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