入门链接 点击打开链接
PS:你需要的一些功能可能在文章最后
1:maven环境
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.3.0</version>
</dependency>
2:画一个PDF挺简单的 直接上代码
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.text.DecimalFormat;
import java.util.Random;
import java.util.ResourceBundle;
import com.huiju.entity.TTablePersonal;//实体 所有字段可按字符串处理
import com.huiju.vo.PDFPersonalDataVo;//实体 所有字段可按字符串处理
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
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.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
/**
* @author 个人投资者模板
*/
public class Demo {
/**
* 根据模板数据生成一个带表格/图片/指定位置文本的PDF
* @param personal 模板实体
* @param dataVo 数据实体
*/
@SuppressWarnings("unused")
public static String testPDF(TTablePersonal personal,PDFPersonalDataVo dataVo) throws DocumentException, MalformedURLException, IOException {
//页面左边距、右边距、上边距和下边距
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
//文档对象的引用 写入的文件的绝对名称 TODO 文件路径
ResourceBundle resourceBundle = ResourceBundle.getBundle("file");
String FILEPATH = resourceBundle.getString("personal_can_remove");
Long timeString = System.currentTimeMillis();
String path = FILEPATH + timeString + (new Random().nextInt(900) + 100) + ".pdf";// TODO 文件名称
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path));
//打开文档
document.open();
//创建章
Chapter chapter1 = new Chapter(0);
float X = 37;//位置
float Y = 739;
float H1 = 16;//行间距
float H2 = 18;
float H3 = 22;
// 1--logo
Image image1 = Image.getInstance(personal.getLogoImg());// 添加图片 将 Image 添加到主 Document 中
image1.scaleAbsolute(68f, 54f);//图片大小
image1.setAbsolutePosition(X, Y);//位置X Y (左下角计数)
chapter1.add(image1);
// 2--添加文本
String FONT = resourceBundle.getString("pdf_font");
Font font1 = new Font(BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED));
font1.setSize(10);
Font font_1 = new Font(BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED));
font_1.setSize(10);
font_1.setStyle(Font.BOLD);
Font font_test = new Font(BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED));
font_test.setSize(10);
font_test.setColor(255, 255, 255);
chapter1.add(new Paragraph(" ", font1));//空行
Paragraph head1 = new Paragraph(personal.getAddress(), font1);//模板地址
head1.setAlignment(2);
head1.setLeading(H1);
chapter1.add(head1);
Paragraph head2 = new Paragraph(personal.getTelephone(), font1);//模板电话
head2.setAlignment(2);
head2.setLeading(H1);
chapter1.add(head2);
Paragraph head3 = new Paragraph(personal.getCode(), font1);//模板邮编
head3.setAlignment(2);
head3.setLeading(H1);
chapter1.add(head3);
Paragraph head4 = new Paragraph(personal.getHead1() + " " + dataVo.getPersonName(), font1);//模板至:
head4.setAlignment(3);
head4.setLeading(H2);
chapter1.add(head4);
Paragraph head6 = new Paragraph(personal.getHead2() + " " + dataVo.getFundCompanyName(), font1);//模板主题:
head6.setAlignment(3);
head6.setLeading(H2);
chapter1.add(head6);
Paragraph head_6 = new Paragraph(personal.getHead3(), font1);//模板尊敬的投资人
head_6.setAlignment(3);
head_6.setLeading(H2);
chapter1.add(head_6);
Paragraph head_7 = new Paragraph(personal.getHead4(), font1);//模板您好
head_7.setAlignment(3);
head_7.setLeading(H2);
chapter1.add(head_7);
Paragraph head7 = new Paragraph(" " + dataVo.getFundCompanyName() + personal.getHead5(), font1);//基金公司 + 上次分配到
head7.setAlignment(3);
head7.setLeading(H2);
chapter1.add(head7);
// 3--添加表格
Font font2 = new Font(BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED));
font2.setSize(10);
font2.setStyle(Font.BOLD);//加粗
font2.setColor(new BaseColor(255, 255, 255));
float[] widths = {0.1f, 0.15f, 0.25f, 0.25f, 0.25f};//表格列的宽度
PdfPTable table1 = new PdfPTable(widths);//表格初始化
table1.setSpacingBefore(8);//开始位置
table1.setSpacingAfter(10);
table1.setTotalWidth(480);//宽度
table1.setLockedWidth(true);
//第一个表格头部信息
String[] table_1_head = {"序号", "项目名称", "分红金额", "本金退出金额", "合计金额"};
for (int i = 0; i < 5; i++) {
PdfPCell pCell1 = new PdfPCell(new Phrase(table_1_head[i], font2));//单元格
pCell1.setBackgroundColor(new BaseColor(153, 1, 52));//背景颜色
pCell1.setFixedHeight(20);//单元格宽度
pCell1.setHorizontalAlignment(1);//居中
pCell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
table1.addCell(pCell1);//添加
}
// -- 第一个列表
DecimalFormat decimalFormat = new DecimalFormat("0.00");//金钱显示格式
Integer size1 = dataVo.getData1().size();
Integer table_h1 = size1;//行数
Double[] table_1_h = {0.00, 0.00, 0.00};
for (int i = 0; i < size1; i++) {
//序号
PdfPCell pCell_data_xh = new PdfPCell(new Phrase(i + 1 +"", font1));
pCell_data_xh.setHorizontalAlignment(1);
pCell_data_xh.setVerticalAlignment(Element.ALIGN_MIDDLE);
pCell_data_xh.setFixedHeight(18);
table1.addCell(pCell_data_xh);
//项目名称
PdfPCell pCell_data_xmmc = new PdfPCell(new Phrase(dataVo.getData1().get(i).getItemCompanyName(), font1));
pCell_data_xmmc.setHorizontalAlignment(1);
pCell_data_xmmc.setVerticalAlignment(Element.ALIGN_MIDDLE);
pCell_data_xmmc.setFixedHeight(18);
table1.addCell(pCell_data_xmmc);
//分红金额
table_1_h[0] +=dataVo.getData1().get(i).getMoneyOne();
PdfPCell pCell_data_fhje = new PdfPCell(new Phrase(decimalFormat.format(dataVo.getData1().get(i).getMoneyOne()), font1));
pCell_data_fhje.setHorizontalAlignment(1);
pCell_data_fhje.setVerticalAlignment(Element.ALIGN_MIDDLE);
pCell_data_fhje.setFixedHeight(18);
table1.addCell(pCell_data_fhje);
//本金退出金额
table_1_h[1] +=dataVo.getData1().get(i).getMoneyTwo();
PdfPCell pCell_data_bjtu = new PdfPCell(new Phrase(decimalFormat.format(dataVo.getData1().get(i).getMoneyTwo()), font1));
pCell_data_bjtu.setHorizontalAlignment(1);
pCell_data_bjtu.setVerticalAlignment(Element.ALIGN_MIDDLE);
pCell_data_bjtu.setFixedHeight(18);
table1.addCell(pCell_data_bjtu);
//合计金额
table_1_h[2] +=dataVo.getData1().get(i).getMoneyThree();
PdfPCell pCell_data_hjje = new PdfPCell(new Phrase(decimalFormat.format(dataVo.getData1().get(i).getMoneyThree()), font1));
pCell_data_hjje.setHorizontalAlignment(1);
pCell_data_hjje.setVerticalAlignment(Element.ALIGN_MIDDLE);
pCell_data_hjje.setFixedHeight(18);
table1.addCell(pCell_data_hjje);
}
// --表格合计
PdfPCell pCell6 = new PdfPCell(new Phrase("合计", font1));
pCell6.setFixedHeight(20);
pCell6.setHorizontalAlignment(1);
pCell6.setVerticalAlignment(Element.ALIGN_MIDDLE);
pCell6.setRowspan(1);
pCell6.setColspan(2);
table1.addCell(pCell6);
// 合计 table_1_h 数组
for (int i = 0; i < 3; i++) {
PdfPCell pCel9 = new PdfPCell(new Phrase(decimalFormat.format(table_1_h[i]), font1));
pCel9.setFixedHeight(18);
pCel9.setHorizontalAlignment(1);
pCel9.setVerticalAlignment(Element.ALIGN_MIDDLE);
table1.addCell(pCel9);
}
// --表格1完
chapter1.add(table1);
//添加数据 说明文本以及表格2数据
String[] table_2_1_s = {"先前分配合计", PDFUtils.getNowDateYM(), "费用", "本次分配合计"};
Double[] table_2_1 = {
0.0, 0.0, 0.0,
0.0, 0.0, 0.0,
0.0, 0.0, 0.0,
0.0, 0.0, 0.0, };//组装数据
for (int i = 0; i < dataVo.getData2().size(); i++) {
if (dataVo.getData2().get(i).getDataMonth().equals("先前分配合计")) {
table_2_1[0] = dataVo.getData2().get(i).getMoneyOne();
table_2_1[1] = dataVo.getData2().get(i).getMoneyTwo();
table_2_1[2] = dataVo.getData2().get(i).getMoneyThree();
} else if (dataVo.getData2().get(i).getDataMonth().equals("费用")) {
table_2_1[6] = dataVo.getData2().get(i).getMoneyOne();
table_2_1[7] = dataVo.getData2().get(i).getMoneyTwo();
table_2_1[8] = dataVo.getData2().get(i).getMoneyThree();
} else if (dataVo.getData2().get(i).getDataMonth().equals("本次分配合计")) {
table_2_1[9] = dataVo.getData2().get(i).getMoneyOne();
table_2_1[10] = dataVo.getData2().get(i).getMoneyTwo();
table_2_1[11] = dataVo.getData2().get(i).getMoneyThree();
} else {
table_2_1_s[1] = dataVo.getData2().get(i).getDataMonth();//数据库中的月份
table_2_1[3] = dataVo.getData2().get(i).getMoneyOne();
table_2_1[4] = dataVo.getData2().get(i).getMoneyTwo();
table_2_1[5] = dataVo.getData2().get(i).getMoneyThree();
}
}
//说明文本13-16
Double money1 = table_2_1[5];//5
Double money2 = table_2_1[8];//8
Double money3 = table_2_1[11];//11
String date1 = PDFUtils.getNowDateYMD();
String company1 = dataVo.getFundCompanyName();
Paragraph head136 = new Paragraph(" "
+ personal.getText1() + decimalFormat.format(money1)
+ personal.getText2() + decimalFormat.format(money2)
+ personal.getText3() + decimalFormat.format(money3)
+ personal.getText4() + date1
+ company1 + personal.getText5()
+ personal.getText6(), font1);
head136.setAlignment(3);
head136.setLeading(H2);
chapter1.add(head136);
//说明文本
Paragraph head_136_1 = new Paragraph(" " + personal.getText7(), font1);
head_136_1.setAlignment(3);
head_136_1.setLeading(H2);
chapter1.add(head_136_1);
Paragraph head_136_2 = new Paragraph(personal.getText8() + " ", font1);
head_136_2.setAlignment(2);
head_136_2.setLeading(H2);
chapter1.add(head_136_2);
// --表格2
PdfPTable table2 = new PdfPTable(widths);//表格初始化
table2.setSpacingBefore(8);//开始位置
table2.setSpacingAfter(10);
table2.setTotalWidth(500);//宽度
table2.setLockedWidth(true);
String[] table_2_head = {"序号", "分配时间", "分红金额", "本金金额", "分配总计"};
for (int i = 0; i < 5; i++) {
PdfPCell pCell_2_1 = new PdfPCell(new Phrase(table_2_head[i], font2));//单元格
pCell_2_1.setBackgroundColor(new BaseColor(153, 1, 52));//背景颜色
pCell_2_1.setFixedHeight(20);//单元格宽度
pCell_2_1.setHorizontalAlignment(1);//居中
pCell_2_1.setVerticalAlignment(Element.ALIGN_MIDDLE);
table2.addCell(pCell_2_1);//添加
}
//序号1
PdfPCell pCell_2_17_1 = new PdfPCell(new Phrase("1", font1));
pCell_2_17_1.setFixedHeight(18);
pCell_2_17_1.setHorizontalAlignment(1);
pCell_2_17_1.setVerticalAlignment(Element.ALIGN_MIDDLE);
table2.addCell(pCell_2_17_1);
PdfPCell pCell_2_17_2 = new PdfPCell(new Phrase(table_2_1_s[0], font1));
pCell_2_17_2.setFixedHeight(18);
pCell_2_17_2.setHorizontalAlignment(1);
pCell_2_17_2.setVerticalAlignment(Element.ALIGN_MIDDLE);
table2.addCell(pCell_2_17_2);
//数据添加到表中
for (int i = 0; i < 3; i++) {
PdfPCell pCell_data_17 = new PdfPCell(new Phrase(decimalFormat.format(table_2_1[i]), font1));
pCell_data_17.setHorizontalAlignment(1);
pCell_data_17.setVerticalAlignment(Element.ALIGN_MIDDLE);
pCell_data_17.setFixedHeight(18);
table2.addCell(pCell_data_17);
}
//序号2
PdfPCell pCell_18 = new PdfPCell(new Phrase("2", font1));
pCell_18.setFixedHeight(20);
pCell_18.setHorizontalAlignment(1);
pCell_18.setVerticalAlignment(Element.ALIGN_MIDDLE);
pCell_18.setRowspan(3);//列数
pCell_18.setColspan(1);
table2.addCell(pCell_18);
//月份
PdfPCell pCell_date_18 = new PdfPCell(new Phrase(table_2_1_s[1], font1));
pCell_date_18.setHorizontalAlignment(1);
pCell_date_18.setVerticalAlignment(Element.ALIGN_MIDDLE);
pCell_date_18.setFixedHeight(18);
table2.addCell(pCell_date_18);
// 月份添加数据_18
for (int i = 3; i < 6; i++) {
PdfPCell pCell_data_18 = new PdfPCell(new Phrase(decimalFormat.format(table_2_1[i]), font1));
pCell_data_18.setHorizontalAlignment(1);
pCell_data_18.setVerticalAlignment(Element.ALIGN_MIDDLE);
pCell_data_18.setFixedHeight(18);
table2.addCell(pCell_data_18);
}
//费用
PdfPCell pCell_fy_18 = new PdfPCell(new Phrase(table_2_1_s[2], font1));
pCell_fy_18.setHorizontalAlignment(1);
pCell_fy_18.setVerticalAlignment(Element.ALIGN_MIDDLE);
pCell_fy_18.setFixedHeight(18);
table2.addCell(pCell_fy_18);
// 费用添加数据_18
for (int i = 6; i < 9; i++) {
PdfPCell pCell_data_18 = new PdfPCell(new Phrase(decimalFormat.format(table_2_1[i]), font1));
pCell_data_18.setHorizontalAlignment(1);
pCell_data_18.setVerticalAlignment(Element.ALIGN_MIDDLE);
pCell_data_18.setFixedHeight(18);
table2.addCell(pCell_data_18);
}
//合计
PdfPCell pCell_hj_18 = new PdfPCell(new Phrase(table_2_1_s[3], font1));
pCell_hj_18.setHorizontalAlignment(1);
pCell_hj_18.setVerticalAlignment(Element.ALIGN_MIDDLE);
pCell_hj_18.setFixedHeight(18);
table2.addCell(pCell_hj_18);
// 合计添加数据_18
for (int i = 9; i < 12; i++) {
PdfPCell pCell_data_18 = new PdfPCell(new Phrase(decimalFormat.format(table_2_1[i]), font1));
pCell_data_18.setHorizontalAlignment(1);
pCell_data_18.setVerticalAlignment(Element.ALIGN_MIDDLE);
pCell_data_18.setFixedHeight(18);
table2.addCell(pCell_data_18);
}
//累计分配
PdfPCell pCell_21 = new PdfPCell(new Phrase("累计分配", font1));
pCell_21.setFixedHeight(20);
pCell_21.setHorizontalAlignment(1);
pCell_21.setVerticalAlignment(Element.ALIGN_MIDDLE);
pCell_21.setRowspan(1);
pCell_21.setColspan(2);
table2.addCell(pCell_21);
//分配数据
Double[] pCell_fp_21_data = {
table_2_1[3] + table_2_1[6] + table_2_1[9],
table_2_1[4] + table_2_1[7] + table_2_1[10],
table_2_1[5] + table_2_1[8] + table_2_1[11]};
for (int i = 0; i < 3; i++) {
PdfPCell pCell_data_18 = new PdfPCell(new Phrase(decimalFormat.format(pCell_fp_21_data[i]), font1));
pCell_data_18.setHorizontalAlignment(1);
pCell_data_18.setVerticalAlignment(Element.ALIGN_MIDDLE);
pCell_data_18.setFixedHeight(18);
table2.addCell(pCell_data_18);
}
//添加表格2
chapter1.add(table2);
//签章 文本不分页
Paragraph head_22_1 = new Paragraph(" " + personal.getText9(), font1);
head_22_1.setAlignment(3);
head_22_1.setLeading(H1);
chapter1.add(head_22_1);
Paragraph head_22_2 = new Paragraph(" " + personal.getText10(), font1);
head_22_2.setAlignment(3);
head_22_2.setLeading(H2);
chapter1.add(head_22_2);
Paragraph head_22_3 = new Paragraph(" " + personal.getText11(), font1);
head_22_3.setAlignment(3);
head_22_3.setLeading(H2);
chapter1.add(head_22_3);
if (table_h1%41 == 10 || table_h1%41 == 11 || table_h1%41 == 12) {
document.add(chapter1);
//公司信息分页
Chapter chapterf = new Chapter(0);
Paragraph head22 = new Paragraph(dataVo.getPersonName(), font1);
head22.setAlignment(2);
head22.setLeading(H2);
chapterf.add(head22);
Paragraph head23 = new Paragraph(dataVo.getFundCompanyName(), font1);
head23.setAlignment(2);
head23.setLeading(H2);
chapterf.add(head23);
Paragraph head24 = new Paragraph(PDFUtils.getNowDateYMD(), font1);
head24.setAlignment(2);
head24.setLeading(H2);
chapterf.add(head24);
Paragraph head_test = new Paragraph("QIANZHANG", font_test);
head_test.setAlignment(2);
head_test.setLeading(H2);
chapterf.add(head_test);
document.add(chapterf);
} else {
//公司信息不分页
Paragraph head22 = new Paragraph(dataVo.getPersonName(), font1);
head22.setAlignment(2);
head22.setLeading(H2);
chapter1.add(head22);
Paragraph head23 = new Paragraph(dataVo.getFundCompanyName(), font1);
head23.setAlignment(2);
head23.setLeading(H2);
chapter1.add(head23);
Paragraph head24 = new Paragraph(PDFUtils.getNowDateYMD(), font1);
head24.setAlignment(2);
head24.setLeading(H2);
chapter1.add(head24);
Paragraph head_test = new Paragraph("QIANZHANG", font_test);
head_test.setAlignment(2);
head_test.setLeading(H2);
chapter1.add(head_test);
//第一页完
document.add(chapter1);
}
/****************************************************************/
//附件一
/****************************************************************/
//创建章
Chapter chapter2 = new Chapter(0);
Paragraph head_25_1 = new Paragraph(personal.getTextF1(), font1);
head_25_1.setAlignment(3);
head_25_1.setLeading(H1);
chapter2.add(head_25_1);
Paragraph head_25_2 = new Paragraph(dataVo.getFundCompanyName(), font1);
head_25_2.setAlignment(3);
head_25_2.setLeading(H1);
chapter2.add(head_25_2);
Paragraph head_25_3 = new Paragraph(personal.getTextF2(), font1);
head_25_3.setAlignment(3);
head_25_3.setLeading(H1);
chapter2.add(head_25_3);
Paragraph head_25_4 = new Paragraph(personal.getTextF3(), font1);
head_25_4.setAlignment(2);
head_25_4.setLeading(H1);
chapter2.add(head_25_4);
// --附件表格
float[] widths_3 = {0.25f, 0.1f, 0.3f, 0.175f, 0.175f};
PdfPTable table3 = new PdfPTable(widths_3);//表格初始化
table3.setSpacingBefore(8);//开始位置
table3.setSpacingAfter(10);
table3.setTotalWidth(560);//宽度
table3.setLockedWidth(true);
table3.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
//附件表格表头 投资人code
String[] table_fj_1 = {" ", "索引", "计算公式", dataVo.getPersonCode(), "本基金合计"};
for (int i = 0; i < 5; i++) {
PdfPCell pCell_3_head = new PdfPCell(new Phrase(table_fj_1[i], font1));//单元格
pCell_3_head.setBorder(0);
table3.addCell(pCell_3_head);
}
//空格 + 投资人名称
String[] table_fj_2 = {" ", " ", " ", dataVo.getPersonName(), " "};
for (int i = 0; i < 5; i++) {
PdfPCell pCell_3_head = new PdfPCell(new Phrase(table_fj_2[i], font1));//单元格
pCell_3_head.setBorder(0);
table3.addCell(pCell_3_head);
}
//第一个固定表格
String[] pCell_3_1_1_data = {
"先前合伙人实缴出资额", "A1", " ", decimalFormat.format(dataVo.getData3().get(0).getMoneyFour()), decimalFormat.format(dataVo.getData3().get(0).getMoneyFive()),
"后续募集合伙人实缴出资额", "A2", " ", decimalFormat.format(dataVo.getData3().get(0).getMoneySix()), decimalFormat.format(dataVo.getData3().get(0).getMoneySeven()),
"实缴出资额", "A", "A1+A2", decimalFormat.format(dataVo.getData3().get(0).getMoneyEight()), decimalFormat.format(dataVo.getData3().get(0).getMoneyNine()),
"扣除管理费后实缴出资额", "B", dataVo.getData3().get(0).getGs(), decimalFormat.format(dataVo.getData3().get(0).getMoneyTen()), decimalFormat.format(dataVo.getData3().get(0).getMoneyEleven()),};
for (int i = 0; i < 20; i++) {
PdfPCell pCell_3_1_1 = new PdfPCell(new Phrase(pCell_3_1_1_data[i], font1));//单元格
pCell_3_1_1.setBorder(0);
table3.addCell(pCell_3_1_1);
}
//数据表格
int charNum = 67;//C-Z ASCII:67-90
String[] hg_money_gs = {"", ""};//扣除博时费用后分配合计 TODO
Double[] hg_money_ge = {
0.0, 0.0,
0.0, 0.0,
0.0, 0.0,
0.0, 0.0,
0.0, 0.0,};
Integer data3Size = dataVo.getData3().size();
for (int i = 0; i < data3Size; i++) {
Integer charNumAdd = 1;
//空行
for (int j = 0; j < 5; j++) {
PdfPCell pCell_3_1_k = new PdfPCell(new Phrase(" ", font1));
pCell_3_1_k.setBorder(0);
table3.addCell(pCell_3_1_k);
}
//项目行
PdfPCell pCell_3_1 = new PdfPCell(new Phrase(dataVo.getData3().get(i).getItemCompanyName(), font1));
pCell_3_1.setBorder(0);
table3.addCell(pCell_3_1);
for (int j = 0; j < 4; j++) {
PdfPCell pCell_3_1_k = new PdfPCell(new Phrase(" ", font1));
pCell_3_1_k.setBorder(0);
table3.addCell(pCell_3_1_k);
}
//投资成本
if (dataVo.getData3().get(i).getMoneyThree() != null && dataVo.getData3().get(i).getMoneyThree() != 0.0) {
PdfPCell pCell_3_1_tzcb_1 = new PdfPCell(new Phrase("投资成本", font1));
pCell_3_1_tzcb_1.setBorder(0);
table3.addCell(pCell_3_1_tzcb_1);
Character pCell_3_1_tzcb_1_xh = (char) charNum;
PdfPCell pCell_3_1_tzcb_2 = new PdfPCell(new Phrase(pCell_3_1_tzcb_1_xh.toString(), font1));
pCell_3_1_tzcb_2.setBorder(0);
table3.addCell(pCell_3_1_tzcb_2);
String pCell_3_1_tzcb_1_jsgs = "B/(B合计)*(" + pCell_3_1_tzcb_1_xh + "合计)";
PdfPCell pCell_3_1_tzcb_3 = new PdfPCell(new Phrase(pCell_3_1_tzcb_1_jsgs, font1));
pCell_3_1_tzcb_3.setBorder(0);
table3.addCell(pCell_3_1_tzcb_3);
PdfPCell pCell_3_1_tzcb_4 = new PdfPCell(new Phrase(decimalFormat.format(dataVo.getData3().get(i).getMoneyThree()), font1));
pCell_3_1_tzcb_4.setBorder(0);
table3.addCell(pCell_3_1_tzcb_4);
PdfPCell pCell_3_1_tzcb_5 = new PdfPCell(new Phrase(decimalFormat.format(dataVo.getData4().get(dataVo.getData3().get(i).getItemCompanyName()).getMoneyThree()), font1));
pCell_3_1_tzcb_5.setBorder(0);
table3.addCell(pCell_3_1_tzcb_5);
}
//分红
if (dataVo.getData3().get(i).getMoneyOne() != null && dataVo.getData3().get(i).getMoneyOne() != 0.0) {
PdfPCell pCell_3_1_tzcb_1 = new PdfPCell(new Phrase("分红金额", font1));
pCell_3_1_tzcb_1.setBorder(0);
table3.addCell(pCell_3_1_tzcb_1);
Character pCell_3_1_tzcb_1_xh = (char) charNum;
PdfPCell pCell_3_1_tzcb_2 = new PdfPCell(new Phrase(pCell_3_1_tzcb_1_xh.toString() + charNumAdd.toString(), font1));
pCell_3_1_tzcb_2.setBorder(0);
table3.addCell(pCell_3_1_tzcb_2);
String pCell_3_1_tzcb_1_jsgs = pCell_3_1_tzcb_1_xh +"/(" + pCell_3_1_tzcb_1_xh + "合计)*(" + pCell_3_1_tzcb_1_xh.toString() + charNumAdd.toString() + "合计)";
PdfPCell pCell_3_1_tzcb_3 = new PdfPCell(new Phrase(pCell_3_1_tzcb_1_jsgs, font1));
pCell_3_1_tzcb_3.setBorder(0);
table3.addCell(pCell_3_1_tzcb_3);
PdfPCell pCell_3_1_tzcb_4 = new PdfPCell(new Phrase(decimalFormat.format(dataVo.getData3().get(i).getMoneyOne()), font1));
pCell_3_1_tzcb_4.setBorder(0);
table3.addCell(pCell_3_1_tzcb_4);
PdfPCell pCell_3_1_tzcb_5 = new PdfPCell(new Phrase(decimalFormat.format(dataVo.getData4().get(dataVo.getData3().get(i).getItemCompanyName()).getMoneyOne()), font1));
pCell_3_1_tzcb_5.setBorder(0);
table3.addCell(pCell_3_1_tzcb_5);
hg_money_gs[0] += pCell_3_1_tzcb_1_xh.toString() + charNumAdd.toString();//分红合计公式
hg_money_ge[0] += dataVo.getData3().get(i).getMoneyOne();//个人分红合计
hg_money_ge[1] += dataVo.getData4().get(dataVo.getData3().get(i).getItemCompanyName()).getMoneyOne();//项目分红合计
//hg_money_ge[0] +=
charNumAdd++;
}
//本金退出
if (dataVo.getData3().get(i).getMoneyTwo() != null && dataVo.getData3().get(i).getMoneyTwo() != 0.0) {
PdfPCell pCell_3_1_tzcb_1 = new PdfPCell(new Phrase("本金退出", font1));
pCell_3_1_tzcb_1.setBorder(0);
table3.addCell(pCell_3_1_tzcb_1);
Character pCell_3_1_tzcb_1_xh = (char) charNum;
PdfPCell pCell_3_1_tzcb_2 = new PdfPCell(new Phrase(pCell_3_1_tzcb_1_xh.toString() + charNumAdd.toString(), font1));
pCell_3_1_tzcb_2.setBorder(0);
table3.addCell(pCell_3_1_tzcb_2);
String pCell_3_1_tzcb_1_jsgs = "A/(A合计)*(" + pCell_3_1_tzcb_1_xh + charNumAdd.toString() + "合计)";
PdfPCell pCell_3_1_tzcb_3 = new PdfPCell(new Phrase(pCell_3_1_tzcb_1_jsgs, font1));
pCell_3_1_tzcb_3.setBorder(0);
table3.addCell(pCell_3_1_tzcb_3);
PdfPCell pCell_3_1_tzcb_4 = new PdfPCell(new Phrase(decimalFormat.format(dataVo.getData3().get(i).getMoneyTwo()), font1));
pCell_3_1_tzcb_4.setBorder(0);
table3.addCell(pCell_3_1_tzcb_4);
PdfPCell pCell_3_1_tzcb_5 = new PdfPCell(new Phrase(decimalFormat.format(dataVo.getData4().get(dataVo.getData3().get(i).getItemCompanyName()).getMoneyTwo()), font1));
pCell_3_1_tzcb_5.setBorder(0);
table3.addCell(pCell_3_1_tzcb_5);
hg_money_gs[1] += pCell_3_1_tzcb_1_xh.toString() + charNumAdd.toString();//本金退出合计公式
hg_money_ge[2] += dataVo.getData3().get(i).getMoneyTwo();//本金退出个人合计
hg_money_ge[3] += dataVo.getData4().get(dataVo.getData3().get(i).getItemCompanyName()).getMoneyTwo();//项目本金退出合计
charNumAdd++;
}
//博时费用
if (dataVo.getData3().get(i).getMoneyTwo() != null && dataVo.getData3().get(i).getMoneyTwo() != 0.0) {
PdfPCell pCell_3_1_tzcb_1 = new PdfPCell(new Phrase("博时费用", font1));
pCell_3_1_tzcb_1.setBorder(0);
table3.addCell(pCell_3_1_tzcb_1);
Character pCell_3_1_tzcb_1_xh = (char) charNum;
PdfPCell pCell_3_1_tzcb_2 = new PdfPCell(new Phrase(pCell_3_1_tzcb_1_xh.toString() + charNumAdd.toString(), font1));
pCell_3_1_tzcb_2.setBorder(0);
table3.addCell(pCell_3_1_tzcb_2);
String pCell_3_1_tzcb_1_jsgs = "A/(A合计)*(" + pCell_3_1_tzcb_1_xh + charNumAdd.toString() + "合计)";
PdfPCell pCell_3_1_tzcb_3 = new PdfPCell(new Phrase(pCell_3_1_tzcb_1_jsgs, font1));
pCell_3_1_tzcb_3.setBorder(0);
table3.addCell(pCell_3_1_tzcb_3);
PdfPCell pCell_3_1_tzcb_4 = new PdfPCell(new Phrase(decimalFormat.format(dataVo.getData3().get(i).getMoney()), font1));
pCell_3_1_tzcb_4.setBorder(0);
table3.addCell(pCell_3_1_tzcb_4);
PdfPCell pCell_3_1_tzcb_5 = new PdfPCell(new Phrase(decimalFormat.format(dataVo.getData4().get(dataVo.getData3().get(i).getItemCompanyName()).getMoney()), font1));
pCell_3_1_tzcb_5.setBorder(0);
table3.addCell(pCell_3_1_tzcb_5);
hg_money_ge[7] += dataVo.getData3().get(i).getMoney();
hg_money_ge[8] += dataVo.getData4().get(dataVo.getData3().get(i).getItemCompanyName()).getMoney();
charNumAdd++;
}
charNum++;
}
//索引计数
String[] sy = {"" + (char) charNum++, "" + (char) charNum++, "" + (char) charNum++, "" + (char) charNum++, "" + (char) charNum++};//索引
String[] gs = {sy[0] + "+" + sy[1], sy[3] + "-" + sy[2]};//公式
//附件一 合计 TODO
String[] data4_hg = {
"本次分红合计", sy[0], hg_money_gs[0], decimalFormat.format(hg_money_ge[0]), decimalFormat.format(hg_money_ge[1]),
"本次本金分配合计", sy[1], hg_money_gs[1], decimalFormat.format(hg_money_ge[2]), decimalFormat.format(hg_money_ge[3]),
"基金分配总计 ", sy[2], gs[0], decimalFormat.format(hg_money_ge[0] + hg_money_ge[2]), decimalFormat.format(hg_money_ge[1] + hg_money_ge[3]),
"博时费用 ", sy[3], " ", decimalFormat.format(hg_money_ge[7]), decimalFormat.format(hg_money_ge[8]),
"扣除博时费用后分配合计", sy[4], gs[1], decimalFormat.format(hg_money_ge[7] - (hg_money_ge[0] + hg_money_ge[2])), decimalFormat.format(hg_money_ge[8] - (hg_money_ge[1] + hg_money_ge[3])),};
//空行
for (int j = 0; j < 5; j++) {
PdfPCell pCell_3_1_k = new PdfPCell(new Phrase(" ", font1));
pCell_3_1_k.setBorder(0);
table3.addCell(pCell_3_1_k);
}
//数据
for (int i = 0; i < 25; i++) {
PdfPCell pCell_3_1_tzcb_1 = new PdfPCell(new Phrase(data4_hg[i], font1));
pCell_3_1_tzcb_1.setBorder(0);
table3.addCell(pCell_3_1_tzcb_1);
}
//合计TODO
chapter2.add(table3);
document.add(chapter2);
//关闭文档
document.close();
//添加文本后的位置
String path_1 = PDFUtils.addIdCard(dataVo.getPersonCode(), path);
String pdfFileOutPath = PDFUtils.addZhang(null, "1", path_1 , dataVo.getData1().get(0).getBzOne(), dataVo.getPersonCode());
//删除临时文件
//1:path
//2:path_1
File file1 = new File(path);
File file2 = new File(path_1);
// 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
if (file1.exists() && file1.isFile()) {
file1.delete();
System.out.println("删除:" + path);
}
if (file2.exists() && file2.isFile()) {
file2.delete();
System.out.println("删除:" + path_1);
}
return pdfFileOutPath;
}
}
这个是画一个纵向的PDF 如果是横向的也很简单:
//页面左边距、右边距、上边距和下边距
Document document = new Document(new RectangleReadOnly(842F, 595F), 50, 50, 50, 50);
创建document的源码默认是595F,842F来创建纸张的 我们把它反过来自然就成横向的了;
附加你可能需要的:
在画PDF的时候遇到过很多问题,比如寻找一个文本的位置,在指定位置加文本,在指定位置加图片等等..... 一下是代码,需要的话改改就能用了
/**
* 在指定位置添加文本
* @param idCard 添加的文本
* @param filePath 添加的文件路径
*/
public static String addIdCard(String idCard, String filePath) throws DocumentException, IOException {
Long timeString = System.currentTimeMillis();
ResourceBundle resourceBundle = ResourceBundle.getBundle("file");
String FILEPATH_REMOVE = resourceBundle.getString("personal_can_remove");
String fileOutString = FILEPATH_REMOVE + timeString.toString() + (new Random().nextInt(900) + 100) + ".pdf";//生成输出PDF的路径 TODO
PdfStamper stamper = null;
try {
PdfReader reader = new PdfReader(filePath);
stamper = new PdfStamper(reader, new FileOutputStream(fileOutString));
PdfContentByte content = stamper.getUnderContent(1);
content.beginText();
String FONT = resourceBundle.getString("pdf_font");
BaseFont baseFont = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//获取字体 TODO
content.setFontAndSize(baseFont, 10);
content.setTextMatrix(400, 700);
int j = idCard.length();
char c = ' ';
for (int k = 0; k < j; k++) {
content.setTextRise(10);
c = idCard.charAt(k);
content.showText(c + "");
}
content.endText();
} catch (Exception e) {
e.printStackTrace();
} finally {
stamper.close();
}
return fileOutString;
}
/**
* 在指定的页,位置添加图片
* @param filePath & fileName PDF文件位置
* @return PDF输出位置
*/
public static String addZhang(String filePath, String fileName, String code) throws MalformedURLException, IOException, DocumentException {
ResourceBundle resourceBundle = ResourceBundle.getBundle("file");
String FILEPATH_REMOVE = null;
FILEPATH_REMOVE = resourceBundle.getString("capital_path");
String fileOutPath = FILEPATH_REMOVE + fileName + code + "-" + (new Random().nextInt(900) + 100) + ".pdf";//生成文件名称 TODO
try {
PdfReader reader = new PdfReader(filePath, "World".getBytes());// 选择需要印章的pdf
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(fileOutPath));// 加完印章后的pdf
float[] f = getKeyWords(filePath);
PdfContentByte over = stamp.getOverContent((int) f[2]);// 设置在第几页打印印章
String PDF_IMAGE = resourceBundle.getString("pdf_zhang");
Image img1 = Image.getInstance(PDF_IMAGE);// 选择图片 TODO
img1.setAlignment(2);
img1.scaleAbsolute(100, 100);// 控制图片大小
//获取印章位置 TODO
img1.setAbsolutePosition(f[0] - 100, f[1]);// 控制图片位置
over.addImage(img1);
stamp.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return fileOutPath;
}
// 定义关键字
private static String KEY_WORD = "QIANZHANG";
// 定义返回值
private static float[] resu = null;
// 定义返回页码
private static int i = 0;
/*
* 返回关键字所在的坐标和页数
* float[0] >> X
* float[1] >> Y
* float[2] >> page
*/
public static float[] getKeyWords(String filePath) {
try {
PdfReader pdfReader = new PdfReader(filePath);
int pageNum = pdfReader.getNumberOfPages();
PdfReaderContentParser pdfReaderContentParser = new PdfReaderContentParser(pdfReader);
// 下标从1开始
for (i = 1; i <= pageNum; i++) {
pdfReaderContentParser.processContent(i, new RenderListener() {
@Override
public void renderText(TextRenderInfo textRenderInfo) {
String text = textRenderInfo.getText();
if (null != text && text.contains(KEY_WORD)) {
com.itextpdf.awt.geom.Rectangle2D.Float boundingRectange = textRenderInfo.getBaseline().getBoundingRectange();
resu = new float[3];
resu[0] = boundingRectange.x;
resu[1] = boundingRectange.y;
resu[2] = i;
}
}
@Override
public void renderImage(ImageRenderInfo arg0) {
// TODO Auto-generated method stub
}
@Override
public void endTextBlock() {
// TODO Auto-generated method stub
}
@Override
public void beginTextBlock() {
// TODO Auto-generated method stub
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
return resu;
}