iText段落溢出处理

3
如何处理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();

2
欢迎来到StackOverflow!您能否在问题中添加更多有关问题的细节以及您希望事情如何工作(换行、裁剪等)?也许您可以添加一个图像,展示您希望这样的文本是什么样子? - trincot
1
文本将自动换行以适应您定义的矩形。无法适应的文本将不会被呈现。它存储在列中,以便您可以在另一个位置添加它。您是否阅读了 iText FAQ - Bruno Lowagie
1个回答

1
我已经将您的代码片段复制/粘贴到一个示例中,我将其命名为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的生成:

enter image description here

正如您所看到的,文本被正确地显示在一个矩形内,其左下角坐标为x = 9; y = 70,右上角坐标为x = 70, y = 95。由于文本宽度超出了该矩形,因此被换行(在空格处分割并分布在两行中)。
当您想要在固定矩形中呈现长段落时,就是这样处理的。如果段落不适合矩形,则段落的剩余部分将存储在ColumnText对象中。您可以定义一个新的简单列(使用不同的坐标)来呈现段落的其余部分。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接