效果大概长这样
导入依赖
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.9</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
工具类
package com.globex.download;
import com.globex.MathOperUtil;
import com.globex.seller.dto.TeContract;
import com.globex.seller.dto.TeContractItem;
import com.globex.seller.dto.TjmSeller;
import com.globex.seller.dto.TsoOrderitemUs;
import com.globex.seller.vo.DownLoadOrderVo;
import com.globex.system.logic.Parameter;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.pdf.draw.DottedLineSeparator;
import com.itextpdf.text.pdf.draw.LineSeparator;
import dev.fpxgroup.common.utility.DateUtility;
import dev.fpxgroup.common.utility.Globals;
import freemarker.template.TemplateException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
*/
public class DownloadPDFUtil {
private static Font titlefont;
private static Font headfont;
private static Font keyfont;
private static Font textfont;
private static Font labelfont;
private static Font textfont1;
private static Font normalfont11;
private static Font boldfont11;
// 最大宽度
private static int maxWidth = 520;
// 静态代码块
static {
try {
// 不同字体(这里定义为同一种字体:包含不同字号、不同style)
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
titlefont = new Font(bfChinese, 17, Font.BOLD);
normalfont11 = new Font(bfChinese, 11, Font.NORMAL);
boldfont11 = new Font(bfChinese, 11, Font.BOLD);
headfont = new Font(bfChinese, 14, Font.BOLD);
keyfont = new Font(bfChinese, 10, Font.BOLD);
labelfont = new Font(bfChinese, 10, Font.BOLD);
textfont = new Font(bfChinese, 10, Font.NORMAL);
} catch (Exception e) {
e.printStackTrace();
}
}
public String createPdfFile(TeContract contract) throws IOException, DocumentException, TemplateException {
if (contract == null) {
return null;
}
String fileName = contract.getTeContractNo() + "_contract.pdf";
Document document = new Document(PageSize.A4);
document.setPageCount(4);
// File file = new File(path + Globals.getFileSperator() + fileName);
File file = new File("E:\\iseafood\\test\\temp_flie\\" + fileName);
file.createNewFile();
PdfWriter.getInstance(document, new FileOutputStream(file));
document.open();
Paragraph paragraph = new Paragraph("互助组销售合同", titlefont);
paragraph.setAlignment(1);
paragraph.setIndentationLeft(12);
paragraph.setIndentationRight(12);
paragraph.setFirstLineIndent(24);
paragraph.setLeading(20f);
paragraph.setSpacingBefore(5f);
paragraph.setSpacingAfter(10f);
document.add(paragraph);
Paragraph p1 = new Paragraph("合同编号:" + contract.getTeContractNo(), normalfont11);
p1.setAlignment(2);
p1.setSpacingBefore(25f);
p1.setSpacingAfter(40f);
document.add(p1);
Paragraph p2 = new Paragraph("采购方(简称甲方):_______________", normalfont11);
p2.setAlignment(0);
p2.setSpacingBefore(7f);
p2.setSpacingAfter(10f);
document.add(p2);
Paragraph p3 = new Paragraph("供应方(简称乙方):_______________", normalfont11);
p3.setAlignment(0);
p3.setSpacingBefore(7f);
p3.setSpacingAfter(10f);
document.add(p3);
Paragraph p4 = new Paragraph("签订地点:___________________", normalfont11);
p4.setAlignment(0);
p4.setSpacingBefore(7f);
p4.setSpacingAfter(10f);
document.add(p4);
Paragraph p5 = new Paragraph("双方在平等、互利、自愿原则上,经双方友好协商,就甲方采购乙方货品达成以下协议:", normalfont11);
p5.setAlignment(0);
p5.setSpacingBefore(7f);
p5.setSpacingAfter(10f);
document.add(p5);
Paragraph p6 = new Paragraph("一、订购品种、规格、采购数量、价格具体如下:", boldfont11);
p6.setAlignment(0);
p6.setSpacingBefore(5f);
p6.setSpacingAfter(20f);
document.add(p6);
// 表格
PdfPTable table = createTable(new float[]{300, 220, 220, 220, 180, 180, 130});
table.setPaddingTop(20f);
table.addCell(createCell("品名", normalfont11, Element.ALIGN_CENTER, 1, true));
table.addCell(createCell("规格", normalfont11, Element.ALIGN_CENTER, 1, true));
table.addCell(createCell("件数(箱)", normalfont11, Element.ALIGN_CENTER, 1, true));
table.addCell(createCell("数量(公斤)", normalfont11, Element.ALIGN_CENTER, 1, true));
table.addCell(createCell("含税单价", normalfont11, Element.ALIGN_CENTER, 1, true));
table.addCell(createCell("价税合计", normalfont11, Element.ALIGN_CENTER, 1, true));
table.addCell(createCell("备注", normalfont11, Element.ALIGN_CENTER, 1, true));
BigDecimal orderTotalAmount = new BigDecimal("0");
BigDecimal orderTotalWeight = new BigDecimal("0");
Integer orderTotalQty = 0;
List<TeContractItem> items = contract.getItems();
if (items == null || items.size() == 0) {
table.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, true));
table.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, true));
table.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, true));
table.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, true));
table.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, true));
table.addCell(createCell("", textfont, Element.ALIGN_CENTER, 1, true));
} else {
int index = 0;
for (TeContractItem item : items) {
index ++;
table.addCell(createCell(item.getImCode(), textfont));
table.addCell(createCell(item.getTeiUnit(), textfont));
table.addCell(createCell(item.getTeiQuantity() + "", textfont));
table.addCell(createCell(item.getTeiWeight() + "", textfont));
table.addCell(createCell(item.getTeiPrice() + "", textfont));
orderTotalQty+= item.getTeiQuantity()==null?0:item.getTeiQuantity();
orderTotalWeight = orderTotalWeight.add(new BigDecimal(item.getTeiWeight()==null?"0.0":item.getTeiWeight()+""));
BigDecimal totalPrice = new BigDecimal(Double.toString(MathOperUtil.mul(item.getTeiQuantity() == null ? 0.0 : item.getTeiQuantity(), item.getTeiPrice() == null ? 0.0 : item.getTeiPrice())));
table.addCell(createCell(totalPrice.setScale(2, BigDecimal.ROUND_HALF_UP).toString(), textfont));
orderTotalAmount = orderTotalAmount.add(totalPrice);
if(index==1){
table.addCell(createCellRowSpan("本合同的采购数量及单价为暂定", textfont,Element.ALIGN_CENTER,items.size()+2,true));
}
}
}
table.addCell(createCell("合计", normalfont11, Element.ALIGN_CENTER, 2, true));
table.addCell(createCell(orderTotalQty+"", normalfont11, Element.ALIGN_CENTER, 1, true));
table.addCell(createCell(orderTotalWeight.toString(), normalfont11, Element.ALIGN_CENTER, 1, true));
table.addCell(createCell("合计金额", normalfont11, Element.ALIGN_CENTER, 1, true));
table.addCell(createCell(orderTotalAmount + "", normalfont11, Element.ALIGN_CENTER, 1, true));
// table.addCell(createCell("", normalfont11, Element.ALIGN_CENTER, 1, true));
table.addCell(createCell("大写合计金额:", normalfont11, Element.ALIGN_CENTER, 1, true));
table.addCell(createCell(RMBChange.change(orderTotalAmount.toString()), normalfont11, Element.ALIGN_CENTER, 5, true));
// table.addCell(createCellRowSpan("本合同的采购数量及单价为暂定", normalfont11, Element.ALIGN_CENTER, items.size() + 2, true));
//
document.add(table);
document.add(setParagraph1(new Paragraph("备注:", normalfont11)));
document.add(setParagraph1(new Paragraph("1、 合同以上采购数量及单价为暂定,最终以每次实际送货清单上的数量及时价作为结算依据。", normalfont11)));
document.add(setParagraph1(new Paragraph("2、 包装规格要求数量均按净重计算。", normalfont11)));
document.add(setParagraph1(new Paragraph("3、 外包装釆用纸箱包装。", normalfont11)));
document.add(setParagraph1(new Paragraph("二、 质量要求:", boldfont11)));
document.add(setParagraph1(new Paragraph("1、 品质新鲜无异味(异味指:氨味、臭味、硫化氢味)、大小均匀,无短秤。", normalfont11)));
document.add(setParagraph1(new Paragraph("2、 乙方需保证供给甲方的货品符合国家相关质量标准无添加违禁添加剂。", normalfont11)));
document.add(setParagraph1(new Paragraph("3、 乙方需保证销售给甲方的所有产品及其原料均来自合法渠道、均可依法用于国内销售。", normalfont11)));
document.add(setParagraph2(new Paragraph("如因上述事宜导致甲方任何经济责任和法律责任,概由乙方承担。", normalfont11)));
Paragraph p8 = new Paragraph();
Chunk f1 = new Chunk("三、结算方式:", boldfont11);
Chunk f2 = new Chunk("甲方需要预付货款,乙方再下订单采购货物,货物经乙方检验无误后,货款从预付账款中冲减。甲方预付货款汇入以下指定帐户。", normalfont11);
p8.add(f1);
p8.add(f2);
document.add(p8);
document.add(setParagraph1(new Paragraph("帐户名称:_______________", normalfont11)));
document.add(setParagraph1(new Paragraph("账号:____________________", normalfont11)));
Paragraph p9 = new Paragraph();
Chunk f3 = new Chunk("四、 交货日期:", boldfont11);
Chunk f4 = new Chunk("___年___月___日前根据甲方需求量分批交货。", normalfont11);
p9.add(f3);
p9.add(f4);
document.add(p9);
Paragraph p9a = new Paragraph();
Chunk f3a = new Chunk("五、 交货地址:", boldfont11);
Chunk f4a = new Chunk("__________________", normalfont11);
p9a.add(f3a);
p9a.add(f4a);
document.add(p9a);
Paragraph p10 = new Paragraph();
Chunk f5 = new Chunk("六、 运输方式:", boldfont11);
Chunk f6 = new Chunk("__________________ (运费由乙方承担)。", normalfont11);
p10.add(f5);
p10.add(f6);
document.add(p10);
document.add(setParagraph1(new Paragraph("七、 违约责任", boldfont11)));
document.add(setParagraph2(new Paragraph("1、甲乙双方约定,乙方必须按照本合同第二条所列产品质量配货,如有货品不符合质量要求的或未能在约定发货期发货的甲方有权拒收,同时乙方需无条件将全部预付定金退还甲方。即双方约定,甲方所购买乙方货品是按公斤计价,乙方需按合同要求包装以保证货品质。", normalfont11)));
document.add(setParagraph2(new Paragraph("八、不可抗力", boldfont11)));
document.add(setParagraph2(new Paragraph("合同期限内,由于不可抗力因素,致使任何一方不能履行合同,应立即将情况以最快方式通知对方。按照不可抗力因素对履行协议影响的程度,由双方协商解决是否解除协议,或者部分免除履行协议的责任,或者延期履行协议。", normalfont11)));
document.add(setParagraph2(new Paragraph("九、纠纷解决", boldfont11)));
document.add(setParagraph2(new Paragraph("履行本合同如有争议由双方协商解决,协商不成,双方同意提交合同签订地人民法院诉讼管辖。", normalfont11)));
document.add(setParagraph2(new Paragraph("十、其它", boldfont11)));
document.add(setParagraph2(new Paragraph("1、本协议未尽事宜,由双方协商解决,也可以本协议的附件,双方往来文件都具有同的法律效力。", normalfont11)));
document.add(setParagraph2(new Paragraph("2、本协议一式两份,经双方签字盖章后生效。甲乙双方各执一份。", normalfont11)));
Paragraph p11 = new Paragraph();
p11.setSpacingBefore(5f);
p11.setSpacingAfter(10f);
Chunk f7 = new Chunk("甲方:__________________________", normalfont11);
Chunk f8 = new Chunk(" 乙方:_________________________", normalfont11);
p11.add(f7);
p11.add(f8);
document.add(p11);
Paragraph p12 = new Paragraph();
p12.setSpacingBefore(5f);
p12.setSpacingAfter(10f);
Chunk f9 = new Chunk("法定代表人或委托人:____________", normalfont11);
Chunk f10 = new Chunk(" 互助组负责人或委托人:_________", normalfont11);
p12.add(f9);
p12.add(f10);
document.add(p12);
Paragraph p13 = new Paragraph();
p13.setSpacingBefore(5f);
p13.setSpacingAfter(10f);
Chunk f11 = new Chunk("日期:__________________________", normalfont11);
Chunk f12 = new Chunk(" 日期:___________________________", normalfont11);
p13.add(f11);
p13.add(f12);
document.add(p13);
document.close();
return fileName;
}
public Paragraph setParagraph1(Paragraph p) {
p.setAlignment(0);
p.setSpacingBefore(5f); //设置段落上空白
p.setSpacingAfter(10f); //设置段落下空白
return p;
}
public Paragraph setParagraph2(Paragraph p) {
p.setAlignment(0);
p.setSpacingBefore(5f); //设置段落上空白
p.setSpacingAfter(7f); //设置段落下空白
return p;
}
/**
* 创建单元格(指定字体、水平..)
*
* @param value
* @param font
* @param align
* @return
*/
public PdfPCell createCell(String value, Font font, int align) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setPhrase(new Phrase(value, font));
return cell;
}
/**
* 创建单元格(指定字体、水平居..、单元格跨x列合并)
*
* @param value
* @param font
* @param align
* @param colspan
* @return
*/
public PdfPCell createCell(String value, Font font, int align, int colspan) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setColspan(colspan);
cell.setPhrase(new Phrase(value, font));
return cell;
}
/**
* 创建单元格(指定字体、水平居..、单元格跨x列合并、设置单元格内边距)
*
* @param value
* @param font
* @param align
* @param colspan
* @param boderFlag
* @return
*/
public PdfPCell createCell(String value, Font font, int align, int colspan, boolean boderFlag) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setColspan(colspan);
cell.setPhrase(new Phrase(value, font));
cell.setPadding(3.0f);
if (!boderFlag) {
cell.setBorder(0);
cell.setPaddingTop(15.0f);
cell.setPaddingBottom(8.0f);
} else if (boderFlag) {
cell.setBorder(15);
cell.setBorderColorRight(BaseColor.BLACK);
cell.setBorderColor(BaseColor.BLACK);
cell.setPaddingTop(0.0f);
// cell.setPaddingBottom(15.0f);
}
return cell;
}
/**
* 创建单元格(指定字体、水平居..、单元格跨x列合并、设置单元格内边距)
*
* @param value
* @param font
* @param align
* @param rowspan
* @param boderFlag
* @return
*/
public PdfPCell createCellRowSpan(String value, Font font, int align, int rowspan, boolean boderFlag) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setRowspan(rowspan);
cell.setPhrase(new Phrase(value, font));
cell.setPadding(3.0f);
if (!boderFlag) {
cell.setBorder(0);
cell.setPaddingTop(15.0f);
cell.setPaddingBottom(8.0f);
} else if (boderFlag) {
cell.setBorder(15);
cell.setBorderColorRight(BaseColor.BLACK);
cell.setBorderColor(BaseColor.BLACK);
cell.setPaddingTop(0.0f);
// cell.setPaddingBottom(15.0f);
}
return cell;
}
/**
* 创建单元格(指定字体、水平..、边框宽度:0表示无边框、内边距)
*
* @param value
* @param font
* @param align
* @param borderWidth
* @param paddingSize
* @param flag
* @return
*/
public PdfPCell createCell(String value, Font font, int align, float[] borderWidth, float[] paddingSize, boolean flag) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setPhrase(new Phrase(value, font));
cell.setBorderWidthLeft(borderWidth[0]);
cell.setBorderWidthRight(borderWidth[1]);
cell.setBorderWidthTop(borderWidth[2]);
cell.setBorderWidthBottom(borderWidth[3]);
cell.setPaddingTop(paddingSize[0]);
cell.setPaddingBottom(paddingSize[1]);
if (flag) {
cell.setColspan(2);
}
return cell;
}
/*
* 创建单元格(指定字体)
* @param value
* @param font
* @return
*/
public PdfPCell createCell(String value, Font font) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setPhrase(new Phrase(value, font));
return cell;
}
/*
* 创建默认列宽,指定列数、水平(居中、右、左)的表格
* @param colNumber
* @param align
* @return
*/
public PdfPTable createTable(int colNumber, int align) {
PdfPTable table = new PdfPTable(colNumber);
try {
table.setTotalWidth(maxWidth);
table.setLockedWidth(true);
table.setHorizontalAlignment(align);
table.getDefaultCell().setBorder(1);
} catch (Exception e) {
e.printStackTrace();
}
return table;
}
/**
* 创建指定列宽、列数的表格
*
* @param widths
* @return
*/
public PdfPTable createTable(float[] widths) {
PdfPTable table = new PdfPTable(widths);
try {
table.setTotalWidth(maxWidth);
table.setLockedWidth(true);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setBorder(1);
} catch (Exception e) {
e.printStackTrace();
}
return table;
}
/**
* 创建空白的表格
*
* @return
*/
public PdfPTable createBlankTable() {
PdfPTable table = new PdfPTable(1);
table.getDefaultCell().setBorder(0);
table.addCell(createCell("", keyfont));
table.setSpacingAfter(20.0f);
table.setSpacingBefore(20.0f);
return table;
}
public static void main(String[] args) throws Exception {
try {
// 1.新建document对象
// Document document = new Document(PageSize.A4);// 建立一个Document对象
// document.setPageCount(5);//设置生成pdf页数
// // 2.建立一个书写器(Writer)与document对象关联
// File file = new File("E:\\k100\\BRANDSLOGO\\aaa22.pdf");
// file.createNewFile();
// PdfWriter.getInstance(document, new FileOutputStream(file));
// // 3.打开文档
// document.open();
//4.生成PDF
DownloadPDFUtil downloadPDFUtil = new DownloadPDFUtil();
TeContract teContract = new TeContract();
teContract.setCpName("cpName");
teContract.setCpId(123456);
teContract.setEndTime("2022-03-02 12:27:11");
teContract.setSpId(4567);
teContract.setTeAmount(1.00d);
teContract.setTeStatus("a");
teContract.setTeContractNo("abcasdasfsdfasdfasdf2323232323");
List<TeContractItem> items = new ArrayList<>();
for (int i = 0; i < 10; i++) {
TeContractItem item = new TeContractItem();
item.setCompId(123);
item.setImCode("IMCODE" + i);
item.setTeiQuantity(100 + i);
item.setTeiWeight(77d);
item.setTeiPrice(17d);
item.setTeContractNo("ctno12343434343434");
item.setTeiCreatetime(new Date());
items.add(item);
}
teContract.setItems(items);
downloadPDFUtil.createPdfFile(teContract);
// downloadPDFUtil.generatePDF(document);
// 5.关闭文档
// document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void tableOutput2(String pdfFilePath) throws Exception {
Document document = new Document(PageSize.A4);
PdfWriter.getInstance(document, new FileOutputStream(pdfFilePath));
// 添加 中文信息
BaseFont bfCN = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",
false);
// 设置字体大小
Font fontCN = new Font(bfCN, 12, Font.NORMAL, BaseColor.BLACK);
// 打开文档
document.open();
PdfPCell cell = null;
// 创建需要填入文档的元素
PdfPTable table = new PdfPTable(3);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.setWidthPercentage(100);
cell = new PdfPCell(new Paragraph(
new Paragraph(" A ", new Font(fontCN))));
cell.setColspan(2);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(
new Paragraph(" D ", new Font(fontCN))));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setRowspan(2);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(
new Paragraph("B ", new Font(fontCN))));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(
new Paragraph("C ", new Font(fontCN))));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(
new Paragraph(" ", new Font(fontCN))));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
document.add(table);
// 6.关闭文档
document.close();
}
}
使用和注释都在工具类代码中