Java 边界框中的 itext 多行文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1678298/
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 21:19:03  来源:igfitidea点击:

itext multiline text in bounding box

javatextpositionitextmultiline

提问by KarolDepka

Does anyone know, how to, in iText, add multiline text in bounding box (with coordinates specified).

有谁知道,如何在 iText 中,在边界框中添加多行文本(指定坐标)。

I tried

我试过

cb.showTextAligned(
    PdfContentByte.ALIGN_LEFT,
    text,
    bounds.getLeft(),
    TOTAL_HEIGHT-bounds.getTop(),
    0 );

But it does not support newlines. I also tried

但它不支持换行。我也试过

PdfContentByte cb = writer.getDirectContent();
cb.moveText(300,400);
document.add(new Paragraph("TEST paragraph\nNewline"));

This supports newlines but does not react to moveText, so I don't know how to put it at given position or better: bounding box.

这支持换行符,但对 moveText 没有反应,所以我不知道如何将它放在给定的位置或更好的位置:边界框。

I suspect chunks or PdfTemplate or maybe table might help, but i don't (yet) know how to put it together. TIA for help.

我怀疑块或 PdfTemplate 或表格可能会有所帮助,但我(尚)不知道如何将它们组合在一起。TIA 寻求帮助。

采纳答案by Yannick Smits

Try this:

尝试这个:

ColumnText ct = new ColumnText(cb);
Phrase myText = new Phrase("TEST paragraph\nAfter Newline");
ct.setSimpleColumn(myText, 34, 750, 580, 317, 15, Element.ALIGN_LEFT);
ct.go();

parameters of SetSimpleColumn are:

SetSimpleColumn 的参数是:

  1. the phrase
  2. the lower left x corner (left)
  3. the lower left y corner (bottom)
  4. the upper right x corner (right)
  5. the upper right y corner (top)
  6. line height (leading)
  7. alignment.
  1. 词组
  2. 左下角 x 角(左)
  3. 左下角y角(底部)
  4. 右上角 x 角(右)
  5. 右上角 y 角(顶部)
  6. 行高(前导)
  7. 结盟。

回答by user1269737

ColumnText ct = new ColumnText(content);
ct.setSimpleColumn(
    new Phrase("Very Long Text"),
    left=20, bottom=100, right=500, top=500,
    fontSize=18, Element.ALIGN_JUSTIFIED);
ct.go(); // for drawing