package com.ymy.util;/*@author ymy
@DESCRIPTION ${DESCRIPTION}
@create 2019/4/12*/
import com.ymy.model.*;importjava.io.ByteArrayOutputStream;importjava.io.File;importjava.io.FileOutputStream;importjava.util.ArrayList;importcom.itextpdf.text.pdf.AcroFields;importcom.itextpdf.text.pdf.BaseFont;importcom.itextpdf.text.pdf.PdfReader;importcom.itextpdf.text.pdf.PdfStamper;public classPDFTempletTicket {privateString templatePdfPath;privateString ttcPath;privateString targetPdfpath;privateTicket ticket;publicPDFTempletTicket() {super();
}publicPDFTempletTicket(String templatePdfPath, String ttcPath,
String targetPdfpath, Ticket ticket) {this.templatePdfPath=templatePdfPath;this.ttcPath=ttcPath;this.targetPdfpath=targetPdfpath;this.ticket=ticket;
}public void templetTicket(File file) throwsException {
PdfReader reader= newPdfReader(templatePdfPath);
ByteArrayOutputStream bos= newByteArrayOutputStream();
PdfStamper ps= newPdfStamper(reader, bos);/*使用中文字体*/BaseFont bf= BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);/*BaseFont bf = BaseFont.createFont(PDFTicket.class.getResource("/") + "org/csun/ns/util/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);*/ArrayList
fontList.add(bf);
AcroFields s=ps.getAcroFields();
s.setSubstitutionFonts(fontList);
s.setField("productName",ticket.getProductName());
s.setField("name",ticket.getName());
s.setField("idCard",ticket.getIdCard());
s.setField("tel",ticket.getTel());
s.setField("address",ticket.getAddress());
ps.setFormFlattening(false);
ps.close();
FileOutputStream fos= newFileOutputStream(file);
fos.write(bos.toByteArray());
fos.close();
}/***@returnthe templatePdfPath*/
publicString getTemplatePdfPath() {returntemplatePdfPath;
}/***@paramtemplatePdfPath the templatePdfPathto set*/
public voidsetTemplatePdfPath(String templatePdfPath) {this.templatePdfPath=templatePdfPath;
}/***@returnthe ttcPath*/
publicString getTtcPath() {returnttcPath;
}/***@paramttcPath the ttcPath to set*/
public voidsetTtcPath(String ttcPath) {this.ttcPath=ttcPath;
}/***@returnthe targetPdfpath*/
publicString getTargetPdfpath() {returntargetPdfpath;
}/***@paramtargetPdfpath the targetPdfpath toset*/
public voidsetTargetPdfpath(String targetPdfpath) {this.targetPdfpath=targetPdfpath;
}/***@returnthe ticket*/
publicTicket getTicket() {returnticket;
}/***@paramticket the ticket to set*/
public voidsetTicket(Ticket ticket) {this.ticket=ticket;
}
}