package com.byt.finance.util;
import com.byt.finance.utils.DateUtils;
import com.byt.finance.vo.CustomerStatementVO;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPRow;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.LineSeparator;
import org.apache.commons.io.FileUtils;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
/**
* itext5工具类
* @author tsp
*
*/
public class PdfUtils {
private static BaseFont baseFont;
private static Font boldFont;
private static Font normalFont;
private static Font boldFont10;
private static Font font12;
static {
try {
//使用iTextAsian.jar中的字体、并设置编码
baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
// baseFont = BaseFont.createFont("\\data\\ininin-g\\byt-finance-server\\font\\simsun.ttc,1",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
boldFont = new Font(baseFont,20,Font.BOLD);
normalFont = new Font(baseFont,10,Font.NORMAL);
boldFont10 = new Font(baseFont,10,Font.BOLD);
font12 = new Font(baseFont, 12,Font.NORMAL);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 添加副标题
* @param document 文档
* @param name 副标题名字
* @throws DocumentException
* @throws IOException
*/
public static void getTitleBasic(Document document, String name, int retract) throws DocumentException, IOException{
Font title_subheading_font = new Font(baseFont,17,Font.BOLD); //字体
Chunk temp_bule = new Chunk(" ",title_subheading_font); //文本蓝色小块
temp_bule.setBackground(new BaseColor(100 ,149 ,237)); //文本背景颜色
Chunk temp_blank = new Chunk(" ",title_subheading_font); //文本空白
Chunk temp_name = new Chunk(name,title_subheading_font); //文本标题文字
Paragraph paragraph = new Paragraph(); //段落
paragraph.add(temp_bule);
paragraph.add(temp_blank);
paragraph.add(temp_name);
paragraph.setFirstLineIndent(retract); //缩进
document.add(paragraph);
Font font = new Font(baseFont,17,Font.BOLD);
Paragraph temp = new Paragraph(" ",font);
document.add(temp);
//getBlank(document); //添加空白段落
}
/**
* 添加空表段落
* @param document
* @throws DocumentException
* @throws IOException
*/
public static void getBlank(Document document) throws DocumentException,IOException {
Paragraph temp = new Paragraph(" ",boldFont);
//Chunk temp = new Chunk(" ",FontFactory.getFont(FontFactory.HELVETICA, 20, Font.NORMAL)); //空白
document.add(temp);
}
/**
* 添加虚线
* @param document
* @throws DocumentException
* @throws IOException
*/
public static void getLineSeparator(Document document) throws DocumentException, IOException{
//1.线宽度
//2.直线长度,是个百分百,0-100之间
//3.直线颜色
//4.直线位置
//5.上下移动位置
LineSeparator line = new LineSeparator(1.2f,80,new BaseColor(190,190,190),1,0f); //虚线
Font line_font = new Font(baseFont,26,Font.BOLD); //空白字体
Paragraph paragraph_temp = new Paragraph(" ",line_font); //空白段落
document.add(paragraph_temp);
document.add(line);
document.add(paragraph_temp);
}
/**
* 创建有边框表格
* @param width 表格的大小 为null则默认大小
* @param list 数据
* @param count 列数
* @param document
* @throws DocumentException
* @throws IOException
*/
public static void getCreateTable(Document document,Integer width,List> list,int count) throws DocumentException, IOException{
Font tetle_font = new Font(baseFont,10,Font.BOLD);//数据字体
PdfPTable table = new PdfPTable(count);
for (int i = 0; i < list.size(); i++) {
PdfPCell cell = new PdfPCell();
String chunlName = list.get(i).toString();
if (i < count) {
Chunk chunk = new Chunk(chunlName,tetle_font); //标题
cell.addElement(chunk);
cell.setBackgroundColor(new BaseColor(232,232,232)); //背景
}else{
Chunk chunk = new Chunk(chunlName,normalFont); //文本块
cell.addElement(chunk);
}
cell.setBorderColor(new BaseColor(190,190,190)); //单元格边框颜色
cell.setUseDescender(true); //是否居中
table.addCell(cell);
}
table.setHorizontalAlignment(1);
//自动把列扩大缩小
if (count>=10) {
float[] columnWidths = new float[count];
for (int i = 0; i < count; i++) {
if (i>1) {
String name = (String)list.get(i);
int num = name.length();
if (num==4) {
columnWidths[i] = 0.75f;
}else if(num>4 && num<7){
columnWidths[i] = 1f;
}else if (num>6 && num<9) {
columnWidths[i] = 1.35f;
}
}else{
if (i == 0) {
columnWidths[i] = 0.4f;
}else{
columnWidths[i] = 1f;
}
}
}
table.setWidths(columnWidths);
}
if (width != null) { //为null则默认大小
table.setWidthPercentage(width);
}
document.add(table);
getBlank(document);
}
/**
* 关闭iText
* @param document 文档
* @param writer 书写器
* @throws DocumentException
* @throws FileNotFoundException
*/
public static void getAllClose(Document document, PdfWriter writer) throws DocumentException, FileNotFoundException {
//关闭文档
document.close();
//关闭书写器
writer.close();
}
/**
* 用户下载并把文件名输出为中文
* @param response
* @param file
* @param fileName
*/
public static void downLoadPDFFile(HttpServletResponse response, File file, String fileName){
try {
// response.setCharacterEncoding("UTF-8");
response.setContentType("application/pdf");// 设置输出格式头信息
// response.setHeader("Content-Disposition", "attachment; filename="+fileName+".pdf"+";target=_blank");
response.addHeader("Content-Disposition", "attachment; filename=" +new String(fileName.getBytes("utf-8"),"iso-8859-1")+ ".pdf");
OutputStream os;
os = response.getOutputStream();
FileUtils.copyFile(file, os);
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建无边框表格
* @param document
* @param list
* @param count
* @throws DocumentException
* @throws IOException
*/
public static void getCreateFramelessTable(Document document,List> list,int count) throws DocumentException, IOException{
PdfPTable table = new PdfPTable(count);
for (int i = 0; i < list.size(); i++) {
PdfPCell cell = new PdfPCell();
String chunlName = list.get(i).toString();
Chunk chunk = new Chunk(chunlName,normalFont);
cell.addElement(chunk);
cell.setBorderWidth(0); //边框代销 为0无边框
cell.setUseDescender(true); //是否居中
table.addCell(cell);
}
table.setWidthPercentage(100);
document.add(table);
}
/**
* 创建无左右边框的表格
* @param document
* @param list
* @param count
* @throws DocumentException
* @throws IOException
*/
public static void getCreateFramelessAroundTable(Document document,List> list,int count) throws DocumentException{
PdfPTable table = new PdfPTable(count);
for (int i = 0; i < list.size(); i++) {
PdfPCell cell = new PdfPCell();
String chunlName = String.valueOf(list.get(i));
Chunk chunk = new Chunk(chunlName,normalFont);
cell.addElement(chunk);
cell.setBorderColor(new BaseColor(176,196,222)); //边框颜色
cell.disableBorderSide(12);
cell.setUseDescender(true); //是否居中
if (i list,String filePath,Date startDate,Date endDate,String ptNameSt,String buyerRememberCode) throws DocumentException, IOException {
//创建文件
Document document = new Document();
//建立一个书写器
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filePath));
writer.setViewerPreferences(PdfWriter.PageModeUseThumbs);
writer.setPageEvent(new MyHeaderFooter());
document.setPageSize(PageSize.A4);
//打开文件
document.open();
//添加标题
Paragraph title = new Paragraph(20,"佛山市御腾环保科技有限公司", boldFont);
title.setAlignment(1);
document.add(title);
Paragraph title1 = new Paragraph(20,"地址:佛山市顺德区伦敦三洲振兴路6A号之一", normalFont);
title1.setAlignment(1);
document.add(title1);
Paragraph title2 = new Paragraph(20,"电话:0757-29994082 传真:0757-27887190", normalFont);
title2.setAlignment(1);
document.add(title2);
Paragraph title3 = new Paragraph(30,"对账单", boldFont);
title3.setAlignment(1);
document.add(title3);
Paragraph title4 = new Paragraph(20,"日期:" + DateUtils.dateToStr(startDate,DateUtils.YYYY_MM_DD) + "~" + DateUtils.dateToStr(endDate,DateUtils.YYYY_MM_DD) + " "+ DateUtils.dateToStr(startDate,"yyyy 年 MM 月") +" 发单日期:"+ com.byt.finance.utils.DateUtils.dateToStr(new Date(), DateUtils.YYYYIMMIDD), normalFont);
title4.setAlignment(Element.ALIGN_LEFT);
document.add(title4);
Paragraph title5 = new Paragraph(20,"客户:"+ ptNameSt +" 代号:"+ buyerRememberCode, normalFont);
title5.setAlignment(Element.ALIGN_LEFT);
document.add(title5);
// 9列的表.
PdfPTable table = new PdfPTable(9);
table.setWidthPercentage(100); // 宽度100%填充
table.setSpacingBefore(10f); // 前间距
table.setSpacingAfter(10f); // 后间距
table.setHorizontalAlignment(Element.ALIGN_LEFT);
List listRow = table.getRows();
//设置列宽
float[] columnWidths = { 0.5f,0.7f,1.2f, 0.7f,1f,0.8f,0.8f,1f,1f};
table.setWidths(columnWidths);
//行1
PdfPCell cells1[]= new PdfPCell[9];
PdfPRow row1 = new PdfPRow(cells1);
//单元格
cells1[0] = new PdfPCell(new Paragraph("日期",normalFont));//单元格内容
cells1[0].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
cells1[0].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
cells1[0].setMinimumHeight(20);//行高
cells1[1] = new PdfPCell(new Paragraph("类型",normalFont));
cells1[1].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
cells1[1].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
cells1[1].setMinimumHeight(20);//行高
cells1[2] = new PdfPCell(new Paragraph("单据编号",normalFont));
cells1[2].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
cells1[2].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
cells1[2].setMinimumHeight(20);//行高
cells1[3] = new PdfPCell(new Paragraph("送货数",normalFont));
cells1[3].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
cells1[3].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
cells1[3].setMinimumHeight(20);//行高
cells1[4] = new PdfPCell(new Paragraph("回签金额",normalFont));
cells1[4].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
cells1[4].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
cells1[4].setMinimumHeight(20);//行高
cells1[5] = new PdfPCell(new Paragraph("调整金额",normalFont));
cells1[5].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
cells1[5].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
cells1[5].setMinimumHeight(20);//行高
cells1[6] = new PdfPCell(new Paragraph("退货金额",normalFont));
cells1[6].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
cells1[6].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
cells1[6].setMinimumHeight(20);//行高
cells1[7] = new PdfPCell(new Paragraph("收款金额",normalFont));
cells1[7].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
cells1[7].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
cells1[7].setMinimumHeight(20);//行高
cells1[8] = new PdfPCell(new Paragraph("备注",normalFont));
cells1[8].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
cells1[8].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
cells1[8].setMinimumHeight(20);//行高
//把标题添加到集合
listRow.add(row1);
Integer totalDeliQuantity = 0;
BigDecimal totalSignAmount = BigDecimal.ZERO;
BigDecimal totalAdjAmount = BigDecimal.ZERO;
BigDecimal totalReturnAmount = BigDecimal.ZERO;
BigDecimal totalReceivedAmount = BigDecimal.ZERO;
BigDecimal totalEndTermArrearsAmount = BigDecimal.ZERO;
Integer totalSingular = 0;
//循环获取数据并添加导表格里
for (int i = 0; i < list.size(); i++) {
CustomerStatementVO customerStatementVO = list.get(i);
//日期
PdfPCell cell1 = new PdfPCell();
//类型
PdfPCell cell2 = new PdfPCell();
//单据编号
PdfPCell cell3 = new PdfPCell();
//送货数
PdfPCell cell4 = new PdfPCell();
//回签金额
PdfPCell cell5 = new PdfPCell();
//调整金额
PdfPCell cell6 = new PdfPCell();
//退货金额
PdfPCell cell7 = new PdfPCell();
//收款金额
PdfPCell cell8 = new PdfPCell();
//备注
PdfPCell cell9 = new PdfPCell();
cell1.setBorderColor(BaseColor.BLACK);
cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell1.setMinimumHeight(20);
cell1.setPhrase(new Paragraph(customerStatementVO.getTransactionDate(), normalFont));
cell2.setBorderColor(BaseColor.BLACK);
cell2.setHorizontalAlignment(Element.ALIGN_LEFT);
cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell2.setMinimumHeight(20);
cell2.setPhrase(new Paragraph(customerStatementVO.getDocumentType(), normalFont));
cell3.setBorderColor(BaseColor.BLACK);
cell3.setHorizontalAlignment(Element.ALIGN_LEFT);
cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell3.setMinimumHeight(20);
cell3.setPhrase(new Paragraph(customerStatementVO.getDocumentNumber(), normalFont));
cell4.setBorderColor(BaseColor.BLACK);
cell4.setHorizontalAlignment(Element.ALIGN_LEFT);
cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell4.setMinimumHeight(20);
cell4.setPhrase(new Paragraph(Objects.nonNull(customerStatementVO.getDeliQuantity()) ? customerStatementVO.getDeliQuantity().toString() : "", normalFont));
cell5.setBorderColor(BaseColor.BLACK);
cell5.setHorizontalAlignment(Element.ALIGN_LEFT);
cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell5.setMinimumHeight(20);
cell5.setPhrase(new Paragraph(Objects.nonNull(customerStatementVO.getSignAmount()) ? customerStatementVO.getSignAmount().stripTrailingZeros().toPlainString() : "", normalFont));
cell6.setBorderColor(BaseColor.BLACK);
cell6.setHorizontalAlignment(Element.ALIGN_LEFT);
cell6.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell6.setMinimumHeight(20);
cell6.setPhrase(new Paragraph(Objects.nonNull(customerStatementVO.getAdjAmount()) ? customerStatementVO.getAdjAmount().stripTrailingZeros().toPlainString() : "", normalFont));
cell7.setBorderColor(BaseColor.BLACK);
// cell7.setPaddingLeft(10);
cell7.setHorizontalAlignment(Element.ALIGN_LEFT);
cell7.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell7.setMinimumHeight(20);
cell7.setPhrase(new Paragraph(Objects.nonNull(customerStatementVO.getReturnAmount()) ? customerStatementVO.getReturnAmount().stripTrailingZeros().toPlainString() : "", normalFont));
cell8.setBorderColor(BaseColor.BLACK);
cell8.setHorizontalAlignment(Element.ALIGN_LEFT);
cell8.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell8.setMinimumHeight(20);
cell8.setPhrase(new Paragraph(Objects.nonNull(customerStatementVO.getReceivedAmount()) ? customerStatementVO.getReceivedAmount().stripTrailingZeros().toPlainString() : "", normalFont));
cell9.setBorderColor(BaseColor.BLACK);
cell9.setHorizontalAlignment(Element.ALIGN_LEFT);
cell9.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell9.setMinimumHeight(20);
if("期初".equals(list.get(i).getDocumentType()) || "客户小计".equals(list.get(i).getDocumentType())){
cell9.setPhrase(new Paragraph(Objects.nonNull(customerStatementVO.getAmount()) ? customerStatementVO.getAmount().stripTrailingZeros().toPlainString() : "", normalFont));
if("客户小计".equals(list.get(i).getDocumentType())){
totalEndTermArrearsAmount = totalEndTermArrearsAmount.add(customerStatementVO.getAmount());
}
}else{
cell9.setPhrase(new Paragraph(list.get(i).getRemark(), normalFont));
}
if("送货".equals(list.get(i).getDocumentType())){
totalSingular++;
}
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
table.addCell(cell5);
table.addCell(cell6);
table.addCell(cell7);
table.addCell(cell8);
table.addCell(cell9);
totalDeliQuantity += Optional.ofNullable(customerStatementVO.getDeliQuantity()).orElse(0);
totalSignAmount = totalSignAmount.add(Optional.ofNullable(customerStatementVO.getSignAmount()).orElse(BigDecimal.ZERO));
totalAdjAmount = totalAdjAmount.add(Optional.ofNullable(customerStatementVO.getAdjAmount()).orElse(BigDecimal.ZERO));
totalReturnAmount = totalReturnAmount.add(Optional.ofNullable(customerStatementVO.getReturnAmount()).orElse(BigDecimal.ZERO));
totalReceivedAmount = totalReceivedAmount.add(Optional.ofNullable(customerStatementVO.getReceivedAmount()).orElse(BigDecimal.ZERO));
}
//日期
PdfPCell cell1 = new PdfPCell();
//类型
PdfPCell cell2 = new PdfPCell();
//单据编号
PdfPCell cell3 = new PdfPCell();
//送货数
PdfPCell cell4 = new PdfPCell();
//回签金额
PdfPCell cell5 = new PdfPCell();
//调整金额
PdfPCell cell6 = new PdfPCell();
//退货金额
PdfPCell cell7 = new PdfPCell();
//收款金额
PdfPCell cell8 = new PdfPCell();
//备注
PdfPCell cell9 = new PdfPCell();
cell1.setBorderColor(BaseColor.BLACK);
cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell1.setMinimumHeight(20);
cell1.setPhrase(new Paragraph("", normalFont));
cell1.disableBorderSide(14);//只剩上
cell2.setBorderColor(BaseColor.BLACK);
cell2.setHorizontalAlignment(Element.ALIGN_LEFT);
cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell2.setMinimumHeight(20);
cell2.setPhrase(new Paragraph("本月合计", normalFont));
cell2.disableBorderSide(14);//只剩上
cell3.setBorderColor(BaseColor.BLACK);
cell3.setHorizontalAlignment(Element.ALIGN_LEFT);
cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell3.setMinimumHeight(20);
cell3.setPhrase(new Paragraph("", normalFont));
cell3.disableBorderSide(14);//只剩上
cell4.setBorderColor(BaseColor.BLACK);
cell4.setHorizontalAlignment(Element.ALIGN_LEFT);
cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell4.setMinimumHeight(20);
cell4.setPhrase(new Paragraph(totalDeliQuantity.toString(), normalFont));
cell4.disableBorderSide(14);//只剩上
cell5.setBorderColor(BaseColor.BLACK);
cell5.setHorizontalAlignment(Element.ALIGN_LEFT);
cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell5.setMinimumHeight(20);
cell5.setPhrase(new Paragraph(totalSignAmount.stripTrailingZeros().toPlainString(), normalFont));
cell5.disableBorderSide(14);//只剩上
cell6.setBorderColor(BaseColor.BLACK);
cell6.setHorizontalAlignment(Element.ALIGN_LEFT);
cell6.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell6.setMinimumHeight(20);
cell6.setPhrase(new Paragraph(totalAdjAmount.stripTrailingZeros().toPlainString(), normalFont));
cell6.disableBorderSide(14);//只剩上
cell7.setBorderColor(BaseColor.BLACK);
// cell7.setPaddingLeft(10);
cell7.setHorizontalAlignment(Element.ALIGN_LEFT);
cell7.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell7.setMinimumHeight(20);
cell7.setPhrase(new Paragraph(totalReturnAmount.stripTrailingZeros().toPlainString(), normalFont));
cell7.disableBorderSide(14);//只剩上
cell8.setBorderColor(BaseColor.BLACK);
cell8.setHorizontalAlignment(Element.ALIGN_LEFT);
cell8.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell8.setMinimumHeight(20);
cell8.setPhrase(new Paragraph(totalReceivedAmount.stripTrailingZeros().toPlainString(), normalFont));
cell8.disableBorderSide(14);//只剩上
cell9.setBorderColor(BaseColor.BLACK);
cell9.setHorizontalAlignment(Element.ALIGN_LEFT);
cell9.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell9.setMinimumHeight(20);
cell9.disableBorderSide(14);//只剩上
//日期
PdfPCell cell11 = new PdfPCell();
//类型
PdfPCell cell22 = new PdfPCell();
//单据编号
PdfPCell cell33 = new PdfPCell();
//送货数
PdfPCell cell44 = new PdfPCell();
//回签金额
PdfPCell cell55 = new PdfPCell();
//调整金额
PdfPCell cell66 = new PdfPCell();
//退货金额
PdfPCell cell77 = new PdfPCell();
//收款金额
PdfPCell cell88 = new PdfPCell();
//备注
PdfPCell cell99 = new PdfPCell();
cell11.setBorderColor(BaseColor.BLACK);
cell11.setHorizontalAlignment(Element.ALIGN_LEFT);
cell11.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell11.setMinimumHeight(20);
cell11.setPhrase(new Paragraph("", normalFont));
cell11.disableBorderSide(15);//只剩上
cell22.setBorderColor(BaseColor.BLACK);
cell22.setHorizontalAlignment(Element.ALIGN_LEFT);
cell22.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell22.setMinimumHeight(20);
cell22.setPhrase(new Paragraph("期末欠款", normalFont));
cell22.disableBorderSide(15);//只剩上
cell33.setBorderColor(BaseColor.BLACK);
cell33.setHorizontalAlignment(Element.ALIGN_LEFT);
cell33.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell33.setMinimumHeight(20);
cell33.setPhrase(new Paragraph(totalEndTermArrearsAmount.stripTrailingZeros().toPlainString(), normalFont));
cell33.disableBorderSide(15);//只剩上
cell44.setBorderColor(BaseColor.BLACK);
cell44.setHorizontalAlignment(Element.ALIGN_LEFT);
cell44.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell44.setMinimumHeight(20);
cell44.setPhrase(new Paragraph("总单数", normalFont));
cell44.disableBorderSide(15);//只剩上
cell55.setBorderColor(BaseColor.BLACK);
cell55.setHorizontalAlignment(Element.ALIGN_LEFT);
cell55.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell55.setMinimumHeight(20);
cell55.setPhrase(new Paragraph(totalSingular.toString(), normalFont));
cell55.disableBorderSide(15);//只剩上
cell66.setBorderColor(BaseColor.BLACK);
cell66.setHorizontalAlignment(Element.ALIGN_LEFT);
cell66.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell66.setMinimumHeight(20);
cell66.setPhrase(new Paragraph("", normalFont));
cell66.disableBorderSide(15);//只剩上
cell77.setBorderColor(BaseColor.BLACK);
// cell7.setPaddingLeft(10);
cell77.setHorizontalAlignment(Element.ALIGN_LEFT);
cell77.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell77.setMinimumHeight(20);
cell77.setPhrase(new Paragraph("", normalFont));
cell77.disableBorderSide(15);//只剩上
cell88.setBorderColor(BaseColor.BLACK);
cell88.setHorizontalAlignment(Element.ALIGN_LEFT);
cell88.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell88.setMinimumHeight(20);
cell88.setPhrase(new Paragraph("", normalFont));
cell88.disableBorderSide(15);//只剩上
cell99.setBorderColor(BaseColor.BLACK);
cell99.setHorizontalAlignment(Element.ALIGN_LEFT);
cell99.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell99.setMinimumHeight(20);
cell99.setPhrase(new Paragraph("", normalFont));
cell99.disableBorderSide(15);//只剩上
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
table.addCell(cell5);
table.addCell(cell6);
table.addCell(cell7);
table.addCell(cell8);
table.addCell(cell9);
table.addCell(cell11);
table.addCell(cell22);
table.addCell(cell33);
table.addCell(cell44);
table.addCell(cell55);
table.addCell(cell66);
table.addCell(cell77);
table.addCell(cell88);
table.addCell(cell99);
//将表格的第一行设置为表头 让它在每一页都显示出来
table.setHeaderRows(1);
//把表格添加到文件中
document.add(table);
//添加分割线
LineSeparator line1 = new LineSeparator(0.5f,100,BaseColor.BLACK,Element.ALIGN_CENTER,-5f);
document.add(line1);
//协议
Paragraph xy = new Paragraph(20,"请于对账单收到后3天内盖章确认回传,并回复御腾财务部,逾期不回传当贵司直接确认;开发票的在此单写明,有\n" +
"其他要求请注明,谢谢!财务电话(对账):0757-29994082,传真0757-27887190", normalFont);
xy.setAlignment(Element.ALIGN_LEFT);
document.add(xy);
//签名
Paragraph qz = new Paragraph(100,"不符说明(签字盖章): 核对无误(签字盖章):", normalFont);
qz.setAlignment(Element.ALIGN_LEFT);
document.add(qz);
//关闭文档
document.close();
//关闭书写器
writer.close();
}
public static void generateMonthlyStatementPdf(List list,String filePath,Date startDate,Date endDate,String ptNameSt,String buyerRememberCode) throws DocumentException, IOException {
//创建文件
Document document = new Document();
//建立一个书写器
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filePath));
writer.setViewerPreferences(PdfWriter.PageModeUseThumbs);
writer.setPageEvent(new MyHeaderFooter());
document.setPageSize(PageSize.A4);
//打开文件
document.open();
//添加标题
Paragraph title = new Paragraph(20,"佛山市御腾环保科技有限公司", boldFont);
title.setAlignment(Element.ALIGN_CENTER);
document.add(title);
Paragraph title1 = new Paragraph(20,"地址:佛山市顺德区伦敦三洲振兴路6A号之一", normalFont);
title1.setAlignment(Element.ALIGN_CENTER);
document.add(title1);
Paragraph title2 = new Paragraph(20,"电话:0757-29994082 传真:0757-27887190", normalFont);
title2.setAlignment(Element.ALIGN_CENTER);
document.add(title2);
Paragraph title3 = new Paragraph(30,"月结单", boldFont);
title3.setAlignment(Element.ALIGN_CENTER);
document.add(title3);
Paragraph title4 = new Paragraph(20,"日期:" + DateUtils.dateToStr(startDate,DateUtils.YYYY_MM_DD) + "~" + DateUtils.dateToStr(endDate,DateUtils.YYYY_MM_DD) + " "+ DateUtils.dateToStr(startDate,"yyyy 年 MM 月") +" 发单日期:"+ com.byt.finance.utils.DateUtils.dateToStr(new Date(), DateUtils.YYYYIMMIDD), normalFont);
title4.setAlignment(Element.ALIGN_LEFT);
document.add(title4);
Paragraph title5 = new Paragraph(20,"客户:"+ ptNameSt +" 代号:"+ buyerRememberCode, normalFont);
title5.setAlignment(Element.ALIGN_LEFT);
document.add(title5);
// 9列的表.
PdfPTable table = new PdfPTable(8);
table.setWidthPercentage(100); // 宽度100%填充
table.setSpacingBefore(10f); // 前间距
table.setSpacingAfter(10f); // 后间距
table.setHorizontalAlignment(Element.ALIGN_LEFT);
List listRow = table.getRows();
//设置列宽
float[] columnWidths = { 0.8f,0.5f,0.5f, 0.5f,0.5f,0.8f,0.8f,0.8f};
table.setWidths(columnWidths);
//行1
PdfPCell cells1[]= new PdfPCell[8];
PdfPRow row1 = new PdfPRow(cells1);
//单元格
cells1[0] = new PdfPCell(new Paragraph("送货单号",normalFont));//单元格内容
cells1[0].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
cells1[0].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
cells1[0].setMinimumHeight(20);//行高
cells1[1] = new PdfPCell(new Paragraph("纸质",normalFont));
cells1[1].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
cells1[1].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
cells1[1].setMinimumHeight(20);//行高
cells1[2] = new PdfPCell(new Paragraph("平方价",normalFont));
cells1[2].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
cells1[2].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
cells1[2].setMinimumHeight(20);//行高
cells1[3] = new PdfPCell(new Paragraph("送货数",normalFont));
cells1[3].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
cells1[3].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
cells1[3].setMinimumHeight(20);//行高
cells1[4] = new PdfPCell(new Paragraph("片单价",normalFont));
cells1[4].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
cells1[4].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
cells1[4].setMinimumHeight(20);//行高
cells1[5] = new PdfPCell(new Paragraph("送货金额",normalFont));
cells1[5].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
cells1[5].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
cells1[5].setMinimumHeight(20);//行高
cells1[6] = new PdfPCell(new Paragraph("接单尺寸",normalFont));
cells1[6].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
cells1[6].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
cells1[6].setMinimumHeight(20);//行高
cells1[7] = new PdfPCell(new Paragraph("送货面积",normalFont));
cells1[7].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
cells1[7].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
cells1[7].setMinimumHeight(20);//行高
//把标题添加到集合
listRow.add(row1);
Integer totalDeliQuantity = 0;
BigDecimal totalDeliAmount = BigDecimal.ZERO;
Double totalDeliArea = 0d;
//循环获取数据并添加导表格里
for (int i = 0; i < list.size(); i++) {
CustomerStatementVO customerStatementVO = list.get(i);
if("期初".equals(customerStatementVO.getDocumentType()) || "客户小计".equals(customerStatementVO.getDocumentType())){
continue;
}
//送货单号
PdfPCell cell1 = new PdfPCell();
//纸质
PdfPCell cell2 = new PdfPCell();
//平方价
PdfPCell cell3 = new PdfPCell();
//送货数
PdfPCell cell4 = new PdfPCell();
//片单价
PdfPCell cell5 = new PdfPCell();
//送货金额
PdfPCell cell6 = new PdfPCell();
//接单尺寸
PdfPCell cell7 = new PdfPCell();
//送货面积
PdfPCell cell8 = new PdfPCell();
cell1.setBorderColor(BaseColor.BLACK);
cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell1.setMinimumHeight(20);
cell1.setPhrase(new Paragraph(customerStatementVO.getStockNo(), normalFont));
cell2.setBorderColor(BaseColor.BLACK);
cell2.setHorizontalAlignment(Element.ALIGN_LEFT);
cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell2.setMinimumHeight(20);
cell2.setPhrase(new Paragraph(customerStatementVO.getPaTypeOrder(), normalFont));
cell3.setBorderColor(BaseColor.BLACK);
cell3.setHorizontalAlignment(Element.ALIGN_LEFT);
cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell3.setMinimumHeight(20);
cell3.setPhrase(new Paragraph(Objects.nonNull(customerStatementVO.getOrderPrice()) ? customerStatementVO.getOrderPrice().toString() : "", normalFont));
cell4.setBorderColor(BaseColor.BLACK);
cell4.setHorizontalAlignment(Element.ALIGN_LEFT);
cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell4.setMinimumHeight(20);
cell4.setPhrase(new Paragraph(Objects.nonNull(customerStatementVO.getDeliQuantity()) ? customerStatementVO.getDeliQuantity().toString() : "", normalFont));
cell5.setBorderColor(BaseColor.BLACK);
cell5.setHorizontalAlignment(Element.ALIGN_LEFT);
cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell5.setMinimumHeight(20);
cell5.setPhrase(new Paragraph(Objects.nonNull(customerStatementVO.getPrice()) ? customerStatementVO.getPrice().toString() : "", normalFont));
cell6.setBorderColor(BaseColor.BLACK);
cell6.setHorizontalAlignment(Element.ALIGN_LEFT);
cell6.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell6.setMinimumHeight(20);
cell6.setPhrase(new Paragraph(Objects.nonNull(customerStatementVO.getDeliAmount()) ? customerStatementVO.getDeliAmount().stripTrailingZeros().toPlainString() : "", normalFont));
cell7.setBorderColor(BaseColor.BLACK);
// cell7.setPaddingLeft(10);
cell7.setHorizontalAlignment(Element.ALIGN_LEFT);
cell7.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell7.setMinimumHeight(20);
cell7.setPhrase(new Paragraph(customerStatementVO.getMtSize(), normalFont));
cell8.setBorderColor(BaseColor.BLACK);
cell8.setHorizontalAlignment(Element.ALIGN_LEFT);
cell8.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell8.setMinimumHeight(20);
cell8.setPhrase(new Paragraph(Objects.nonNull(customerStatementVO.getDeliArea()) ? customerStatementVO.getDeliArea().toString() : "", normalFont));
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
table.addCell(cell5);
table.addCell(cell6);
table.addCell(cell7);
table.addCell(cell8);
totalDeliQuantity += Optional.ofNullable(customerStatementVO.getDeliQuantity()).orElse(0);
totalDeliAmount = totalDeliAmount.add(Optional.ofNullable(customerStatementVO.getDeliAmount()).orElse(BigDecimal.ZERO));
totalDeliArea = totalDeliArea + Optional.ofNullable(customerStatementVO.getDeliArea()).orElse(0d);
}
//送货单号
PdfPCell cell1 = new PdfPCell();
//纸质
PdfPCell cell2 = new PdfPCell();
//平方价
PdfPCell cell3 = new PdfPCell();
//送货数
PdfPCell cell4 = new PdfPCell();
//片单价
PdfPCell cell5 = new PdfPCell();
//送货金额
PdfPCell cell6 = new PdfPCell();
//接单尺寸
PdfPCell cell7 = new PdfPCell();
//送货面积
PdfPCell cell8 = new PdfPCell();
cell1.setBorderColor(BaseColor.BLACK);
cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell1.setMinimumHeight(20);
cell1.setPhrase(new Paragraph("", normalFont));
cell1.disableBorderSide(14);//只剩上
cell2.setBorderColor(BaseColor.BLACK);
cell2.setHorizontalAlignment(Element.ALIGN_LEFT);
cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell2.setMinimumHeight(20);
cell2.setPhrase(new Paragraph("本月合计", normalFont));
cell2.disableBorderSide(14);//只剩上
cell3.setBorderColor(BaseColor.BLACK);
cell3.setHorizontalAlignment(Element.ALIGN_LEFT);
cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell3.setMinimumHeight(20);
cell3.setPhrase(new Paragraph("", normalFont));
cell3.disableBorderSide(14);//只剩上
cell4.setBorderColor(BaseColor.BLACK);
cell4.setHorizontalAlignment(Element.ALIGN_LEFT);
cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell4.setMinimumHeight(20);
cell4.setPhrase(new Paragraph(totalDeliQuantity.toString(), normalFont));
cell4.disableBorderSide(14);//只剩上
cell5.setBorderColor(BaseColor.BLACK);
cell5.setHorizontalAlignment(Element.ALIGN_LEFT);
cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell5.setMinimumHeight(20);
cell5.setPhrase(new Paragraph("", normalFont));
cell5.disableBorderSide(14);//只剩上
cell6.setBorderColor(BaseColor.BLACK);
cell6.setHorizontalAlignment(Element.ALIGN_LEFT);
cell6.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell6.setMinimumHeight(20);
cell6.setPhrase(new Paragraph(totalDeliAmount.stripTrailingZeros().toPlainString(), normalFont));
cell6.disableBorderSide(14);//只剩上
cell7.setBorderColor(BaseColor.BLACK);
// cell7.setPaddingLeft(10);
cell7.setHorizontalAlignment(Element.ALIGN_LEFT);
cell7.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell7.setMinimumHeight(20);
cell7.setPhrase(new Paragraph("", normalFont));
cell7.disableBorderSide(14);//只剩上
cell8.setBorderColor(BaseColor.BLACK);
cell8.setHorizontalAlignment(Element.ALIGN_LEFT);
cell8.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell8.setMinimumHeight(20);
cell8.setPhrase(new Paragraph(totalDeliArea.toString(), normalFont));
cell8.disableBorderSide(14);//只剩上
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
table.addCell(cell5);
table.addCell(cell6);
table.addCell(cell7);
table.addCell(cell8);
//将表格的第一行设置为表头 让它在每一页都显示出来
table.setHeaderRows(1);
//把表格添加到文件中
document.add(table);
//添加分割线
LineSeparator line1 = new LineSeparator(0.5f,100,BaseColor.BLACK,Element.ALIGN_CENTER,-5f);
document.add(line1);
//协议
Paragraph xy = new Paragraph(20,"请于对账单收到后3天内盖章确认回传,并回复御腾财务部,逾期不回传当贵司直接确认;开发票的在此单写明,有\n" +
"其他要求请注明,谢谢!财务电话(对账):0757-29994082,传真0757-27887190", normalFont);
xy.setAlignment(Element.ALIGN_LEFT);
document.add(xy);
//签名
Paragraph qz = new Paragraph(100,"不符说明(签字盖章): 核对无误(签字盖章):", normalFont);
qz.setAlignment(Element.ALIGN_LEFT);
document.add(qz);
//关闭文档
document.close();
//关闭书写器
writer.close();
}
/**
* 内部类
* 添加页眉、页脚
*/
public static class MyHeaderFooter extends PdfPageEventHelper {
// 总页数
PdfTemplate totalPage;
Font font12 = PdfUtils.font12;
// 打开文档时,创建一个总页数的模版
public void onOpenDocument(PdfWriter writer, Document document) {
PdfContentByte cb =writer.getDirectContent();
totalPage = cb.createTemplate(30, 16);
}
// 一页加载完成触发,写入页眉和页脚
public void onEndPage(PdfWriter writer, Document document) {
PdfPTable table = new PdfPTable(3);
try {
table.setTotalWidth(PageSize.A4.getWidth() - 80);
table.setWidths(new int[] { 24, 24, 3});
table.setLockedWidth(true);
table.getDefaultCell().setFixedHeight(-10);
table.getDefaultCell().setBorder(Rectangle.BOTTOM);
table.getDefaultCell().setBorderWidth(0.5f);
table.addCell(new Paragraph("", font12));// 可以直接使用addCell(str),不过不能指定字体,中文无法显示
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(new Paragraph("第" + writer.getPageNumber() + "页/", font12));
// 总页数
PdfPCell cell = new PdfPCell(Image.getInstance(totalPage));
cell.setBorder(Rectangle.BOTTOM);
table.addCell(cell);
// 将页眉写到document中,位置可以指定,指定到下面就是页脚
table.writeSelectedRows(0, -1, 40, PageSize.A4.getHeight() - 20, writer.getDirectContent());
table.writeSelectedRows(0, -1, 40, 40, writer.getDirectContent());
} catch (Exception de) {
throw new ExceptionConverter(de);
}
}
// 全部完成后,将总页数的pdf模版写到指定位置
public void onCloseDocument(PdfWriter writer, Document document) {
String text = "总" + (writer.getPageNumber()) + "页";
ColumnText.showTextAligned(totalPage, Element.ALIGN_LEFT, new Paragraph(text,font12), 2, 2, 0);
}
}
}