<p>
需要导入itext-2.0.6.jar.zip 和itextasian-1.5.2.jar包,asian的是为了支持中文。</p><p>
</p><p>PdfContentByte和Image可以绝对定位。</p>
/**
* @模块:PDF版培训证书的格式
* @描述: PDF版培训证书的格式类
* @作者: Li Yifang
* @日期: 2016-4-13 下午
*/
package com.pxxy.util;
import java.io.File;
import java.io.FileOutputStream;
import java.text.ParseException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
import com.pxxy.pxperson.model.TeacherModel;
public class TeacherPdfFormate {
Document document = new Document();// 建立一个Document对象
private static Font enfont;
private static Font textfont;
static BaseFont bfChinese;
static {
try {
bfChinese = BaseFont.createFont(Constant.MSJDBOLD,
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
enfont = new Font(bfChinese, 12, Font.NORMAL);// 设置英文字体大小
textfont = new Font(bfChinese, 15, Font.NORMAL);// 设置字体大小
} catch (Exception e) {
e.printStackTrace();
}
}
public TeacherPdfFormate(File file, TeacherModel model) {
document.setPageSize(PageSize.A4);// 设置页面大小
PdfContentByte cb = null;
PdfWriter writer = null;
try {
writer = PdfWriter
.getInstance(document, new FileOutputStream(file));// 读取文档
document.open();
// 设置时间和证书编号
cb = writer.getDirectContent();
String publishDate = dateFormate(model.getStartTime(),
model.getEmploytype());
if (publishDate != null) {
cb.beginText();
cb.setFontAndSize(bfChinese, 12);// 设置证书编号和日期字体大小
cb.setTextRise(30);// 设置文本上升参数。
cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
"证书编号:" + model.getTcertification(), 190f, 140f, 0);// 设置证书编号
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, publishDate,
435f, 160f, 0);// 设置日期
cb.endText();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void generatePDF(TeacherModel model) throws Exception {
if (!model.getEmploytype().isEmpty()) {
//5001 特邀 5002 特聘
if (model.getEmploytype().equals("5001")) {
// 设置背景图片
Image img1 = Image.getInstance(Constant.TEACHER1_CERT_IMGPATH);// 特邀背景图片
img1.setAlignment(Image.MIDDLE | Image.UNDERLYING);// 设置图片位置
// img.setBorderWidth(10);
img1.scaleToFit(1130f, 800f);// 大小
document.add(img1);
} else {
Image img2 = Image.getInstance(Constant.TEACHER2_CERT_IMGPATH);// 特聘背景图片
img2.setAlignment(Image.MIDDLE | Image.UNDERLYING);// 设置图片位置
// img.setBorderWidth(10);
img2.scaleToFit(1130f, 800f);// 大小
document.add(img2);
}
// 设置段落
//控制格式
StringBuffer name = new StringBuffer(model.getName());
switch (name.length()) {
case 2:
name = name.append(" ");
name = name.insert(0, " ");
break;
case 3:
name = name.append(" ");
name = name.insert(0, " ");
break;
case 4:
name = name.append(" ");
name = name.insert(0, " ");
break;
}
String employType = model.getEmploytype();
if(employType.equals("5001")){
employType = "特邀";
}else{
employType = "特聘";
}
Paragraph content = new Paragraph("兹聘请 " + name.toString()
+ " 为本培训学院 "+ employType +"讲师", textfont);
content.setAlignment(Element.ALIGN_JUSTIFIED);//左右对齐排列
content.setIndentationLeft(86f);//右移
content.setIndentationRight(83f);//左移
content.setSpacingAfter(3f);//段前高度
content.setSpacingBefore(300f);//段后高度
content.setLeading(24f);//行间距
document.add(content);
Paragraph p = new Paragraph("聘任期限为 "
+ dateFormate(model.getStartTime(), model.getEmploytype())
+ "至"
+ dateFormate(model.getEndTime(), model.getEmploytype()),
textfont);
p.setIndentationLeft(86f);
document.add(p);
Paragraph pe = new Paragraph(
"Mr / Mrs "
+ model.getNameEn()
+ " is hereby appointed as a distinguished lecture for Continuing Education Base.",
enfont);
pe.setAlignment(Element.ALIGN_JUSTIFIED);
pe.setIndentationLeft(86f);
pe.setIndentationRight(83f);
pe.setSpacingAfter(5f);
pe.setSpacingBefore(13f);
// content.setFirstLineIndent(20);
pe.setLeading(15f);
document.add(pe);
Paragraph pet = new Paragraph("The valid date is from "
+ model.getStartTimeEn() + " to " + model.getEndTimeEn()
+ ".", enfont);
pet.setIndentationLeft(86f);
pet.setSpacingBefore(10f);
document.add(pet);
// 设置二维码
String qrPath = Constant.QRCODE + model.getTcertification()
+ ".jpg";
Image imgQR = Image.getInstance(qrPath);
imgQR.setAlignment(Image.LEFT | Image.CHUNK);// 设置图像的对齐方式。
imgQR.setAbsolutePosition(165f, 192f);// 设置图像的绝对位置
imgQR.scaleAbsolute(57f, 57f);// 直接设置显示尺寸
document.add(imgQR);
document.close();
}
}
// 证书日期处理
public String dateFormate(String date, String teacherType)
throws ParseException {
date = DateParse.DateChange(date);
String[] tempDate = null;
String publishDate = null;
if (date != null) {
tempDate = date.split("-");
/* if (teacherType == "5001") {
publishDate = tempDate[0] + "年" + tempDate[1] + "月"
+ tempDate[2] + "日";
} else {*/
publishDate = tempDate[0] + "年" + tempDate[1] + "月";
/*}*/
}
return publishDate;
}
}