中文排版输出文件时,有的标点符号不能在行首如:“)”,“;”等,有的不能在行末如:“(”,“《”等....
word等软件中有默认设置,也可以很方便的修改设置对某些符号做特别处理。使用iText7输出中文文档时,就不那么方便了。
值得庆幸的是,iText默认输出虽然没有考虑中文的符号处理,但考虑了欧美文字的断句问题。我们有机会接管这块处理,自定义符号断句折行规则。
这个规则由接口
/**
* Interface for customizing the split character.
*/
public interface ISplitCharacters {
/**
* The splitting implementation is free to look ahead or look behind characters to make a decision.
* @param glyphPos the position of {@link Glyph} in the {@link GlyphLine}
* @param text an array of unicode char codes which represent current text
* @return true if the character can split a line.
*/
boolean isSplitCharacter(GlyphLine text, int glyphPos);
}
参数:GlyphLine包含当前要输出的文本,具体结构看文档
参数:glyhPos是当前输出的字符位置。
方法很简单,则iText在计算布局时,需要断句折行时,调用isSplitCharacter方法判断是否可以在当前字符位置断句折行。当返回ture时,可以折行。返回false时,则反之。
我们通过实现该接口,在当前字符后一个字符为不能在行首的标点符号时,在当前字符为不能在行末的标点符号时,均返回false,则阻断折行,即可实现标点符号行首行末的控制。
使用时,创建完Document后,调setSplitCharacters设置实例即可实现全文控制。
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(output));
Document document = new Document(pdfDocument, PageSize.A4);
document.setSplitCharacters(new ChineseSplitterCharacters());
完整实现参见资源((本人下资料比较猛,弄得很穷,没有积分了,见谅。锁定积分:2分))。
包含通常的较全的标点符号规则。