createPdf demo


private boolean createPdfCont() { try { /* 获取当日文件的存放路径,若不存在则创建文件路径 */ if(!getFilePath()) { return false; } /* 创建一个文件,并获得它的写入流,将其打开 */ Document tDoc = new Document(PageSize.A4, 50, 50, 50, 50); PdfWriter tWriter = PdfWriter.getInstance(tDoc, new FileOutputStream(mFileFullPath.toString())); tDoc.open(); /* 获得文本写入器 */ PdfContentByte tPcd = tWriter.getDirectContent(); /* 创建合同需要的所有基本字体 */ BaseFont tBFChinese= BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.EMBEDDED); mBoldConFont = new Font(tBFChinese, 10, Font.BOLD); mConFont = new Font(tBFChinese, 10); /*******************************************开始处理合同第一页**************************************/ /* 加载带有logo的图片 */ Image tImgCompany = Image.getInstance(mImgPath+"logo.jpg"); /* 设置logo的位置 */ tImgCompany.setAbsolutePosition(50, 500); /* 设置logo的大小 */ tImgCompany.scaleAbsolute(200, 50); /* 加载印有'保险合同'字样的图片 */ Image tImgCont = Image.getInstance(mImgPath+"cont.jpg"); /* 设置图片的位置*/ tImgCont.setAbsolutePosition(50, 430); /* 设置图片的大小 */ tImgCont.scaleAbsolute(280, 70); /* 将以上图片写入文件 */ tDoc.add(tImgCompany); tDoc.add(tImgCont); /* 设置第一页的文字显示信息 */ tPcd.beginText(); tPcd.setFontAndSize(tBFChinese, 10); /* 显示产品名称 */ tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT, this.mContPlanGrpName , 60, 420, 0); /* 显示合同号 */ tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"保险合同号:"+this.mContNo, 420, 387, 0); /* 显示投保人名字 */ tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"投 保 人:"+this.mName, 420, 367, 0); /* 显示页数 */ tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"第一页,共五页", 280, 50, 0); tPcd.endText(); /* 换页 */ tDoc.newPage(); System.out.println("第一页写入完成"); /*****************************************开始处理合同第二页**************************************/ /* 插入顶头图片 */ Image tImgComName = Image.getInstance(mImgPath+"companyname.jpg"); tImgComName.setAbsolutePosition(147, 750); tImgComName.scaleAbsolute(300, 50); tDoc.add(tImgComName); tPcd.beginText(); /* 写入保险单大标题 */ tPcd.setFontAndSize(tBFChinese, 20); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"保 险 单", 250, 710, 0); /* 设置正文行间距 */ int tHeight = 16; /* 设置正文最高像素 */ int tStartHeight = 700; /* 设置正文左边距*/ int tLeftWidth = 50; /*设置一行有两列的第二列左边距*/ int tRigthWidth = 370; /* 更改成小字体,开始写正文 */ tPcd.setFontAndSize(tBFChinese, 10); /* PDF中显示的正文文字信息 */ tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"币值单位:人民币元", tLeftWidth, tStartHeight-tHeight*1, 0); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"-------------------------------------------------------------------------------------------------------------------------------------------------", tLeftWidth, tStartHeight-tHeight*2, 0); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"保险合同号:"+this.mContNo, tLeftWidth, tStartHeight-tHeight*3, 0); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"合同生效日:"+this.mStartDate.substring(0,4)+""+this.mStartDate.substring(5,7)+""+this.mStartDate.substring(8,10)+"日0时",tRigthWidth, tStartHeight-tHeight*3, 0); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"-------------------------------------------------------------------------------------------------------------------------------------------------", tLeftWidth, tStartHeight-tHeight*4, 0); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"投保人", tLeftWidth, tStartHeight-tHeight*5, 0); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"姓名:"+this.mName, tLeftWidth, tStartHeight-tHeight*6, 0); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"性别:"+this.mSex, tRigthWidth, tStartHeight-tHeight*6, 0); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"出生日期:"+this.mBirtth.substring(0,4)+""+this.mBirtth.substring(5,7)+""+this.mBirtth.substring(8,10)+"", tLeftWidth, tStartHeight-tHeight*7, 0); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"证件号码:"+this.mIDNO, tRigthWidth, tStartHeight-tHeight*7, 0); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"通讯地址:"+this.mAddress, tLeftWidth, tStartHeight-tHeight*8, 0); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"邮政编码:"+this.mZipCode, tLeftWidth, tStartHeight-tHeight*9, 0); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"电子邮箱:"+this.mEmail, tRigthWidth, tStartHeight-tHeight*9, 0); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"-------------------------------------------------------------------------------------------------------------------------------------------------", tLeftWidth, tStartHeight-tHeight*10, 0); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"被保险人为投保人本人", tLeftWidth, tStartHeight-tHeight*11, 0); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"-------------------------------------------------------------------------------------------------------------------------------------------------", tLeftWidth, tStartHeight-tHeight*12, 0); this.mBnfNo = mSLCBnfSet.size(); for(int i=1;i<=this.mSLCBnfSet.size();i++) { tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"身故受益人:"+mSLCBnfSet.get(i).getName(), tLeftWidth, tStartHeight-tHeight*(12+i), 0); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"证件号码:"+mSLCBnfSet.get(i).getIDNo(), 160, tStartHeight-tHeight*(12+i), 0); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"受益顺序:"+i, 300, tStartHeight-tHeight*(12+i), 0); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"受益份额:"+mSLCBnfSet.get(i).getBnfLot()+"%",tRigthWidth,tStartHeight-tHeight*(12+i), 0); } tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"-------------------------------------------------------------------------------------------------------------------------------------------------", tLeftWidth, tStartHeight-tHeight*(13+this.mBnfNo), 0); tPcd.setFontAndSize(tBFChinese, 10); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"君龙守护保障计划", tLeftWidth, tStartHeight-tHeight*(14+this.mBnfNo), 0);[color=#FF0000] --(这里想弄成粗体)[/color] mTable = this.getItemTable(); /* 按指定位置写入表格 */ mTable.writeSelectedRows(0, -1,tLeftWidth ,tStartHeight-tHeight*(14.2f+this.mBnfNo),tPcd); /* 准备写入合同尾部信息 */ tPcd.beginText(); tPcd.setFontAndSize(tBFChinese, 10); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,this.mPrem, tLeftWidth, tStartHeight-tHeight*(16.5f+this.mBnfNo+this.mItemRow), 0); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"具体保险责任以条款所载内容为准。", tLeftWidth, tStartHeight-tHeight*(17.5f+this.mBnfNo+this.mItemRow), 0); if((tStartHeight-tHeight*(17.5f+this.mBnfNo+this.mItemRow))<= 150 + tHeight*3) { System.out.println("警告:此页信息有可能因为篇幅问题而发生重叠!"); } tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"-------------------------------------------------------------------------------------------------------------------------------------------------", tLeftWidth, 150 + tHeight*3, 0); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"签发机构:"+this.mComCode, tLeftWidth, 150 + tHeight*2, 0); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"合同成立日期:"+this.mStartDate.substring(0,4)+""+this.mStartDate.substring(5,7)+""+this.mStartDate.substring(8,10)+"", tRigthWidth, 150 + tHeight*2, 0); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"服务地址:"+this.mServiceAddr, tLeftWidth, 150 + tHeight*1, 0); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"邮政编码:"+this.mSerAddZipCode, tRigthWidth, 150 + tHeight*1, 0); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"全国服务电话:400-666-0123", tLeftWidth, 150, 0); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"网址:www.kdlins.com.cn", tRigthWidth, 150, 0); tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"第二页,共五页", 280, 50, 0); tPcd.endText(); /* 插入图章图片 */ Image tImgStamp = Image.getInstance(mImgPath+"stamp.jpg"); tImgStamp.setAbsolutePosition(380, 70); tImgStamp.scaleAbsolute(130, 120); tDoc.add(tImgStamp); tDoc.newPage(); System.out.println("第2页写入完成");

 

转载自

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值