1.生成pdf代码
package test;
import java.io.FileOutputStream;
import test.sd.PdfEvent;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.Barcode;
import com.itextpdf.text.pdf.Barcode128;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class TestPdf {
@org.junit.Test
public void fun() {
try {
String fileName = "F://sdasdaddd.pdf";
String barCode1 = "98746221556666";
String appntName = "张三";
String contNo = "05465132";
String edorAppDate = "2018年11月12日";//受理时间
String edorTypeName = "退保";//申请项目
String bankName = "";//授权银行
String accName = "收到的大";//账户名
String bankAccNo = "";//授权账号
String money = "";//转账金额
String transferFailReason = "";//转账不成功原因
// 初始化Document对象
Document pdfDocument = new Document(PageSize.A4, 72, 72, 72, 54);
PdfWriter writer = PdfWriter.getInstance(pdfDocument, new FileOutputStream(fileName));
// 打开Document对象
pdfDocument.open();
// 设置字体
BaseFont bSong = BaseFont.createFont("F://simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font fSong9 = new Font(bSong, 9);
// 创建页面事件
PdfEvent event = new PdfEvent(bSong);
writer.setPageEvent(event);
// 画布对象,绝对位置输出内容
PdfContentByte cb = writer.getDirectContent();
//logo
Image logoImage = Image.getInstance("F://guolianLogo.jpg");
logoImage.setAbsolutePosition(24, 780);
logoImage.scaleAbsolute(160, 36);
cb.addImage(logoImage);
//条形码
Barcode barcode=new Barcode128();
barcode.setCode(barCode1);
Image barcodeImage = barcode.createImageWithBarcode(cb, null, null);
barcodeImage.setAbsolutePosition(432, 785);
barcodeImage.scaleAbsolute(140, 30);
cb.addImage(barcodeImage);
Paragraph paragraph = new Paragraph();
paragraph.setAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
paragraph.add(new Phrase("\n\n\n转账不成功通知书\n\n", new Font(bSong, 16 ,Font.BOLD)));
pdfDocument.add(paragraph);
paragraph.clear();
paragraph.setAlignment(com.itextpdf.text.Element.ALIGN_LEFT);
paragraph.setLeading(1f,1.2f);
paragraph.add(new Phrase("尊敬的" + appntName + ":\n" +
" 您在办理保单号" + contNo + "保全业务时," +
"提供的银行账户转账不成功,请核对如下银行及账户信息。如银行或账户信息有误,请将正确的信息如实填写," +
"并将本通知书交至我公司客户服务中心办理相关手续。\n" +
" 为了保障您的利益不受损失,务必请在本通知书上亲笔签名,使之具有法律效力。", fSong9));
pdfDocument.add(paragraph);
PdfPTable table = new PdfPTable(1);
table.setSpacingBefore(8f);
table.getDefaultCell().setLeading(1f, 1.5f);
table.getDefaultCell().setPadding(5f);
table.getDefaultCell().setPaddingTop(-2f);
table.setWidthPercentage(100);
Phrase phrase = new Phrase();
phrase.add(new Phrase(" 转账不成功信息", new Font(bSong, 12.5f, Font.BOLD)));
phrase.add(new Phrase("\n受理时间:" + edorAppDate + "\n" +
"申请项目:" + edorTypeName + "\n" +
"授权银行:" + bankName + "\n" +
"账户名:" + accName + "\n" +
"授权账号:" + bankAccNo + "\n" +
"转账金额:" + money + "元\n" +
"转账不成功原因:" + transferFailReason, fSong9));
table.addCell(phrase);
phrase = new Phrase();
phrase.add(new Phrase(" 回复如下", new Font(bSong, 12.5f, Font.BOLD)));
phrase.add(new Phrase("\n声明,上述保全业务使用的授权银行信息:\n" +
"□授权账号正确,继续转账\n" +
"□授权账号变更为:\n" +
"授权账号所在省_____________市__________________ 授权银行______________________ \n" +
"账户名______________________________\n" +
"授权账号 ", fSong9));
table.addCell(phrase);
pdfDocument.add(table);
table = new PdfPTable(20);
table.getDefaultCell().setMinimumHeight(20f);
table.setSpacingBefore(0f);
for (int i = 0; i < 20; i++) {
table.addCell(" ");
}
table.setWidthPercentage(100);
pdfDocument.add(table);
table = new PdfPTable(1);
table.setSpacingBefore(10f);
table.setWidthPercentage(100);
table.getDefaultCell().setPadding(5f);
table.getDefaultCell().setPaddingTop(-2f);
table.getDefaultCell().setLeading(1f, 1.5f);
table.addCell(new Phrase("□其他:__________________________________________________", fSong9));
pdfDocument.add(table);
paragraph.clear();
paragraph.setIndentationLeft(5f);
paragraph.add(new Phrase("请在收到本通知书之日起10日内给予回复,非常感谢您的配合与支持!\n" +
"如有疑问,请致电我公司客服电话95570或4008-888-000,或联系保单服务专员。\n\n\n\n\n\n", fSong9));
paragraph.add(new Phrase("申请人签字:___________________ 日期:_____ 年_____月_____日", fSong9));
pdfDocument.add(paragraph);
pdfDocument.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.pdfEvent
package com.sz.kcygl.common.util;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
public class PdfEvent extends PdfPageEventHelper
{
public PdfTemplate tpl;
public BaseFont baseFont;
public PdfEvent(BaseFont baseFont)
{
super();
this.baseFont = baseFont;
}
/* (non-Javadoc)
* 描述:页尾事件,添加页码
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(com.itextpdf.text.pdf.PdfWriter,
* com.itextpdf.text.Document)
*/
public void onEndPage(PdfWriter writer, Document document) {
int iPage = writer.getPageNumber();
PdfContentByte cb = writer.getDirectContent();
// 添加页码信息
String text = "第 " + Integer.toString(iPage) + " 页 ,共 页";
cb.beginText();
cb.setFontAndSize(baseFont, 9);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, text, document
.getPageSize().getWidth() / 2 + 1f, 36, 0);
cb.endText();
// 创建模板对象
if (tpl == null) {
tpl = writer.getDirectContent().createTemplate(20f, 15f);
}
// 定位“y页” 在具体的页面调试时候需要更改这xy的坐标
cb.addTemplate(tpl, (document.getPageSize().getWidth() / 2 + 21), 36);
}
/* (non-Javadoc)
* itext关闭页面时间
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onCloseDocument(com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
*/
public void onCloseDocument(PdfWriter writer, Document document)
{
//关闭document的时候获取总页数,并把总页数按模版写道之前预留的位置
if(tpl != null)
{
// 添加最后一份信函的总页数
tpl.beginText();
tpl.setFontAndSize(baseFont, 9);
tpl.showText(Integer.toString(writer.getPageNumber() - 1));
tpl.endText();
tpl.closePath();
}
}
}