1. import com.itextpdf.text.Font;
    2. import com.itextpdf.text.Image;
    3. import com.itextpdf.text.Rectangle;
    4. import com.itextpdf.text.*;
    5. import com.itextpdf.text.pdf.BaseFont;
    6. import com.itextpdf.text.pdf.PdfPCell;
    7. import com.itextpdf.text.pdf.PdfPTable;
    8. import com.itextpdf.text.pdf.PdfWriter;
    9. import org.apache.commons.lang3.StringUtils;
    10. import org.jfree.chart.ChartFactory;
    11. import org.jfree.chart.ChartUtils;
    12. import org.jfree.chart.JFreeChart;
    13. import org.jfree.chart.axis.CategoryAxis;
    14. import org.jfree.chart.axis.ValueAxis;
    15. import org.jfree.chart.labels.ItemLabelAnchor;
    16. import org.jfree.chart.labels.ItemLabelPosition;
    17. import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
    18. import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
    19. import org.jfree.chart.plot.CategoryPlot;
    20. import org.jfree.chart.plot.PiePlot3D;
    21. import org.jfree.chart.plot.PlotOrientation;
    22. import org.jfree.chart.renderer.category.BarRenderer;
    23. import org.jfree.chart.renderer.category.LineAndShapeRenderer;
    24. import org.jfree.chart.title.TextTitle;
    25. import org.jfree.chart.ui.TextAnchor;
    26. import org.jfree.data.category.CategoryDataset;
    27. import org.jfree.data.general.PieDataset;
    28. import javax.servlet.http.HttpServletResponse;
    29. import java.awt.*;
    30. import java.io.BufferedOutputStream;
    31. import java.io.FileOutputStream;
    32. import java.io.IOException;
    33. import java.io.OutputStream;
    34. import java.lang.reflect.Field;
    35. import java.net.URLEncoder;
    36. import java.text.DecimalFormat;
    37. import java.text.NumberFormat;
    38. import java.text.SimpleDateFormat;
    39. import java.util.List;
    40. import java.util.*;
    41. /**
    42. * @author X-MD
    43. * @version 1.0.0
    44. * @Description PDF导出工具类
    45. * @createTime 2022年01月19日 16:47:00
    46. */
    47. public class PdfExportUtil {
    48. private static String templatePath = PdfExportUtil.class.getResource("/").getPath() + "/templates/";
    49. private static final String FONTNAME = "微软雅黑";
    50. private static final String FONTCONT = "宋体";
    51. /**
    52. * 需要导出的字段名称
    53. */
    54. private static List<String> fieldNameList;
    55. /**
    56. * 需要导出的字段
    57. */
    58. private static List<String> fieldList;
    59. /**
    60. * 需要增加的列
    61. */
    62. private static int relativeWidths;
    63. /**
    64. * 字段所占列
    65. */
    66. private static Map<String, Integer> colspanMap;
    67. /**
    68. * 字段所占行
    69. */
    70. private static Map<String, Integer> rowspanMap;
    71. private static List<Element> elementList = new ArrayList<>();
    72. private static List<Element> newPageList = new ArrayList<>();
    73. /**
    74. * 纯文字
    75. * @param content 文字内容
    76. * @return
    77. */
    78. public static void createParagraph(String content) {
    79. createParagraph(content, 12, Font.NORMAL, BaseColor.BLACK, Element.ALIGN_LEFT);
    80. }
    81. /**
    82. * 纯文字
    83. * @param content 文字内容
    84. * @param size 文字大小
    85. * @param style 格式
    86. * @param color 颜色
    87. * @param alignment 位置
    88. * @return
    89. */
    90. public static void createParagraph(String content, float size, int style, BaseColor color, int alignment) {
    91. try {
    92. //设置中文样式(不设置,中文将不会显示)
    93. BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
    94. Font fontChineseContent = new Font(bfChinese, size,style, color);
    95. Paragraph pieParagraph = new Paragraph(content, fontChineseContent);
    96. pieParagraph.setAlignment(alignment);
    97. elementList.add(pieParagraph);
    98. } catch (Exception e) {
    99. e.printStackTrace();
    100. }
    101. }
    102. /**
    103. * 添加图片
    104. * @param filename 文件路径
    105. */
    106. public static void createImage(String filename) {
    107. createImage(filename,Element.ALIGN_CENTER,400F, 200F);
    108. }
    109. /**
    110. * 添加图片
    111. * @param filename 文件路径
    112. * @param alignment 位置 (Element 接口)
    113. * ALIGN_CENTER = 1
    114. * ALIGN_LEFT = 0
    115. * ALIGN_RIGHT = 2
    116. * @param newWidth 宽度
    117. * @param newHeight 高度
    118. */
    119. public static void createImage(String filename,int alignment,float newWidth, float newHeight) {
    120. try {
    121. Image image = Image.getInstance(filename);
    122. image.setAlignment(alignment);
    123. image.scaleAbsolute(newWidth, newHeight);
    124. //依照比例缩放
    125. //image.scalePercent(40);
    126. elementList.add(image);
    127. } catch (Exception e) {
    128. e.printStackTrace();
    129. }
    130. }
    131. /**
    132. * 导出表格
    133. * @param dataSet 数据
    134. * @param pojoClass 数据对应的类
    135. * @return
    136. */
    137. public static void createPdfPTable(List<?> dataSet, Class<?> pojoClass) {
    138. createPdfPTable(dataSet,pojoClass, 15, Font.BOLD, BaseColor.BLUE, Font.DEFAULTSIZE, Font.NORMAL, BaseColor.BLACK);
    139. }
    140. /**
    141. * 导出表格
    142. * @param dataSet
    143. * @param pojoClass
    144. * @param titleSize 标题字体大小
    145. * @param titleStyle 标题字体格式
    146. * @param titleColor 标题字体颜色
    147. * @param contentSize 内容字体大小
    148. * @param contentStyle 内容字体格式
    149. * @param contentColor 内容字体颜色
    150. * @return
    151. */
    152. public static void createPdfPTable(List<?> dataSet, Class<?> pojoClass, int titleSize, int titleStyle, BaseColor titleColor, int contentSize, int contentStyle, BaseColor contentColor) {
    153. try {
    154. fieldNameList = new ArrayList<>();
    155. fieldList = new ArrayList<>();
    156. relativeWidths = 0;
    157. colspanMap = new HashMap<>();
    158. rowspanMap = new HashMap<>();
    159. filedTitle(pojoClass);
    160. PdfPCell cell;
    161. //标题字体设置
    162. Font bigFont = createFont(titleSize, titleStyle, titleColor);
    163. PdfPTable pdfPTable = createPdfPTable(fieldList.size() + relativeWidths);
    164. pdfPTable.setWidthPercentage(100.0F);
    165. //先设置需要导出的字段名称
    166. for (String header : fieldNameList) {
    167. cell = new PdfPCell(new Phrase(header, bigFont));
    168. cell.setFixedHeight(60);
    169. cell.setUseAscender(true);
    170. if (colspanMap.containsKey(header)) {
    171. Integer integer = colspanMap.get(header);
    172. cell.setColspan(integer);
    173. }
    174. cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    175. //水平居中
    176. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    177. pdfPTable.addCell(cell);
    178. }
    179. //设置具体值
    180. //具体值得字体设置
    181. bigFont = createFont(contentSize, contentStyle, contentColor);
    182. for (Object o : dataSet) {
    183. for (String pama : fieldList) {
    184. String afterData = getFieldValueByFieldName(pama, o);
    185. //产品编号
    186. cell = new PdfPCell(new Phrase(afterData, bigFont));
    187. if (colspanMap.containsKey(pama)) {
    188. Integer integer = colspanMap.get(pama);
    189. cell.setColspan(integer);
    190. }
    191. if (rowspanMap.containsKey(pama)) {
    192. Integer integer = rowspanMap.get(pama);
    193. cell.setRowspan(integer);
    194. cell.setRowspan(integer);
    195. }
    196. cellSet(cell);
    197. pdfPTable.addCell(cell);
    198. }
    199. }
    200. elementList.add(pdfPTable);
    201. } catch (Exception e) {
    202. e.printStackTrace();
    203. }
    204. }
    205. /**
    206. * 创建饼状图
    207. * @param dataset 数据
    208. * @param title 标题
    209. * @return String 图片地址
    210. */
    211. public static String createPieChart3D(PieDataset dataset,String title) {
    212. return createPieChart3D(dataset, title, 12, 12, 20);
    213. }
    214. /**
    215. * 创建饼状图
    216. * @param dataset 数据
    217. * @param title 标题
    218. * @param labelSize Label字体大小
    219. * @param legendSize Legend字体大小
    220. * @param textTitleSize 图标字体大小
    221. * @return String 图片地址
    222. * @return
    223. */
    224. public static String createPieChart3D(PieDataset dataset,String title,int labelSize,int legendSize,int textTitleSize) {
    225. String strUrl = templatePath + "pieChart3D.jpg";
    226. try (
    227. FileOutputStream fosJpg = new FileOutputStream(strUrl)
    228. ) {
    229. JFreeChart chart = ChartFactory.createPieChart3D(title, dataset, true, true, false);
    230. PiePlot3D plot = (PiePlot3D) chart.getPlot();
    231. //设置Label字体
    232. plot.setLabelFont(new java.awt.Font(FONTNAME, java.awt.Font.PLAIN, labelSize));
    233. //设置legend字体
    234. chart.getLegend().setItemFont(new java.awt.Font(FONTNAME, java.awt.Font.PLAIN, legendSize));
    235. // 图片中显示百分比:默认方式
    236. //plot.setLabelGenerator(new StandardPieSectionLabelGenerator(StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT));
    237. // 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位
    238. plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));
    239. // 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例
    240. plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})"));
    241. // 设置背景色为白色
    242. chart.setBackgroundPaint(Color.white);
    243. // 指定图片的透明度(0.0-1.0)
    244. plot.setForegroundAlpha(1.0f);
    245. // 指定显示的饼图上圆形(false)还椭圆形(true)
    246. plot.setCircular(true);
    247. // 设置图标题的字体
    248. java.awt.Font font = new java.awt.Font(FONTCONT, java.awt.Font.CENTER_BASELINE, textTitleSize);
    249. TextTitle textTitle = new TextTitle(title);
    250. textTitle.setFont(font);
    251. chart.setTitle(textTitle);
    252. ChartUtils.writeChartAsJPEG(fosJpg, 0.7f, chart, 800, 1000, null);
    253. // createImage(strUrl,alignment,newWidth,newHeight);
    254. return strUrl;
    255. } catch (Exception e) {
    256. e.printStackTrace();
    257. return null;
    258. }
    259. }
    260. /**
    261. * 折线图
    262. * @param lineDataset 数据
    263. * @param title 标题
    264. * @param categoryAxisLabel
    265. * @param valueAxisLabel
    266. * @return Document
    267. */
    268. public static String createLineChart(CategoryDataset lineDataset,String title,String categoryAxisLabel,String valueAxisLabel) {
    269. return createLineChart(lineDataset, title, categoryAxisLabel, valueAxisLabel, 12, 15, false);
    270. }
    271. /**
    272. * 折线图
    273. * @param lineDataset 数据
    274. * @param title 标题
    275. * @param categoryAxisLabel
    276. * @param valueAxisLabel
    277. * @param legendSize
    278. * @param titleSize
    279. * @param bool 拐点是否显式小图标
    280. * @return Document
    281. */
    282. public static String createLineChart(CategoryDataset lineDataset, String title, String categoryAxisLabel,
    283. String valueAxisLabel, int legendSize, int titleSize,boolean bool) {
    284. String strUrl = templatePath + "lineChart.jpg";
    285. try (
    286. FileOutputStream fosJpg = new FileOutputStream(strUrl)
    287. ) {
    288. JFreeChart lineChart = ChartFactory.createLineChart(title, categoryAxisLabel, valueAxisLabel, lineDataset, PlotOrientation.VERTICAL, true, true, true);
    289. lineChart.getLegend().setItemFont(new java.awt.Font(FONTCONT, java.awt.Font.PLAIN, legendSize));
    290. lineChart.getTitle().setFont(new java.awt.Font(FONTCONT, java.awt.Font.PLAIN, titleSize));
    291. //获取绘图区对象
    292. CategoryPlot linePlot = lineChart.getCategoryPlot();
    293. //设置绘图区域背景的 alpha 透明度 ,在 0.0f 到 1.0f 的范围内
    294. linePlot.setBackgroundAlpha(0.0f);
    295. //区域背景色
    296. //linePlot.setBackgroundPaint(Color.white);
    297. //背景底部横虚线
    298. linePlot.setRangeGridlinePaint(Color.gray);
    299. //linePlot.setOutlinePaint(Color.RED);//边界线
    300. // 设置水平方向背景线颜色
    301. // 设置是否显示水平方向背景线,默认值为true
    302. linePlot.setRangeGridlinesVisible(true);
    303. // 设置垂直方向背景线颜色
    304. linePlot.setDomainGridlinePaint(Color.gray);
    305. // 设置是否显示垂直方向背景线,默认值为false
    306. linePlot.setDomainGridlinesVisible(true);
    307. //获取坐标轴对象
    308. CategoryAxis lineAxis = linePlot.getDomainAxis();
    309. //设置坐标轴字体
    310. lineAxis.setLabelFont(new java.awt.Font(FONTCONT, java.awt.Font.PLAIN, 12));
    311. //设置坐标轴标尺值字体(x轴)
    312. lineAxis.setTickLabelFont(new java.awt.Font(FONTCONT, java.awt.Font.PLAIN, 12));
    313. //获取数据轴对象(y轴)
    314. ValueAxis rangeAxis = linePlot.getRangeAxis();
    315. rangeAxis.setLabelFont(new java.awt.Font(FONTCONT, java.awt.Font.PLAIN, 12));
    316. //将折线设置为虚线
    317. LineAndShapeRenderer renderer = (LineAndShapeRenderer) linePlot.getRenderer();
    318. // 设置显示小图标
    319. renderer.setDefaultShapesVisible(bool);
    320. //利用虚线绘制
    321. // float[] dashes = {5.0f};
    322. // BasicStroke brokenLine = new BasicStroke(2.2f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 8f, dashes, 0.6f);
    323. // renderer.setSeriesStroke(0, brokenLine);
    324. //折点显式数值
    325. // DecimalFormat decimalformat1 = new DecimalFormat("##"); // 数据点显示数据值的格式
    326. // renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", decimalformat1));
    327. // renderer.setDefaultItemLabelsVisible(true); // 设置项标签显示
    328. // renderer.setDefaultItemLabelsVisible(true); // 基本项标签显示
    329. ChartUtils.writeChartAsJPEG(fosJpg, 0.7f, lineChart, 600, 300);
    330. return strUrl;
    331. } catch (Exception e) {
    332. e.printStackTrace();
    333. return null;
    334. }
    335. }
    336. /**
    337. * 柱状图
    338. * @param title
    339. * @param categoryAxisLabel
    340. * @param valueAxisLabel
    341. * @param dataSet 数据
    342. * @return
    343. */
    344. public static String createBarChart(String title, String categoryAxisLabel, String valueAxisLabel, CategoryDataset dataSet) {
    345. String strUrl = templatePath + "barChart.jpg";
    346. try (
    347. FileOutputStream fos = new FileOutputStream(strUrl)
    348. ) {
    349. JFreeChart jfreechart = ChartFactory.createBarChart(title, categoryAxisLabel, valueAxisLabel, dataSet);
    350. CategoryPlot categoryPlot = jfreechart.getCategoryPlot();
    351. categoryPlot.setBackgroundAlpha(0.0f);
    352. categoryPlot.setRangeGridlinePaint(Color.gray);
    353. categoryPlot.setRangeGridlinesVisible(true);
    354. // 设置网格beijingse
    355. categoryPlot.setBackgroundPaint(Color.WHITE);
    356. // 设置网格竖线颜色
    357. categoryPlot.setDomainGridlinePaint(Color.pink);
    358. // 设置网格横线颜色
    359. categoryPlot.setRangeGridlinePaint(Color.pink);
    360. // 显示每个柱的数值,并修改该数值的字体属性
    361. BarRenderer renderer2 = new BarRenderer();
    362. renderer2.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    363. renderer2.setDefaultItemLabelsVisible(true);
    364. renderer2.setDefaultPositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.INSIDE8, TextAnchor.BASELINE_CENTER));
    365. renderer2.setItemLabelAnchorOffset(10D);
    366. // 设置平行柱的之间距离
    367. renderer2.setItemMargin(0.2);
    368. categoryPlot.setRenderer(renderer2);
    369. ChartUtils.writeChartAsJPEG(fos, 0.7f, jfreechart, 600, 300);
    370. return strUrl;
    371. } catch (Exception e) {
    372. e.printStackTrace();
    373. return null;
    374. }
    375. }
    376. /**
    377. * 分页
    378. */
    379. public static void newPage(){
    380. Element element = elementList.get(elementList.size() - 1);
    381. newPageList.add(element);
    382. }
    383. /**
    384. * PDF预览
    385. * @param response
    386. * @param filename 文件名称
    387. * @param rect PDF每张页面大小
    388. * @param backgroundColor 背景颜色
    389. * @param marginLeft 左页边距
    390. * @param marginRight 右页边距
    391. * @param marginTop 顶部页边距
    392. * @param marginBottom 底部页面距
    393. * @throws Exception
    394. */
    395. public static void pdfExportPreview(HttpServletResponse response, String filename,Rectangle rect,BaseColor backgroundColor,
    396. float marginLeft, float marginRight, float marginTop, float marginBottom) throws Exception {
    397. response.setContentType("application/pdf");
    398. pdfExport(response, filename, rect, backgroundColor, marginLeft, marginRight, marginTop, marginBottom);
    399. }
    400. /**
    401. * 预览PDF
    402. * @param response
    403. * @param filename 文件名
    404. * @throws Exception
    405. */
    406. public static void pdfExportPreview(HttpServletResponse response, String filename) throws Exception {
    407. response.setContentType("application/pdf");
    408. pdfExport(response, filename, PageSize.A4,BaseColor.WHITE,20, 20, 20, 20);
    409. }
    410. /**
    411. * PDF导出
    412. * @param response
    413. * @param filename 文件名称
    414. * @param rect PDF每张页面大小
    415. * @param backgroundColor 背景颜色
    416. * @param marginLeft 左页边距
    417. * @param marginRight 右页边距
    418. * @param marginTop 顶部页边距
    419. * @param marginBottom 底部页面距
    420. * @throws Exception
    421. */
    422. public static void pdfExportDownload(HttpServletResponse response, String filename,Rectangle rect,BaseColor backgroundColor,
    423. float marginLeft, float marginRight, float marginTop, float marginBottom) throws Exception {
    424. response.setContentType("application/x-download");
    425. pdfExport(response, filename, rect, backgroundColor, marginLeft, marginRight, marginTop, marginBottom);
    426. }
    427. /**
    428. * 下载PDF
    429. * @param response
    430. * @param filename 文件名
    431. * @throws Exception
    432. */
    433. public static void pdfExportDownload(HttpServletResponse response, String filename) throws Exception {
    434. response.setContentType("application/x-download");
    435. pdfExport(response, filename, PageSize.A4,BaseColor.WHITE,20, 20, 20, 20);
    436. }
    437. private static void pdfExport(HttpServletResponse response,String filename,Rectangle rect,BaseColor backgroundColor,
    438. float marginLeft, float marginRight, float marginTop, float marginBottom) throws IOException {
    439. Document document = null;
    440. OutputStream os = null;
    441. try {
    442. response.addHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(filename, "UTF-8") + ".pdf");
    443. document =createDocument(rect,backgroundColor,marginLeft, marginRight, marginTop, marginBottom);
    444. os = new BufferedOutputStream(response.getOutputStream());
    445. // 2. 获取writer
    446. PdfWriter.getInstance(document, os);
    447. // 3. open()
    448. document.open();
    449. for (Element element : elementList) {
    450. document.add(element);
    451. if (newPageList.contains(element)) {
    452. document.newPage();
    453. }
    454. }
    455. } catch (Exception e) {
    456. e.printStackTrace();
    457. } finally {
    458. elementList.clear();
    459. newPageList.clear();
    460. if (document != null){
    461. document.close();
    462. }
    463. if (os != null){
    464. os.close();
    465. }
    466. }
    467. }
    468. /**
    469. * 创建 Document
    470. * @param marginLeft
    471. * @param marginRight
    472. * @param marginTop
    473. * @param marginBottom
    474. * @return
    475. */
    476. public static Document createDocument(Rectangle rect, BaseColor backgroundColor, float marginLeft, float marginRight, float marginTop, float marginBottom) {
    477. //生成pdf
    478. Document document = new Document();
    479. // 页面大小
    480. Rectangle rectangle = new Rectangle(rect);
    481. // 页面背景颜色
    482. rectangle.setBackgroundColor(backgroundColor);
    483. document.setPageSize(rectangle);
    484. // 页边距 左,右,上,下
    485. document.setMargins(marginLeft, marginRight, marginTop, marginBottom);
    486. return document;
    487. }
    488. /**
    489. * @param len 表格列数
    490. * @return
    491. */
    492. public static PdfPTable createPdfPTable(int len) {
    493. PdfPTable pdfPTable = new PdfPTable(len);
    494. pdfPTable.setSpacingBefore(5);
    495. pdfPTable.setWidthPercentage(100.0F);
    496. pdfPTable.setHorizontalAlignment(Element.ALIGN_CENTER);
    497. return pdfPTable;
    498. }
    499. /**
    500. * 创建字体
    501. * @param size
    502. * @param style
    503. * @param fontColor
    504. * @return
    505. */
    506. public static Font createFont(int size, int style, BaseColor fontColor) {
    507. //中文字体 ----不然中文会乱码
    508. BaseFont bf = null;
    509. try {
    510. bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
    511. return new Font(bf, size, style, fontColor);
    512. } catch (Exception e) {
    513. e.printStackTrace();
    514. }
    515. return new Font(bf, Font.DEFAULTSIZE, Font.NORMAL, BaseColor.BLACK);
    516. }
    517. /**
    518. * 设置PdfPCell
    519. * @param cell
    520. */
    521. private static void cellSet(PdfPCell cell) {
    522. cell.setFixedHeight(40);
    523. cell.setUseAscender(true);
    524. cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    525. //水平居中
    526. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    527. }
    528. /**
    529. * 根据属性名获取属性值
    530. *
    531. * @param fieldName
    532. * @param object
    533. * @return
    534. */
    535. private static String getFieldValueByFieldName(String fieldName, Object object) {
    536. try {
    537. Field field = object.getClass().getDeclaredField(fieldName);
    538. //设置对象的访问权限,保证对private的属性的访问
    539. field.setAccessible(true);
    540. Object ostr = field.get(object);
    541. String value = (String) ostr;
    542. PdfField excel = field.getAnnotation(PdfField.class);
    543. String[] replace = excel.replace();
    544. if (replace != null && replace.length > 0) {
    545. value = (String) replaceValue(replace, (String) ostr);
    546. } else if (StringUtils.isNotBlank(excel.format())) {
    547. SimpleDateFormat formatDate = new SimpleDateFormat(excel.format());
    548. Date parse = formatDate.parse(String.valueOf(ostr));
    549. value = formatDate.format(parse);
    550. }
    551. return value;
    552. } catch (Exception e) {
    553. return null;
    554. }
    555. }
    556. private static Object replaceValue(String[] replace, String value) {
    557. String[] temp;
    558. for (String str : replace) {
    559. temp = str.split("_");
    560. if (value.equals(temp[1])) {
    561. value = temp[0];
    562. break;
    563. }
    564. }
    565. return value;
    566. }
    567. /**
    568. * 获取所有字段名称
    569. *
    570. * @param pojoClass
    571. * @return
    572. */
    573. private static void filedTitle(Class<?> pojoClass) {
    574. Field[] fileds = getClassFields(pojoClass);
    575. for (Field filed : fileds) {
    576. if (filed.getAnnotation(PdfField.class) != null) {
    577. PdfField excel = filed.getAnnotation(PdfField.class);
    578. fieldNameList.add(excel.name());
    579. fieldList.add(filed.getName());
    580. if (excel.colspan() > 0) {
    581. relativeWidths += excel.colspan();
    582. relativeWidths--;
    583. colspanMap.put(excel.name(), excel.colspan());
    584. colspanMap.put(filed.getName(), excel.colspan());
    585. }
    586. if (excel.rowspan() > 0) {
    587. colspanMap.put(filed.getName(), excel.rowspan());
    588. }
    589. }
    590. }
    591. }
    592. /**
    593. * 根据class获取类字段
    594. *
    595. * @param clazz
    596. * @return
    597. */
    598. public static Field[] getClassFields(Class<?> clazz) {
    599. List<Field> list = new ArrayList<>();
    600. Field[] fields;
    601. do {
    602. fields = clazz.getDeclaredFields();
    603. list.addAll(Arrays.asList(fields));
    604. clazz = clazz.getSuperclass();
    605. } while (clazz != Object.class && clazz != null);
    606. return list.toArray(fields);
    607. }
    608. }