一、概述
以下代码可以在指定文件夹内创建一个简历pdf。
以下代码生成pdf,主要是设置cell所占的行、列、内容。
二、代码
1.需要的jar包
itext-asian-5.2.0.jar
itextpdf-5.5.5.jar
2.个人信息类MsgUtil.java
这个类里面放了个人信息;也可以放多个人的、生成多个pdf;代码如下:
import java.util.ArrayList;
import java.util.HashMap;
/**
* 个人信息类
*/
public class MsgUtil {
public static final ArrayList<HashMap<String,String>> baseMsgList = new ArrayList<>();
public static final ArrayList<ArrayList<HashMap<String,String>>> experienceList = new ArrayList<>();
static{
//装入第一个人的信息
addMsg1();
}
/**
* 装入1个人的信息,还有就复制下这个方法,再加,然后static里调用下装进去
*/
private static void addMsg1(){
//第一个人的信息
//这个只有1行数据,是1个map
HashMap<String, String> baseMsgListMy = new HashMap<>();
baseMsgList.add(baseMsgListMy);
baseMsgListMy.put("FullName","小明");
baseMsgListMy.put("Gender","男");
baseMsgListMy.put("Birthdate","2002-01-10");
baseMsgListMy.put("Card","1001112002011VWXYZ");
baseMsgListMy.put("HomeAddress","北京");
baseMsgListMy.put("detailAdress","海淀区");
baseMsgListMy.put("Tel","142xxxxzzzz");
baseMsgListMy.put("E_mail","test@qq.com");
baseMsgListMy.put("Wechat","testqq");
baseMsgListMy.put("Linkman","大明");
baseMsgListMy.put("Relationship","朋友");
baseMsgListMy.put("LinkmanTel","123ddddpppp");
//工作经历list
ArrayList<HashMap<String, String>> experienceListMy = new ArrayList<>();
experienceList.add(experienceListMy);
//工作经历,第一个map(第一行数据,还有就再加)
HashMap<String, String> experienceMap1 = new HashMap<>();
experienceListMy.add(experienceMap1);
experienceMap1.put("Type","2");
experienceMap1.put("TimeFrom","2022.01.01");
experienceMap1.put("Time","2022.02.02");
experienceMap1.put("School","公司");
experienceMap1.put("Degree","员工");
//工作经历,第二个map(第二行数据,还有就再加)
HashMap<String, String> experienceMap2 = new HashMap<>();
experienceListMy.add(experienceMap2);
experienceMap2.put("Type","2");
experienceMap2.put("TimeFrom","2022.01.01");
experienceMap2.put("Time","2023.01.01");
experienceMap2.put("School","国企");
experienceMap2.put("Degree","员工");
HashMap<String, String> experienceMap3 = new HashMap<>();
experienceListMy.add(experienceMap3);
experienceMap3.put("Type","1");
experienceMap3.put("TimeFrom","2122.01.01");
experienceMap3.put("Time","2123.01.01");
experienceMap3.put("School","大学");
experienceMap3.put("Degree","学士");
HashMap<String, String> experienceMap4 = new HashMap<>();
experienceListMy.add(experienceMap4);
experienceMap4.put("Type","1");
experienceMap4.put("TimeFrom","2122.01.01");
experienceMap4.put("Time","2123.01.01");
experienceMap4.put("School","高中");
experienceMap4.put("Degree","毕业");
}
}
3.生成pdf工具类CreatePdfUtil.java
这个类有生成pdf的方法,如果调整pdf格式就需要修改这个类,代码如下:
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* 生成PDF简历工具类
*/
public class CreatePdfUtil {
public static void createPdf(Integer total, String filePath){
try{
System.out.println("生成所有开始时间:"+System.currentTimeMillis());
if(total!=null && total>0){
int count=0;
ArrayList<HashMap<String,String>> baseMsgList = MsgUtil.baseMsgList;
for(int i=0;i<total;i++){
if(baseMsgList!=null && baseMsgList.size()>0){
HashMap<String,String> baseMsgBean = baseMsgList.get(i);
if(baseMsgBean!=null && baseMsgBean.get("Card") != null){
ArrayList<HashMap<String,String>> experienceListBean = MsgUtil.experienceList.get(i);
List<HashMap<String,String>> educationList = new ArrayList<HashMap<String,String>>();//教育经历
List<HashMap<String,String>> workList = new ArrayList<HashMap<String,String>>();//工作经历
if( experienceListBean!= null && experienceListBean.size()>0){
for(int j=0;j<experienceListBean.size();j++){
HashMap<String,String> Bean = experienceListBean.get(j);
if("1".equals(Bean.get("Type"))){//教育
educationList.add(Bean);
}else if("2".equals(Bean.get("Type"))){//工作
workList.add(Bean);
}
}
}
try{
Document document = new Document(PageSize.A4);
PdfWriter.getInstance(document,new FileOutputStream(filePath+baseMsgBean.get("Card")+".pdf"));
document.open();
PdfPTable table = createTable(baseMsgBean,workList,educationList);
document.add(table);
document.close();
}catch(Exception e){
System.out.println("生成简历异常!");
e.printStackTrace();
//生产简历异常后删除创建的文件
String filepath = filePath+baseMsgBean.get("Card")+".pdf";
File file = new File(filepath);
if (file.exists()) {
file.delete();
}
}
count = count+1;
}
}
}
System.out.println("生成所有结束时间:"+System.currentTimeMillis());
}
}catch(Exception e){
System.out.println("生成简历失败!");
System.out.println(e);
}
}
public static PdfPTable createTable(HashMap<String,String> baseMsgBean, List<HashMap<String,String>> workList, List<HashMap<String,String>> educationList)throws DocumentException, IOException {
BaseFont bfChinese = BaseFont.createFont( "STSongStd-Light" ,"UniGB-UCS2-H",false);
Font font = new Font(bfChinese,11, Font.NORMAL, BaseColor.BLACK);
Font font1 = new Font(bfChinese,11, Font.BOLD, BaseColor.BLACK);
PdfPTable table = new PdfPTable(7);
table.setWidthPercentage(110f);
table.setWidths(new float[]{1.1f,1.1f,1,1,1.3f,1,1});
table.setHorizontalAlignment(1);
PdfPCell cell;
cell = new PdfPCell(new Phrase("我的简历",new Font(bfChinese,20, Font.BOLD, BaseColor.BLACK)));
cell.setColspan(7);
cell.setPaddingBottom(8);
cell.setBorder(0);
cell.setHorizontalAlignment(1);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("基本信息",font1));
cell.setRowspan(2);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
table.addCell(new Phrase("姓名",font1));
table.addCell(new Phrase(baseMsgBean.get("FullName"),font));
table.addCell(new Phrase("性别",font1));
table.addCell(new Phrase(baseMsgBean.get("Gender"),font));
table.addCell(new Phrase("出生时间",font1));
String birthdate = baseMsgBean.get("Birthdate");
table.addCell(new Phrase(birthdate,font));
table.addCell(new Phrase("身份证号",font1));
cell = new PdfPCell(new Phrase(baseMsgBean.get("Card"),font));
cell.setColspan(5);
table.addCell(cell);
cell = new PdfPCell(new Phrase("通讯信息",font1));
cell.setRowspan(3);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
table.addCell(new Phrase("常住联系地址",font1));
cell = new PdfPCell(new Phrase(baseMsgBean.get("HomeAddress")+baseMsgBean.get("detailAdress"),font));
cell.setColspan(5);
table.addCell(cell);
table.addCell(new Phrase("手机号",font1));
String phone=baseMsgBean.get("Tel");
table.addCell(new Phrase(phone,font));
table.addCell(new Phrase("邮箱",font1));
table.addCell(new Phrase(baseMsgBean.get("E_mail"),font));
table.addCell(new Phrase("微信号",font1));
table.addCell(new Phrase(baseMsgBean.get("Wechat"),font));
table.addCell(new Phrase("紧急联系人",font1));
table.addCell(new Phrase(baseMsgBean.get("Linkman"),font));
table.addCell(new Phrase("与之关系",font1));
table.addCell(new Phrase(baseMsgBean.get("Relationship"),font));
table.addCell(new Phrase("手机号",font1));
String linkManPhone=baseMsgBean.get("LinkmanTel");
table.addCell(new Phrase(linkManPhone,font));
cell = new PdfPCell(new Phrase("工作经历",font1));
if(workList!=null && workList.size()>0){
cell.setRowspan(workList.size()+1);
}else{
cell.setRowspan(1);
}
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new PdfPCell(new Phrase("时间",font1));
cell.setColspan(2);
cell.setHorizontalAlignment(1);
table.addCell(cell);
cell = new PdfPCell(new Phrase("单位/公司",font1));
cell.setColspan(2);
cell.setHorizontalAlignment(1);
table.addCell(cell);
cell = new PdfPCell(new Phrase("岗位及工作内容",font1));
cell.setColspan(2);
cell.setHorizontalAlignment(1);
table.addCell(cell);
if(workList!=null && workList.size()>0){
for(int i=0;i<workList.size();i++){
HashMap<String,String> experienceBean = workList.get(i);
cell = new PdfPCell(new Phrase(experienceBean.get("TimeFrom")+"-"+experienceBean.get("Time"),font));
cell.setColspan(2);
cell.setHorizontalAlignment(1);
table.addCell(cell);
cell = new PdfPCell(new Phrase(experienceBean.get("School"),font));
cell.setColspan(2);
table.addCell(cell);
cell = new PdfPCell(new Phrase(experienceBean.get("Degree"),font));
cell.setColspan(2);
table.addCell(cell);
}
}
cell = new PdfPCell(new Phrase("教育经历",font1));
if(educationList!=null && educationList.size()>0){
cell.setRowspan(educationList.size()+1);
}else{
cell.setRowspan(1);
}
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new PdfPCell(new Phrase("时间",font1));
cell.setColspan(2);
cell.setHorizontalAlignment(1);
table.addCell(cell);
cell = new PdfPCell(new Phrase("院校/培训机构",font1));
cell.setColspan(2);
cell.setHorizontalAlignment(1);
table.addCell(cell);
cell = new PdfPCell(new Phrase("取得学位/资质",font1));
cell.setColspan(2);
cell.setHorizontalAlignment(1);
table.addCell(cell);
if(educationList!=null && educationList.size()>0){
for(int i=0;i<educationList.size();i++){
HashMap<String,String> experienceBean = educationList.get(i);
cell = new PdfPCell(new Phrase(experienceBean.get("Time"),font));
cell.setColspan(2);
cell.setHorizontalAlignment(1);
table.addCell(cell);
cell = new PdfPCell(new Phrase(experienceBean.get("School"),font));
cell.setColspan(2);
table.addCell(cell);
cell = new PdfPCell(new Phrase(experienceBean.get("Degree"),font));
cell.setColspan(2);
table.addCell(cell);
}
}
return table;
}
}
4.入口方法Main.java
这个类就是程序的入口,代码如下:
public class Main {
public static void main(String[] args) throws Exception {
//生成pdf的个数,生成pdf存放的文件夹
CreatePdfUtil.createPdf(1,"f:/jl/");
}
}
三、运行结果
上方代码执行后,可以生成一份个人简历pdf,如下:
如果需要增加内容、调整内容,自行修改代码即可。