package cn.forx.controller.forcan.forcbiz.product.impl;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
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 cn.forx.model.forcan.basebiz.shop.DiscountModel;
import cn.forx.model.forcan.forcbiz.product.DishModel;
public class PdfUtil {
private static final String fontPath =
"/cn/forx/controller/forcan/util/font/simsun.ttf";
public PdfUtil() {
}
public static void
createPdf(List list,String pdfPath,String subject) throws
IllegalAccessException, IllegalArgumentException,
InvocationTargetException, DocumentException, IOException
{
// list中所存放对象的数据类型
Object vo = list.get(0);
Class currentClass = vo.getClass();
// 获取当前类中的属性
Field[] fields =
currentClass.getDeclaredFields();
// System.out.println(fields);
// 获得类中的方法
Method[] methods =
currentClass.getMethods();
//创建PDF
Document doc = new Document();
//将PDF与本地进行连接
PdfWriter.getInstance(doc, new
FileOutputStream(pdfPath));
BaseFont baseFontChinese=
BaseFont.createFont(fontPath,
BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
Font fontChinese = new
Font(baseFontChinese,12);
Font title1 = new Font(baseFontChinese, 20,
Font.BOLD);
//打开pdf
doc.open();
// 向PDF文档写入内容
PdfPTable mainTable = new PdfPTable(1);// 大表格一列
mainTable.setSplitLate(false);//跨页处理
mainTable.setSplitRows(true);
BaseColor skyColor = new BaseColor(224, 234,
239);
PdfPCell cellMain = new PdfPCell(new
Paragraph("测试", fontChinese));// 第一列,创建单元格cell
cellMain.setHorizontalAlignment(Element.ALIGN_CENTER);
cellMain.setBorderWidth(2f);//边框7px
cellMain.setFixedHeight(30);//单元格高30px
mainTable.addCell(cellMain);//添加到table去,每创一个cell,都添加到table才有效。
PdfPCell repayCell = new PdfPCell();
repayCell.setBorderWidth(2f);
repayCell.setPaddingLeft(-45f);
repayCell.setPaddingTop(10f);
repayCell.setPaddingBottom(10f);
repayCell.setPaddingRight(-45f);
PdfPTable repayTable = new
PdfPTable(5);//一行5列(column,row);
repayTable.setWidths(new
float[]{0.2f,0.2f,0.2f,0.2f,0.2f});//每个单元格占多宽
PdfPCell cell = new PdfPCell();//创建单元格
cell.setColspan(5);//跨5列
cell = new PdfPCell(new Paragraph("名称:",
fontChinese));
cell.setBorderWidth(1f);
cell.setFixedHeight(20);
repayTable.addCell(cell);
cell=new PdfPCell(new Paragraph("类型:",
fontChinese));
cell.setBorderWidth(1f);
cell.setFixedHeight(20);
repayTable.addCell(cell);
cell=new PdfPCell(new Paragraph("价格",
fontChinese));
cell.setBorderWidth(1f);
cell.setFixedHeight(20);
repayTable.addCell(cell);
cell=new PdfPCell(new Paragraph("菜系",
fontChinese));
cell.setBorderWidth(1f);
cell.setFixedHeight(20);
repayTable.addCell(cell);
cell=new PdfPCell(new Paragraph("单位",
fontChinese));
cell.setBorderWidth(1f);
cell.setFixedHeight(20);
repayTable.addCell(cell);
//加载图片
// Image image =
Image.getInstance(imagePath+"/images/pic/pic_"+isIndex(repayIndexInfo.getScore())+".jpg");
// cell.setImage(image);
// BaseColor whiteColor = new BaseColor(255, 255,
255);
// cell.setBorderColor(whiteColor);//设置背景颜色,我这里是白色
BaseColor whiteColor = new BaseColor(255, 255, 255);
// repayTable.addCell(cell);//table加入单元格
cell = new PdfPCell(new Paragraph("土豆丝",
fontChinese));
cell.setBorderWidth(1f);
cell.setFixedHeight(20);
repayTable.addCell(cell);
cell = new PdfPCell(new Paragraph("热菜",
fontChinese));
cell.setBorderWidth(1);
repayTable.addCell(cell);
// PdfPCell cell2= new PdfPCell();//创建单元格
cell=new PdfPCell(new Paragraph("22",
fontChinese));
cell.setBorderWidth(1f);
cell.setFixedHeight(20);
repayTable.addCell(cell);
cell=new PdfPCell(new Paragraph("川菜",
fontChinese));
cell.setBorderWidth(1f);
cell.setFixedHeight(20);
repayTable.addCell(cell);
cell=new PdfPCell(new Paragraph("盘",
fontChinese));
cell.setBorderWidth(1f);
cell.setFixedHeight(20);
repayTable.addCell(cell);
//第三行
for(int i=0;i<5;i++){
cell=new PdfPCell(new Paragraph(i+"",
fontChinese));
cell.setBorderWidth(1f);
cell.setFixedHeight(20);
repayTable.addCell(cell);
}
// 基本信息的表格-----end-----
repayCell.addElement(repayTable);
// 基本信息的单元格-----end-----
mainTable.addCell(repayCell);//把mainTable
第2行的单元格加进去
doc.add(mainTable);
// doc.add(table);
doc.close();
}
public static void
main(String[] args) {
// List<> list=new
ArrayList<>();
String
pdfPath="I:/test/dfdfdf.pdf";
String
subject="ggg";
try {
new PdfUtil().createPdf(list, pdfPath, subject);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentExceptione) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}