iText - 如何获取文本的渲染尺寸? [英] iText -- How do I get the rendered dimensions of text?

查看:672
本文介绍了iText - 如何获取文本的渲染尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到有关PdfPCell中文本布局的信息。我知道BaseFont.getWidthPointKerned(),但我正在寻找更详细的信息,如:

I would like to find out information about the layout of text in a PdfPCell. I'm aware of BaseFont.getWidthPointKerned(), but I'm looking for more detailed information like:


  1. 多少行如果在给定宽度的单元格(例如,30pt)中渲染,是否需要字符串? PdfPCell的高点是多少?

  1. How many lines would a string need if rendered in a cell of a given width (say, 30pt)? What would the height in points of the PdfPCell be?

给我一​​个适合给定宽度和高度的单元格的字符串的前缀或后缀。也就是说,如果我必须在宽度为12pt且高度为20pt的PdfPCell中以特定字体呈现文本今天是美好的一天,那么该字符串的哪一部分将适合可用空间?

Give me the prefix or suffix of a string that fits in a cell of a given width and height. That is, if I have to render the text "Today is a good day to die" in a specific font in a PdfPCell of width 12pt and height 20pt, what portion of the string would fit in the available space?

当要求在给定宽度的单元格中呈现给定字符串时,iText会在哪里中断?

Where does iText break a given string when asked to render it in a cell of a given width?

这与iText 2.1.6有关。谢谢。

This is with regard to iText 2.1.6. Thanks.

推荐答案

iText使用 ColumnText 类将内容呈现给细胞。我在第98-99页的书中解释了这一点。这意味着,就像 ColumnText 一样,您需要区分文本模式复合模式

iText uses the ColumnText class to render content to a cell. This is explained in my book on page 98-99. This means that, just like with ColumnText, you need to make the distinction between text mode and composite mode.

在任何情况下, ColumnText 测量字符的宽度并测试它们是否适合可用宽度。如果没有,则文本被拆分。您可以通过不同的方式更改拆分行为:通过引入连字符或定义自定义拆分字符。

In any case, ColumnText measures the width of the characters and tests if they fit the available width. If not, the text is split. You can change the split behavior in different ways: by introducing hyphenation or by defining a custom split character.

我写了一个小概念证明来说明你怎么做实现自定义截断行为。请参阅 TruncateTextInCell 示例。

I've written a small proof of concept to show how you could implement custom "truncation" behavior. See the TruncateTextInCell example.

我没有将内容添加到单元格,而是有一个空单元格,我为其定义了一个单元格事件。我传递了长文本D2是一个内容超出我们可以放入单元格的单元格。来自此事件。

Instead of adding the content to the cell, I have an empty cell for which I define a cell event. I pass the long text "D2 is a cell with more content than we can fit into the cell." to this event.

在这种情况下,我使用了一种奇特的算法:我希望文本在中间被截断,并在我截断文本的地方插入...。

In the event, I use a fancy algorithm: I want the text to be truncated in the middle and insert "..." at the place where I truncated the text.

BaseFont bf = BaseFont.createFont();
Font font = new Font(bf, 12);
float availableWidth = position.getWidth();
int contentLength = content.length();
int leftChar = 0;
int rightChar = contentLength - 1;
availableWidth -= bf.getWidthPoint("...", 12);
while (leftChar < contentLength && rightChar != leftChar) {
    availableWidth -= bf.getWidthPoint(content.charAt(leftChar), 12);
    if (availableWidth > 0)
        leftChar++;
    else
        break;
    availableWidth -= bf.getWidthPoint(content.charAt(rightChar), 12);
    if (availableWidth > 0)
        rightChar--;
    else
        break;
    }
    String newContent = content.substring(0, leftChar) + "..." + content.substring(rightChar);
    PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
    ColumnText ct = new ColumnText(canvas);
    ct.setSimpleColumn(position);
    ct.addElement(new Paragraph(newContent, font));
    ct.go();

如您所见,我们从位置获得可用宽度参数,我们检查多少个字符匹配,在开头的字符和内容结尾的字符之间交替。

As you can see, we get the available width from the position parameter and we check how many characters match, alternating between a character at the start and a character at the end of the content.

结果显示在生成的PDF 中:内容被截断为:D2是一个c ...单元格。

The result is shown in the resulting PDF: the content is truncated like this: "D2 is a c... the cell."

关于多少行的问题可以通过类似方法解决办法。 ColumnText 类有一个 getLinesWritten()方法,可以为您提供该信息。您可以在我对其他问题的回答中找到有关定位 ColumnText 对象的更多信息:我可以告诉iText如何剪辑文本以适应单元格

Your question about "how many lines" can be solved in a similar way. The ColumnText class has a getLinesWritten() method that gives you that information. You can find more info about positioning a ColumnText object in my answer to your other question: Can I tell iText how to clip text to fit in a cell

这篇关于iText - 如何获取文本的渲染尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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