如何连字文字? [英] How to hyphenate text?

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

问题描述

我用Java生成带有iText的PDF文件。
我的表格列具有固定的宽度和文本,这对于包含在单元格中的一行来说太长了。
但不使用连字符。 Leistungsscheinziffer这个词显示为:
Leistungssc //在这里打破
heinziffer

I generate an PDF file with iText in Java. My table columns have fixed widths and text, which is too long for one line is wrapped in the cell. But hyphenation is not used. The word "Leistungsscheinziffer" is shown as: Leistungssc //Break here heinziffer

我使用连字符的代码:

final PdfPTable table = new PdfPTable(sumCols);
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
table.getDefaultCell().setPadding(4f);
table.setWidths(widthsCols);
table.setWidthPercentage(100);
table.setSpacingBefore(0);
table.setSpacingAfter(5);

final Phrase result = new Phrase(text, font);
result.setHyphenation(new HyphenationAuto("de", "DE", 2,2));
final PdfPCell cell = new PdfPCell(table.getDefaultCell());
cell.setPhrase(result);
table.addCell(cell);
...

连字符被激活,以下测试结果为Lei-stungs-schein -zif-fer

Hyphen is activated and the following test results "Lei-stungs-schein-zif-fer "

Hyphenator h = new Hyphenator("de", "DE", 2, 2);
Hyphenation s = h.hyphenate("Leistungsscheinziffer"); 
System.out.println(s);

有什么我忘记设置连字符在那里工作的表吗?
感谢您的帮助。如果您需要有关我的代码的更多信息,我会告诉您。

Is there anything I forgot to set to the table that hyphen is working there? Thanks for your help. If you need more information about my code, I will tell you.

推荐答案

首先是一个与问题无关的评论:你创建一个 PdfPCell 对象,但不要使用它。您将短语添加到表中而不是使用单元格对象。

First a remark that is irrelevant to the problem: you create a PdfPCell object, but you don't use it. You add the phrase to the table instead of using the cell object.

现在您的问题:通常连字符是设置在级别:

Now for your question: normally hyphenation is set on the Chunk level:

Chunk chunk = new Chunk("Leistungsscheinziffer");
chunk.setHyphenation(new HyphenationAuto("de", "DE", 2,2));
table.addCell(new Phrase(chunk));

如果要在短语 level,你可以这样做,但这只适用于所有后续添加的 Chunk 。它不适用于已存储在短语中的内容。换句话说:您需要创建一个空的 Phrase 对象,然后添加一个或多个 Chunk 对象:

If you want to set the hyphenation on the Phrase level, you can do so, but this will only work for all subsequent Chunks that are added. It won't work for the content that is already stored inside the Phrase. In other words: you need to create an empty Phrase object and then add one or more Chunk objects:

Phrase phrase = new Phrase();
phrase.setHyphenation(new HyphenationAuto("de", "DE", 2,2));
phrase.add(new Chunk("Leistungsscheinziffer"));

我根据你的代码做了一个例子( HyphenationExample ); Leistungsscheinziffer一词在生成的PDF中用连字符表示: hyphenation_table.pdf

I've made an example based on your code (HyphenationExample); the word "Leistungsscheinziffer" is hyphenated in the resulting PDF: hyphenation_table.pdf.

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

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