我可以告诉iText如何剪辑文本以适应单元格 [英] Can I tell iText how to clip text to fit in a cell
问题描述
当我在PdfPCell上调用setFixedHeight()并添加更多文本而不是给定高度时,iText似乎打印出适合的字符串的前缀。
When I call setFixedHeight() on a PdfPCell, and add more text than fits in the given height, iText seems to print the prefix of the string which fits.
我可以控制此裁剪算法吗?例如:
Can I control this clipping algorithm? For example:
-
打印字符串的后缀而不是前缀。
Print a suffix of the string rather than a prefix.
将字符串的子字符串标记为不删除。这是脚注参考。如果我添加Hello World [1]文本,则[1]是对脚注的引用,不应删除。可以删除字符串的其他字符,例如World。
Mark a substring of the string as not to be removed. This is with footnote references. If I add text saying "Hello World [1]", the [1] is a reference to a footnote and should not be removed. It's okay to remove the other characters of the string, like "World".
当字符串中有多个单词时,iText似乎消除了一个单词不适合,而我想部分打印。也就是说,如果字符串是Hello World,并且单元格只有Hello Wo ...的空间,我希望打印出来,而不仅仅是Hello,因为iText会打印出来。
When there are multiple words in the string, iText seems to eliminate a word that doesn't fit, while I would like it partially printed. That is, if the string is "Hello World", and the cell has room only for "Hello Wo...", I would like that to be printed, rather than just "Hello", as iText prints.
不是完整打印字符,而是只打印部分字符。想象一下,将文本打印到PNG并切掉PNG的顶部和/或底部以使其适合可用空间。例如,请注意顶部线和底线部分被剪切:
Rather than printing characters in their entirety, print only part of them. Imagine printing the text to a PNG and chopping off the top and/or bottom part of the PNG to fit it in the space available. For example, notice that the top line and the bottom line are partially clipped here:
这些都可能吗? iText是否可以控制文本的剪切方式?谢谢。
Are any of these possible? Does iText give me any control over how text is clipped? Thanks.
这是参考iText 2.1.6。
This is with reference to iText 2.1.6.
推荐答案
<我已经写了一个概念证明, ClipCenterCellContent ,我们试着在那里填写文本D2是一个包含更多内容的单元格。
在一个太小的单元格中。
I have written a proof of concept, ClipCenterCellContent, where we try to fit the text "D2 is a cell with more content than we can fit into the cell."
in a cell that is too small.
就像你的另一个问题一样( iText - - 如何获取文本的渲染尺寸?),我们使用单元格事件添加内容,但现在我们将其添加两次:一次在模拟模式下(以找出垂直需要多少空间)和一次for real(使用偏移量)。
Just like in your other question ( iText -- How do I get the rendered dimensions of text? ), we add the content using a cell event, but we now add it twice: once in simulation mode (to find out how much space is needed vertically) and once for real (using an offset).
这会在模拟模式中添加内容(我们使用单元格的宽度和任意高度):
This adds the content in simulation mode (we use the width of the cell and an arbitrary height):
PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
ColumnText ct = new ColumnText(canvas);
ct.setSimpleColumn(new Rectangle(0, 0, position.getWidth(), -1000));
ct.addElement(content);
ct.go(true);
float spaceneeded = 0 - ct.getYLine();
System.out.println(String.format("The content requires %s pt whereas the height is %s pt.", spaceneeded, position.getHeight()));
我们现在知道所需的高度,我们可以使用偏移量添加实际内容:
We now know the needed height and we can add the content for real using an offset:
float offset = (position.getHeight() - spaceneeded) / 2;
System.out.println(String.format("The difference is %s pt; we'll need an offset of %s pt.", -2f * offset, offset));
PdfTemplate tmp = canvas.createTemplate(position.getWidth(), position.getHeight());
ct = new ColumnText(tmp);
ct.setSimpleColumn(0, offset, position.getWidth(), offset + spaceneeded);
ct.addElement(content);
ct.go();
canvas.addTemplate(tmp, position.getLeft(), position.getBottom());
在这种情况下,我使用了 PdfTemplate
剪辑内容。
In this case, I used a PdfTemplate
to clip the content.
我也有其他问题的答案,但我现在没有时间回答。
I also have answers to your other questions, but I don't have the time to answer them right now.
这篇关于我可以告诉iText如何剪辑文本以适应单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!