java生成PDF文档(二)

2014-11-24 02:29:10 · 作者: · 浏览: 1
tColspan(colspan);
cell.setPhrase(new Phrase(value,font));
cell.setPadding(3.0f);
if(!boderFlag){
cell.setBorder(0);
cell.setPaddingTop(15.0f);
cell.setPaddingBottom(8.0f);
}
return cell;
}
public PdfPTable createTable(int colNumber){
PdfPTable table = new PdfPTable(colNumber);
try{
table.setTotalWidth(maxWidth);
table.setLockedWidth(true);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setBorder(1);
}catch(Exception e){
e.printStackTrace();
}
return table;
}
public PdfPTable createTable(float[] widths){
PdfPTable table = new PdfPTable(widths);
try{
table.setTotalWidth(maxWidth);
table.setLockedWidth(true);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setBorder(1);
}catch(Exception e){
e.printStackTrace();
}
return table;
}

public PdfPTable createBlankTable(){
PdfPTable table = new PdfPTable(1);
table.getDefaultCell().setBorder(0);
table.addCell(createCell("", keyfont));
table.setSpacingAfter(20.0f);
table.setSpacingBefore(20.0f);
return table;
}

public void generatePDF() throws Exception{
PdfPTable table = createTable(4);
table.addCell(createCell("学生信息列表:", keyfont,Element.ALIGN_LEFT,4,false));

table.addCell(createCell("姓名", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("年龄", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("性别", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("住址", keyfont, Element.ALIGN_CENTER));

for(int i=0;i<5;i++){
table.addCell(createCell("姓名"+i, textfont));
table.addCell(createCell(i+15+"", textfont));
table.addCell(createCell((i%2==0) "男":"女", textfont));
table.addCell(createCell("地址"+i, textfont));
}
document.add(table);

document.close();
}

public static void main(String[] args) throws Exception {
File file = new File("D:\\text.pdf");
file.createNewFile();
new PDFReport(file).generatePDF();
}


}

本文出自“greatjone”