itext生成pdf
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.7</version>
</dependency>
package com.example.spring01;
import com.alibaba.fastjson.JSONObject;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.*;
public class Test2 {
private static final int LENGTH = 20;
private static Map<Integer,String> questionTypeMap = new HashMap<Integer,String>();
static {
questionTypeMap.put(0,"技术");
questionTypeMap.put(1,"商务");
}
public static String getQuestionType(){
Random random = new Random();
return questionTypeMap.get(random.nextInt(2));
}
public static void main(String[] args) {
String[] headers = {"编码", "品类名称", "类别", "问题描述", "答复意见"};
List<Map<String,Object>> mapList = new ArrayList<>();
Map<String,Object> param;
String str = "[{\"noticeClarifyCode\":\"CHD放大了看是否节点数量看见了江东父老杀了\n\n\n\n\n\n\n\n\ndfjaljfdlaj减肥的拉萨解放了圣诞节Y20220022\",\"materialCategoryName\":\"110kV 交流电力变压器\",\"questionType\":\"商务\",\"questionDespription\":\"测试营业执照问题\",\"answerOpinion\":\"1111111111111111\\n房的垃圾分类的卡六块腹肌卡戴珊老福克斯\\n非登记克拉斯就发了数据了看的附件拉是地脚螺栓,\\n发送点击案例集冯老师的附件撒了减肥了\\n非打死克劳福德拉克丝解放路山东矿机分开了上登记来看登记非拉上激发了\\n22222222222222\"},{\"noticeClarifyCode\":\"CHDY20220188\",\"materialCategoryName\":\"110kV 交流电力变压器\",\"questionType\":\"商务\",\"questionDespription\":\"测试营业执照问题\",\"answerOpinion\":\"1111111111111111\\n房的垃圾分类的卡六块腹肌卡戴珊老福克斯\\n非登记克拉斯就发了数据了看的附件拉是地脚螺栓,\\n发送点击案例集冯老师的附件撒了减肥了\\n非打死克劳福德拉克丝解放路发动机阿斯利康房价历史记录份都是垃圾分类介绍了肯定就是离开家飞机迪斯科垃圾分类电视机分厘卡数据代理·解放东路沙发了的时间里就山东矿机分开了上登记来看登记非拉上激发了\\n22222222222222\"},{\"noticeClarifyCode\":\"CHDY20220022\",\"materialCategoryName\":\"110kV 交流电力变压器\",\"questionType\":\"商务\",\"questionDespription\":\"测试营业执照问题\",\"answerOpinion\":\"1111111111111111\\n房的垃圾分类的卡六块腹肌卡戴珊老福克斯\\n非登记克拉斯就发了数据了看的附件拉是地脚螺栓,\\n发送点击案例集冯老师的附件撒了减肥了\\n非打死克劳福德拉克丝解放路山东矿机分开了上登记来看登记非拉上激发了\\n22222222222222\"},{\"noticeClarifyCode\":\"CHDY20220119\",\"materialCategoryName\":\"工业级以太网交换机\",\"questionType\":\"技术\",\"questionDespription\":\"11111111111\",\"answerOpinion\":\"122222222222222222二我v人·1222222222222\"},{\"noticeClarifyCode\":\"CHDY20220115\",\"materialCategoryName\":\"工业级以太网交换机\",\"questionType\":\"商务\",\"questionDespription\":\"111\",\"answerOpinion\":\"11111111111111111111111111111111111111111\"},{\"noticeClarifyCode\":\"CHDY20220116\",\"materialCategoryName\":\"工业级以太网交换机\",\"questionType\":\"商务\",\"questionDespription\":\"啛啛喳喳\\n\\nfqwfgq/\\n/\\n/\\n/\\n/\\n/\\n\",\"answerOpinion\":\"给发四个的时间考虑就考虑对方金克拉金克拉尽快立法大数据飞机喀什的老玩家·发大水了开发接口连接·1即使对方克拉金克拉\"},{\"noticeClarifyCode\":\"CHDY20220126\",\"materialCategoryName\":\"工业级以太网交换机\",\"questionType\":\"商务\",\"questionDespription\":\"kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkks\",\"answerOpinion\":\"打发士大夫克拉金克拉·肌肤的卡拉JFK劳动节快乐·见风使舵卡拉JFK了数据来看·\\n的发散开来\"},{\"noticeClarifyCode\":\"CHDY20220125\",\"materialCategoryName\":\"工业级以太网交换机\",\"questionType\":\"商务\",\"questionDespription\":\"222222\",\"answerOpinion\":\"1111111111111111\"},{\"noticeClarifyCode\":\"CHDY20220127\",\"materialCategoryName\":\"工业级以太网交换机\",\"questionType\":\"商务\",\"questionDespription\":\"13247544444\",\"answerOpinion\":\"1212121212121212121212121212121212121212122222222222222222\"},{\"noticeClarifyCode\":\"CHDY20220242\",\"materialCategoryName\":\"10kV 交流电力电缆(防白蚁型)\",\"questionType\":\"商务\",\"questionDespription\":\"22222222222发什么麻烦大了目录开发商贷款的反馈\",\"answerOpinion\":\"如涛涛涛涛涛涛涛涛涛涛涛涛涛涛涛涛涛涛\"},{\"noticeClarifyCode\":\"CHDY20220243\",\"materialCategoryName\":\"10kV 固定式高压开关柜\",\"questionType\":\"商务\",\"questionDespription\":\" 啊手动阀v五百的风格顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶是\",\"answerOpinion\":\"腹股沟灌灌灌灌灌灌灌灌灌灌灌灌灌灌灌灌\"},{\"noticeClarifyCode\":\"CHDY20220244\",\"materialCategoryName\":\"110kV 电容式电压互感器\",\"questionType\":\"商务\",\"questionDespription\":\"按时地方烦烦烦烦烦烦烦烦烦烦烦烦烦烦烦\",\"answerOpinion\":\"法国发过法国发过法国发过法国风格非官方分顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶\"},{\"noticeClarifyCode\":\"CHDY20220245\",\"materialCategoryName\":\"10kV 交流电力电缆(防白蚁型)\",\"questionType\":\"商务\",\"questionDespription\":\"让他八八八八八八八八八八\",\"answerOpinion\":\"士大夫的反对大幅度反对法大幅度大幅度发反对撒啊啊啊啊啊啊啊啊啊啊啊啊啊\"},{\"noticeClarifyCode\":\"CHDY20220246\",\"materialCategoryName\":\"110kV 电容式电压互感器\",\"questionType\":\"商务\",\"questionDespription\":\"饿哇日日日日日日日日日日日日日日日日日\",\"answerOpinion\":\"方式大啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊·的呱呱呱呱呱呱呱呱呱呱呱呱呱呱呱呱呱呱\"},{\"noticeClarifyCode\":\"CHDY20220247\",\"materialCategoryName\":\"过程层交换机\",\"questionType\":\"商务\",\"questionDespription\":\"公司的反反复复烦烦烦烦烦烦烦烦烦\",\"answerOpinion\":\"啊手动阀手动阀手动阀手动阀手动阀手动阀\"},{\"noticeClarifyCode\":\"CHDY20220248\",\"materialCategoryName\":\"过程层交换机\",\"questionType\":\"商务\",\"questionDespription\":\"厄特特特特特特特特特特特特特特特特特2\",\"answerOpinion\":\"腹股沟灌灌灌灌灌灌灌灌灌灌灌灌灌灌灌灌\"},{\"noticeClarifyCode\":\"CHDY20220249\",\"materialCategoryName\":\"110kV 电容式电压互感器\",\"questionType\":\"商务\",\"questionDespription\":\"热嗡嗡嗡嗡嗡嗡嗡嗡嗡嗡嗡嗡嗡嗡嗡嗡嗡嗡\",\"answerOpinion\":\"法国发过法国发过法国发过法国风格非官方1111111111但如果水水水水水水水水水水水水水水水水\"},{\"noticeClarifyCode\":\"CHDY20220250\",\"materialCategoryName\":\"过程层交换机\",\"questionType\":\"商务\",\"questionDespription\":\"饿哇日日日日日日日日日日日日日日日日日\",\"answerOpinion\":\"额我热热热热热热热热热热热热热热热热\"}]";
List<JSONObject> jsonObjects = JSONObject.parseArray(str, JSONObject.class);
for (JSONObject jsonObject : jsonObjects) {
param = new LinkedHashMap<>();
param.put("key1",jsonObject.getString("noticeClarifyCode"));
param.put("key2",jsonObject.getString("materialCategoryName"));
param.put("key3",jsonObject.getString("questionType"));
param.put("key4",jsonObject.getString("questionDespription"));
param.put("key5",jsonObject.getString("answerOpinion"));
mapList.add(param);
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Document document = null;
PdfWriter writer = null;
try {
document = PdfUtils.createTransverseDocument();
writer = PdfWriter.getInstance(document, byteArrayOutputStream);
document.open();
Font bigFont = PdfUtils.createFont(14, Font.NORMAL, BaseColor.BLACK);
Font blueFont = PdfUtils.createFont(10, Font.NORMAL, BaseColor.BLACK);
Paragraph title = PdfUtils.createParagraph("澄清答疑明细", bigFont);
title.setAlignment(Element.ALIGN_CENTER);
document.add(title);
PdfPTable table = PdfUtils.createPdfPTable(headers.length);
table.setWidths(new int[]{10,15,5,35,35});
PdfUtils.createHeader(document, table, headers, bigFont);
PdfUtils.dataProcessing(document, table, mapList, blueFont);
} catch (DocumentException e) {
} finally {
if (document != null) {
document.close();
}
if (writer != null) {
writer.close();
}
}
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream("D:\\temp\\test.pdf");
fileOutputStream.write(byteArrayOutputStream.toByteArray());
System.out.println("生成成功!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fileOutputStream != null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (byteArrayOutputStream != null){
try {
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package com.example.spring01;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class PdfUtils {
public static float fixedHeight = 27f;
public static int rowSpan = 0;
public static int colSpan = 0;
public static float marginLeft = 20;
public static float marginRight = 20;
public static float marginTop = 20;
public static float marginBottom = 20;
public static Document createPortraitDocument() {
Document document = new Document();
Rectangle rectangle = new Rectangle(PageSize.A4);
rectangle.setBackgroundColor(BaseColor.WHITE);
document.setPageSize(rectangle);
document.setMargins(marginLeft, marginRight, marginTop, marginBottom);
return document;
}
public static Document createTransverseDocument() {
Document document = new Document();
Rectangle rectangle = new Rectangle(new RectangleReadOnly(842F, 595F));
rectangle.setBackgroundColor(BaseColor.WHITE);
document.setPageSize(rectangle);
document.setMargins(marginLeft, marginRight, marginTop, marginBottom);
return document;
}
public static Paragraph createParagraph(String text, Font font) {
Paragraph elements = new Paragraph(text, font);
elements.setSpacingBefore(5);
elements.setSpacingAfter(5);
return elements;
}
public static Font createFont(int fontNumber, int fontSize, BaseColor fontColor) {
BaseFont bf = null;
try {
bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
return new Font(bf, fontNumber, fontSize, fontColor);
} catch (Exception e) {
e.printStackTrace();
}
return new Font(bf, Font.DEFAULTSIZE, Font.NORMAL, BaseColor.BLACK);
}
public static void disableBorderSide(PdfPCell cell) {
if (cell != null) {
cell.disableBorderSide(1);
cell.disableBorderSide(2);
cell.disableBorderSide(4);
cell.disableBorderSide(8);
}
}
public static PdfPCell createCenterPdfPCell() {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setFixedHeight(fixedHeight);
return cell;
}
public static PdfPCell createCenterPdfPCell(String text, int rowSpan, int colSpan, Font font) {
PdfPCell cell = new PdfPCell(new Paragraph(text, font));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setFixedHeight(fixedHeight);
cell.setRowspan(rowSpan);
cell.setColspan(colSpan);
return cell;
}
public static PdfPTable createPdfPTable(int len) {
PdfPTable pdfPTable = new PdfPTable(len);
pdfPTable.setWidthPercentage(100);
return pdfPTable;
}
public static void createHeader(Document document, PdfPTable table, String[] headers, Font font) throws DocumentException {
for (int i = 0; i < headers.length; i++) {
PdfPCell cell = PdfUtils.createCenterPdfPCell(headers[i], rowSpan, rowSpan, font);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
}
document.add(table);
}
public static void dataProcessing(Document document, PdfPTable table, List<Map<String, Object>> list, Font font) throws DocumentException {
Iterator<Map<String, Object>> iterator = list.iterator();
while (iterator.hasNext()) {
Map<String, Object> map = iterator.next();
for (Map.Entry<String, Object> entry : map.entrySet()) {
Object value = entry.getValue();
PdfPCell cell = PdfUtils.createCenterPdfPCell(value.toString(), rowSpan, rowSpan, font);
table.addCell(cell);
}
document.add(table);
}
}
}