生成pdf文件

public void createPDF() throws Exception{
Document document=new Document(PageSize.A4.rotate());
// ByteArrayOutputStream out=new ByteArrayOutputStream();
try {

PdfWriter.getInstance(document, new FileOutputStream("D:/test.pdf"));
document.open();

Paragraph title= new Paragraph("反反复复反反复复反反复复证明",FontCN.getTitleFont());

//大标题--文本标题

title.setAlignment(1);  //对齐方式  1:居中  2:居右  3:居左  默认为左对齐
document.add(title);
//个人信息
PdfPTable tablet=new PdfPTable(5);
tablet.setWidthPercentage(100f);
float [] f = new float[]{15f,30f,50f,25f,30f};
tablet.setTotalWidth(f);


this.addCell(tablet, "姓名:yy", 1, 0, 20, FontCN.getNormalFont(), Element.ALIGN_LEFT, 0, 0, 0, 0);
this.addCell(tablet, "反反复复:12345679210", 1, 0, 20, FontCN.getNormalFont(), Element.ALIGN_LEFT, 0, 0, 0, 0);
this.addCell(tablet, "反反复复(身份证号码):410422199206198612", 1, 0, 20, FontCN.getNormalFont(), Element.ALIGN_LEFT, 0, 0, 0, 0);
this.addCell(tablet, "反反复复:12346678", 1, 0, 20, FontCN.getNormalFont(), Element.ALIGN_LEFT, 0, 0, 0, 0);
this.addCell(tablet, "反反复复:2016年11月23日", 1, 0, 20, FontCN.getNormalFont(), Element.ALIGN_LEFT, 0, 0, 0, 0);

document.add(tablet);
Paragraph  pn = new Paragraph("验证码:",FontCN.getNormalFont());
document.add(pn);
//小标题 --表格标题
Paragraph  title2 = new Paragraph("反反复复凤飞飞凤飞飞缴费明细",FontCN.getFont());
title2.setAlignment(1);
title2.setSpacingAfter(5f);
document.add(title2);
//创建表格
PdfPTable table=new PdfPTable(16);
table.setWidthPercentage(100f);
//第一行
this.addCell(table, "反反复复", 1, 0, 30, FontCN.getSmallFont(), Element.ALIGN_CENTER, 1, 0, 1, 0);
this.addCell(table, "反反复复", 1, 0, 30, FontCN.getSmallFont(), Element.ALIGN_CENTER, 1, 0, 1, 0);
this.addCell(table, "反反复复", 3, 0, 30, FontCN.getSmallFont(), Element.ALIGN_CENTER, 1, 0, 1, 0);
this.addCell(table, "反反复复", 3, 0, 30, FontCN.getSmallFont(), Element.ALIGN_CENTER, 1, 0, 1, 0);
this.addCell(table, "反反复复", 3, 0, 30, FontCN.getSmallFont(), Element.ALIGN_CENTER, 1, 0, 1, 0);
this.addCell(table, "反反复复", 3, 0, 30, FontCN.getSmallFont(), Element.ALIGN_CENTER, 1, 0, 1, 0);
this.addCell(table, "反反复复", 2, 0, 30, FontCN.getSmallFont(), Element.ALIGN_CENTER, 1, 0, 1, 1);
//第二行
this.addCell(table, " ", 1, 0, 36, FontCN.getSmallFont(), Element.ALIGN_CENTER, 0, 1, 1, 0);
this.addCell(table, " ", 1, 0, 36, FontCN.getSmallFont(), Element.ALIGN_CENTER, 0, 1, 1, 0);
//"城镇职工养老保险"
this.addCell(table, "反反复复", 1, 0, 30, FontCN.getSmallFont(), Element.ALIGN_CENTER, 1, 1, 1, 0);
this.addCell(table, "反反复复", 1, 0, 30, FontCN.getSmallFont(), Element.ALIGN_CENTER, 1, 1, 1, 0);
this.addCell(table, "反反复复", 1, 0, 30, FontCN.getSmallFont(), Element.ALIGN_CENTER, 1, 1, 1, 0);
//"城镇职工医疗保险"
this.addCell(table, "反反复复", 1, 0, 30, FontCN.getSmallFont(), Element.ALIGN_CENTER, 1, 1, 1, 0);
this.addCell(table, "反反复复", 1, 0, 30, FontCN.getSmallFont(), Element.ALIGN_CENTER, 1, 1, 1, 0);
this.addCell(table, "反反复复", 1, 0, 30, FontCN.getSmallFont(), Element.ALIGN_CENTER, 1, 1, 1, 0);
//"生育保险"
this.addCell(table, "反反复复", 1, 0, 30, FontCN.getSmallFont(), Element.ALIGN_CENTER, 1, 1, 1, 0);
this.addCell(table, "反反复复", 1, 0, 30, FontCN.getSmallFont(), Element.ALIGN_CENTER, 1, 1, 1, 0);
this.addCell(table, "反反复复", 1, 0, 30, FontCN.getSmallFont(), Element.ALIGN_CENTER, 1, 1, 1, 0);
//"失业保险"
this.addCell(table, "反反复复", 1, 0, 30, FontCN.getSmallFont(), Element.ALIGN_CENTER, 1, 1, 1, 0);
this.addCell(table, "反反复复", 1, 0, 30, FontCN.getSmallFont(), Element.ALIGN_CENTER, 1, 1, 1, 0);
this.addCell(table, "反反复复", 1, 0, 30, FontCN.getSmallFont(), Element.ALIGN_CENTER, 1, 1, 1, 0);
//"工伤保险"
this.addCell(table, "反反复复", 1, 0, 30, FontCN.getSmallFont(), Element.ALIGN_CENTER, 1, 1, 1, 0);
this.addCell(table, "反反复复", 1, 0, 30, FontCN.getSmallFont(), Element.ALIGN_CENTER, 1, 1, 1, 1);
//标题栏结束
for (int i = 1; i < 25; i++) {
for (int j = 1; j < 17; j++) {
if(j==16){
this.addCell(table, " "+i+j, 1, 0, 30, FontCN.getSmallFont(), Element.ALIGN_CENTER, 0, 1, 1, 1);
}else {
this.addCell(table, " "+i+j, 1, 0, 30, FontCN.getSmallFont(), Element.ALIGN_CENTER, 0, 1, 1, 0);
}
}

}

document.add(table);

//尾部
Paragraph one  = new Paragraph("表格说明:1、缴费明细表中空格为未缴费或中断缴费。"
+ "2、缴费明细表中“单位编码”对应的单位名称为:543999养老保险个人户,可能多个单位。",FontCN.getSmallFont());
Paragraph  two = new Paragraph("验证说明:1、本证明采用电子验证方式,不再加盖红色公章,"
+ "如需要核对真伪,请登陆http://XXXX,凭本证明左上角的验证码验证。"
+ "2、本验证码的有效期至 2016年11月7日",FontCN.getSmallFont());
Paragraph ttwo = new Paragraph("3、本证明复印件有效,有效期内验证码可多次使用。4、验证码由个人妥善保管,慎防泄漏。"
+ "5、咨询电话:12333。",FontCN.getSmallFont());
ttwo.setFirstLineIndent(40f);
document.add(one);
document.add(two);
document.add(ttwo);
document.close();

} catch (DocumentException e) {
e.printStackTrace();
}




}



private void addCell(PdfPTable table,String content,int colspan, int border,int fixedHeight,Font font,int horizontalAlignment,int top,int bottom,int left,int right){
PdfPCell cell = new PdfPCell(new Paragraph(content, font));
cell.setColspan(colspan);
if(border==0){
cell.setBorder(0);
}
cell.setFixedHeight(20); //fixedHeight 可以更改 现在已写死
cell.setHorizontalAlignment(horizontalAlignment);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setBorderWidthTop(top);
cell.setBorderWidthBottom(bottom);
cell.setBorderWidthLeft(left);
cell.setBorderWidthRight(right);
table.addCell(cell);
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值