4

我用 Java 中的 iText 生成 PDF 文件。我的表格列具有固定的宽度和文本,这对于单元格中的一行来说太长了。但不使用连字符。“Leistungsscheinziffer”一词显示为: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-ziff-fer”

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

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

4

1 回答 1

6

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

现在问您的问题:通常在Chunk级别上设置断字:

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

如果要在Phrase关卡上设置连字符,可以这样做,但这仅适用于所有后续Chunk添加的 s。它不适用于已存储在Phrase. 换句话说:您需要创建一个空Phrase对象,然后添加一个或多个Chunk对象:

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

我根据您的代码(HyphenationExample)制作了一个示例;“Leistungsscheinziffer”一词在生成的 PDF 中连字符:hyphenation_table.pdf

于 2013-11-21T14:39:12.787 回答