Java itext 绝对定位文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1625455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:57:44  来源:igfitidea点击:

itext positioning text absolutely

java.netitextsharpitext

提问by Hyman

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?

在 itext 中,我有一个块/短语/段落(我不介意哪个),我想在页面上的其他位置放置一些位置,例如 300 x 200。我该怎么做?

采纳答案by Hyman

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

回答by aaronbartell

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

这是一个包含所有正确大小写和 try/catch 块的版本:

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

回答by JstnPwll

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

回答by Hiren Patel

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.

这肯定会帮助你。

回答by Saurabh Gaddelpalliwar

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