Itext 生成pdf 表格嵌套的时候,内表格与外表格上下有间隙
Itext 生成pdf 表格嵌套的时候,内表格与外表格上下有间隙
我不知道有什么好的处理办法,大家帮帮忙
public void createPDF(HttpServletResponse response) {
Document document = new Document();
StringBuffer script = new StringBuffer();
script.append("this.print({bUI: false,bSilent: true,bShrinkToFit: false});")
.append("\r\nthis.closeDoc();");
document.setPageSize(PageSize.A4);
try {
BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 设置中文字体
Font headFont0 = new Font(bfChinese, 10);
ByteArrayOutputStream ops = new ByteArrayOutputStream();
PdfWriter pdfwriter = PdfWriter.getInstance(document,ops);
document.open();
// pdfwriter.addJavaScript(script.toString());
pdfwriter.setViewerPreferences(PdfWriter.PageModeUseThumbs);
float[] widths = {474f};
float[] width1 = {74f,80f,20f,40f,20f,40f,20f,180f};
float[] width2 = {74f,170f,80f,150f};
float[] width3 = {74f,400f};
float[] width4 = {158f,158f,158f};
float[] width5 = {316f,158f};
PdfPTable table = new PdfPTable(widths);
table.setSpacingBefore(130f);// 设置表格上面空白宽度
table.setTotalWidth(474);
table.setLockedWidth(true);// 设置表格的宽度固定
table.setWidthPercentage(100f);
PdfPTable pt = null;
PdfPCell cell = null;
//1=====
cell = new PdfPCell(new Paragraph("", headFont0));
pt = this.createTable(width1, 0f, 474f, true);
cell.setBorderWidth(0.1f);
cell.setFixedHeight(14);
pt.addCell(cell);
pt.addCell(cell);
pt.addCell(cell);
pt.addCell(cell);
pt.addCell(cell);
pt.addCell(cell);
pt.addCell(cell);
pt.addCell(cell);
table.addCell(pt);
//2=====
pt = this.createTable(width2, 0f, 474f, true);
cell.setFixedHeight(14);
pt.addCell(cell);
pt.addCell(cell);
pt.addCell(cell);
pt.addCell(cell);
table.addCell(pt);
//3=====
pt = this.createTable(width3, 0f, 474f, true);
pt.addCell(cell);
pt.addCell(cell);
table.addCell(pt);
//4=====
pt = this.createTable(width4, 0f, 474f, true);
pt.addCell(cell);
pt.addCell(cell);
pt.addCell(cell);
table.addCell(pt);
//5=====
pt = this.createTable(width4, 0f, 474f, true);
cell.setFixedHeight(100);
pt.addCell(cell);
pt.addCell(cell);
pt.addCell(cell);
table.addCell(pt);
//6=====
pt = this.createTable(width5, 0f, 474f, true);
cell.setFixedHeight(14);
pt.addCell(cell);
pt.addCell(cell);
table.addCell(pt);
//7=====
cell = new PdfPCell(new Paragraph("", headFont0));
cell.setFixedHeight(14);
table.addCell(cell);
//8=====
pt = this.createTable(width5, 0f, 474f, true);
cell.setFixedHeight(14);
pt.addCell(cell);
pt.addCell(cell);
table.addCell(pt);
document.add(table);
document.close();
response.setContentType("application/pdf");
response.setContentLength(ops.size());
ServletOutputStream out = response.getOutputStream();
ops.writeTo(out);
ops.write(1);
out.flush();
//end
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
}
public PdfPTable createTable(float[] f,float spacingBefore,float totalWidth,boolean lockedWidth){
PdfPTable table = new PdfPTable(f);
table.setSpacingBefore(spacingBefore);
table.setTotalWidth(totalWidth);
table.setLockedWidth(true);
return table;
}
}
[解决办法]
结不了贴 害的我又申请了个号