Java iText-5.0.1+;用虚线绘制PdfPTable的边框
在iText-5.0.1中,有没有办法用虚线(如UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUJava iText-5.0.1+;用虚线绘制PdfPTable的边框,java,itext,Java,Itext,在iText-5.0.1中,有没有办法用虚线(如UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU 您也可以使用PdfPCellEvent自己绘制边界。有不同的层要添加到。请参见此处的API:正如建议的那样,使用PdfPC
您也可以使用PdfPCellEvent自己绘制边界。有不同的层要添加到。请参见此处的API:正如建议的那样,使用PdfPCellEvent。下面的代码应该可以帮助您完成大部分任务。 通过覆盖单元格事件,您基本上可以告诉iText您认为它应该如何绘制单元格。无论何时将任何单元格添加到表中,它们都将遵循您的规则
class CustomCell implements PdfPCellEvent {
public void cellLayout(PdfPCell cell, Rectangle rect,
PdfContentByte[] canvas) {
PdfContentByte cb = canvas[PdfPTable.LINECANVAS];
cb.setLineDash(new float[] {3.0f, 3.0f}, 0);
cb.stroke();
}
}
public class Main {
public static void main(String[] args) throws Exception {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("output.pdf"));
document.open();
CustomCell border = new CustomCell();
PdfPTable table = new PdfPTable(6);
PdfPCell cell;
for (int i = 1; i <= 6; i++) {
cell = new PdfPCell(new Phrase("test"));
cell.setCellEvent(border);
table.addCell(cell);
}
document.add(table);
document.close();
}
}
类CustomCell实现PdfPCellEvent{
公共空心单元布局(PdfPCell单元,矩形矩形,
PdfContentByte[]画布){
PdfContentByte cb=canvas[PdfPTable.LINECANVAS];
cb.setLineDash(新浮点[]{3.0f,3.0f},0);
cb.stroke();
}
}
公共班机{
公共静态void main(字符串[]args)引发异常{
文档=新文档();
getInstance(文档,新文件outputStream(“output.pdf”);
document.open();
CustomCell边框=新的CustomCell();
PdfPTable=新的PdfPTable(6);
PDFP细胞;
对于(int i=1;i带破折号的单元格:
public class UnderlinedCell implements PdfPCellEvent {
public void cellLayout(PdfPCell cell, Rectangle position,
PdfContentByte[] canvases) {
PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
canvas.setLineWidth(0.5f);
canvas.setLineDash(3f, 3f);
canvas.moveTo(position.getLeft(), position.getBottom());
canvas.lineTo(position.getRight(), position.getBottom());
canvas.stroke();
}
}
我想我们无法设置单元格的高度?我在尝试您的代码时通过E-Clipse收到了以下错误消息…“无法访问pdf类型的封闭实例…”知道发生了什么吗?
PdfPCell Border1 = new PdfPCell(new Paragraph("-----------------------------------------------------------------------------------------------------------------------"));
Border1.Border = 0;
Border1.VerticalAlignment = 3;
Border1.FixedHeight = 5F;
Border1.PaddingLeft = -5;
Border1.PaddingRight = -5;
Border1.PaddingBottom = -5;
Border1.PaddingTop = -5;
public class UnderlinedCell implements PdfPCellEvent {
public void cellLayout(PdfPCell cell, Rectangle position,
PdfContentByte[] canvases) {
PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
canvas.setLineWidth(0.5f);
canvas.setLineDash(3f, 3f);
canvas.moveTo(position.getLeft(), position.getBottom());
canvas.lineTo(position.getRight(), position.getBottom());
canvas.stroke();
}
}