如何指示具有某些字符(例如省略号)的用户,如果单元格中的文本延伸超过单元格的固定高度,则单元格中的文本将被截断。
pdfTable = new PdfPTable (1);
pdfTable.setWidthPercentage(100);
cell = new PdfPCell(new Paragraph("Label Heading", statementLabelFont));
pdfTable.addCell(cell);
StringBuffer str = new StringBuffer();
for (data : dataList) {
str.append(data != null ?data.getdata1() :"");
str.append(strSeparator);
}
cell = new PdfPCell();
cell.setFixedHeight(100f);
cell.setCellEvent(new TruncateContent(str.toString()));
pdfTable.addCell(cell);
static class TruncateContent implements PdfPCellEvent {
protected String content;
public TruncateContent(String content) {
this.content = content;
}
public void cellLayout(PdfPCell cell, Rectangle position,
PdfContentByte[] canvases) {
try {
BaseFont bf = BaseFont.createFont();
Font font = new Font(bf, 12);
float availableWidth = position.getWidth();
int contentLength = content.length();
int leftChar = 0;
int rightChar = contentLength - 1;
availableWidth -= bf.getWidthPoint("...", 12);
while (leftChar < contentLength && rightChar != leftChar) {
availableWidth -= bf.getWidthPoint(content.charAt(leftChar), 12);
if (availableWidth > 0)
leftChar++;
else
break;
availableWidth -= bf.getWidthPoint(content.charAt(rightChar), 12);
if (availableWidth > 0)
rightChar--;
else
break;
}
String newContent = content.substring(0, leftChar) + "..." + content.substring(rightChar);
PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
ColumnText ct = new ColumnText(canvas);
ct.setSimpleColumn(position.getLeft(), position.getBottom(), position.getRight(), position.getTop());
ct.addElement(new Paragraph(newContent, font));
ct.go();
} catch (DocumentException e) {
throw new ExceptionConverter(e);
} catch (IOException e) {
throw new ExceptionConverter(e);
}
}
}