如何为iText文档中固定矩形的段落处理真正长的动态文本?
ColumnText ct = new ColumnText(canvas);
Font paragraphFont=new Font(baseFont,4.5f);
ct.setSimpleColumn(9, 70, 70, 95);
Paragraph paragraph=new Paragraph("REALLLLLLLLLLY LONGGGGGGGGGG text",paragraphFont);
ct.addElement(paragraph);
ct.go();发布于 2015-12-24 21:03:43
我已经在一个我命名为SimpleColumn的示例中复制/粘贴了您的代码片段
public void createPdf(String dest) throws IOException, DocumentException {
// step 1
Rectangle rect = new Rectangle(100, 120);
Document document = new Document(rect);
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
// step 3
document.open();
// step 4
PdfContentByte canvas = writer.getDirectContent();
BaseFont baseFont = BaseFont.createFont();
ColumnText ct = new ColumnText(canvas);
Font paragraphFont=new Font(baseFont,4.5f);
ct.setSimpleColumn(9, 70, 70, 95);
Paragraph paragraph = new Paragraph("REALLLLLLLLLLY LONGGGGGGGGGG text",paragraphFont);
ct.addElement(paragraph);
ct.go();
// step 5
document.close();
}这将生成文件simple_column.pdf

如您所见,文本正确显示在一个矩形内,其中左下角的坐标为x = 9; y = 70,右上角的坐标为x = 70, y = 95。文本不适合此矩形的宽度,因此它被换行(在空白字符处拆分,分布在两行上)。
这是当您想要在固定的矩形中呈现段落时处理它们的长度。如果段落不适合矩形,则段落的其余部分将存储在ColumnText对象中。您可以定义一个新的简单列(使用不同的坐标)来呈现段落的其余部分。
https://stackoverflow.com/questions/34445641
复制相似问题