如何计算java中段落的行? [英] how to count lines of a paragraph in java?

查看:81
本文介绍了如何计算java中段落的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个程序,我创建了一个列并在该列中插入了一个段落。

I have developed a program and I created a column and inserted a paragraph into that column.

ColumnText ct = new ColumnText(cb);
 Font font = new Font(bfBold.createFont());
 //float maxFontSize=10;
 font.setSize(10);
 Paragraph p = new Paragraph("This is a long paragraph that doesn't fit the width we defined for the simple column of the ColumnText object, so it will be distributed over several lines (and we don't know in advance how many).",font);
 p.setAlignment(Element.ALIGN_JUSTIFIED);
    //LineNumberReader lineNumberReader = new LineNumberReader(new StringReader());
    ct.setSimpleColumn(100f, 550f, 395f, 150f);

    ct.addElement(p);
    ct.go();

我已将段落插入 ct(列文本)

所以现在我想计算这一段的行。
那么我该怎么做?

So now I wanted to count lines of this paragraph. So how can I do that?

我使用了itext api。我在这里生成一个pdf。

I used itext api. I'm generating a pdf here.

推荐答案

ColumnText 渲染(无论是真实模式还是模拟模式),您可以使用 getLinesWritten()方法获取已写入的行数。

After the content of the ColumnText is rendered (either for real, or in simulation mode), you can use the getLinesWritten() method to get the number of lines that were written.

我使用的链接点在iText API文档中。您还可以在常见问题解答中找到此信息:如何获取渲染的尺寸文本?和iText in Action一书中的例子;请参阅 MovieColumns1 示例。

The link I used points at the iText API documentation. You'll also find this information in the FAQ: How to get the rendered dimensions of text? and in the examples of the "iText in Action" book; see the MovieColumns1 example.

我很抱歉仅限链接的答案(在StackOverflow上通常不受欢迎),但唯一需要的代码行是:

I'm sorry for the "link-only" answer (which is usually not appreciated on StackOverflow), but the only line of code that is needed, is:

int lines = ct.getLinesWritten();

这篇关于如何计算java中段落的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆