ITEXT 表格的指定列合并-升级版

x33g5p2x  于2021-12-28 转载在 其他  
字(4.0k)|赞(0)|评价(0)|浏览(377)

问题场景

今天在review代码的时候,发现对于ITEXT 表格的指定列合并还可以升级下,支持重复的字段出现合并。

比较

ITEXT 表格的指定列合并-升级版:方法简单,且支持 ‘A,A,B,C,A,D,D,D,E’,即字符串A可以重复不间断出现;
ITEXT 表格的指定列合并:方法略微复杂,且不支持 ‘A,A,B,C,A,D,D,D,E’,只支持 ‘A,A,B,C,D,D,D,E’,即字符串如果有重复的只能连续出现在一起。

CODE

需要导入的包:itext-pdfa-5.5.6.jar、itext-xtra-5.5.6.jar、itext-5.5.6.jar、itext-asian.jar

  1. package itext.a14;
  2. import com.itextpdf.text.*;
  3. import com.itextpdf.text.pdf.BaseFont;
  4. import com.itextpdf.text.pdf.PdfPCell;
  5. import com.itextpdf.text.pdf.PdfPTable;
  6. import com.itextpdf.text.pdf.PdfWriter;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. /**
  12. * Created on 2017/8/15
  13. * Author: youxingyang.
  14. */
  15. public class A14 {
  16. public static String split = "<brb>";
  17. public static void main(String[] args) {
  18. Document document = new Document(PageSize.A4, 48, 48, 60, 65);
  19. try {
  20. PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("A14.pdf"));
  21. document.open();
  22. // 设置字体
  23. BaseFont bfCN = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
  24. // 正文的字体
  25. Font headFont = new Font(bfCN, 12f, Font.BOLD, BaseColor.ORANGE);
  26. Font textFont = new Font(bfCN, 12f, Font.NORMAL, BaseColor.BLUE);
  27. //添加测试数据
  28. List<String[]> list = new ArrayList<>();
  29. for (int i = 0; i < 24; i++) {
  30. String[] arr = new String[4];
  31. String value;
  32. if (i < 4) {
  33. value = "A";
  34. } else if (i >= 4 && i < 6) {
  35. value = "B";
  36. } else if (i >= 6 && i < 8) {
  37. value = "C";
  38. } else if (i >= 8 && i < 13) {
  39. value = "D";
  40. } else if (i == 13 || i == 14) {
  41. value = "A";
  42. } else if (i > 14 && i < 16) {
  43. value = "E";
  44. } else if (i == 17) {
  45. value = "F";
  46. } else {
  47. value = "G";
  48. }
  49. arr[0] = "0" + i;
  50. arr[1] = value;
  51. arr[2] = "2" + i;
  52. arr[3] = "3" + i;
  53. list.add(arr);
  54. }
  55. //复制要合并的列
  56. List<String> ListIn = new ArrayList<>();
  57. for (String[] aList : list) {
  58. ListIn.add(aList[1]);
  59. }
  60. //根据算法改变合并列的值
  61. List<String> changeList = changeList(ListIn);
  62. //改变该列后把它复制到原来的list
  63. for (int i = 0; i < changeList.size(); i++) {
  64. String[] arr = list.get(i);
  65. arr[1] = changeList.get(i);
  66. list.set(i, arr);
  67. }
  68. document.newPage();
  69. //建立一个4列的表格
  70. PdfPTable table = new PdfPTable(4);
  71. String[] titleArr = {"第1列", "合并列", "第3列", "第4列"};
  72. //加表格头部
  73. addTitle1(table, titleArr, headFont);
  74. //加表格内容
  75. addContent1(table, list, textFont);
  76. document.add(table);
  77. document.close();
  78. } catch (DocumentException | IOException e){
  79. e.printStackTrace();
  80. }
  81. }
  82. private static void addContent1(PdfPTable table, List<String[]> list, Font textFont) {
  83. //表格数据内容
  84. for (String[] str : list) {
  85. for (int j = 0; j < str.length; j++) {
  86. String value = str[j];
  87. if (value != null) {
  88. Paragraph paragraph01;
  89. int spanNum = 1;
  90. if (j == 1 && value.contains(split)) {
  91. spanNum = Integer.parseInt(value.split(split)[1]);
  92. paragraph01 = new Paragraph(value.split(split)[0], textFont);
  93. } else {
  94. paragraph01 = new Paragraph(value, textFont);
  95. }
  96. paragraph01.setAlignment(1);
  97. PdfPCell cell = new PdfPCell();
  98. cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
  99. cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);//然并卵
  100. cell.setPaddingTop(-2f);//把字垂直居中
  101. cell.setPaddingBottom(8f);//把字垂直居中
  102. cell.addElement(paragraph01);
  103. cell.setRowspan(spanNum);
  104. table.addCell(cell);
  105. }
  106. }
  107. }
  108. }
  109. private static void addTitle1(PdfPTable table, String[] titleArr, Font headFont) {
  110. for (String aTitleArr : titleArr) {
  111. Paragraph p = new Paragraph(aTitleArr, headFont);
  112. PdfPCell cell = new PdfPCell();
  113. p.setAlignment(1);
  114. cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
  115. cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);//然并卵
  116. cell.setPaddingTop(-2f);//把字垂直居中
  117. cell.setPaddingBottom(8f);//把字垂直居中
  118. cell.addElement(p);
  119. table.addCell(cell);
  120. }
  121. }
  122. /**
  123. * 改变合并列的值
  124. * @param drugList 内容支持 'A,A,B,C,D,D,D,E' 支持 'A,A,B,C,A,D,D,D,E'
  125. * @return
  126. */
  127. private static List<String> changeList(List<String> drugList) {
  128. List<String> drugListCopy = new ArrayList<String>();
  129. drugListCopy.addAll(drugList);
  130. int nullNum = 0;
  131. for (int i = 0; i < drugList.size(); i++) {
  132. if (i > 0) {
  133. if (drugList.get(i).equals(drugList.get(i - 1))) {
  134. drugListCopy.set(i, null);
  135. nullNum++;
  136. } else {
  137. if (nullNum > 0) {
  138. int start = i - nullNum - 1;
  139. drugListCopy.set(start, drugList.get(start) + split + (nullNum + 1));
  140. nullNum = 0;
  141. }
  142. }
  143. // 处理某一列值都相同的情况
  144. if (nullNum > 0) {
  145. int start = i - nullNum;
  146. drugListCopy.set(start, drugList.get(start) + split + (nullNum + 1));
  147. }
  148. }
  149. }
  150. return drugListCopy;
  151. }
  152. }

效果

相关文章