又是好久没写博客了,今天来记录一下心得。话不多说,上才艺。
使用场景:导出的pdf中表格内的单元格数据出现数字自动换行。
![在这里插入图片描述](https://img-blog.csdnimg.cn/fe4efe534cab47c8862ebbba7a95d2d4.png)
处理方案:原先的PdfPCell赋值改成包装一层Chunk赋值,使用Chunk的setSplitCharacter方法。这里是否进行语句的分割方法,直接返回false。
public static PdfPCell productCellTwo(String value, Font font){
Chunk chunk = new Chunk(value,font);
chunk.setSplitCharacter(new OwnerSplitCharacter());
PdfPCell cell = new PdfPCell();
cell.addElement(chunk);
// 水平居中
// cell.setHorizontalAlignment(Element.ALIGN_CENTER);
// 垂直居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
// 动态高度
if(StringUtils.isNotEmpty(value)){
if(value.length() <= 72){
cell.setFixedHeight(20);
} else {
int height = (value.length()/36 + 1) * 10;
cell.setFixedHeight(height);
}
}
return cell;
}
private static class OwnerSplitCharacter implements SplitCharacter {
@Override
public boolean isSplitCharacter(int i, int i1, int i2, char[] chars, PdfChunk[] pdfChunks) {
return false;
}
}