ITEXT 实现背景色交替的三线表

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

问题场景

用itext写完table表之后,又要求实现三线表。。。

解决方式

通过查阅API知可以设置每个单元格的边线显示属性,可以通过变量控制显示的属性来达到效果。

效果

code

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

  1. package itext;
  2. import com.itextpdf.text.*;
  3. import com.itextpdf.text.pdf.*;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. /** * Created on 2017/5/16 * Author: youxingyang. */
  7. public class TableAndTitle {
  8. /** * @param args */
  9. public static void main(String[] args) throws Exception {
  10. String fileName = "tableAndTitle.pdf";
  11. TableAndTitle.test(fileName);
  12. }
  13. private static void test(String fileName) {
  14. Document document = new Document();
  15. try {
  16. PdfWriter.getInstance(document, new FileOutputStream(fileName));
  17. document.open();
  18. PdfPTable table = new PdfPTable(1);
  19. table.setKeepTogether(true);
  20. table.setSplitLate(false);
  21. PdfPTable table1 = new PdfPTable(1);
  22. PdfPCell cell0 = new PdfPCell();
  23. Paragraph p = new Paragraph("table title sample");
  24. p.setAlignment(1);
  25. p.setSpacingBefore(15f);
  26. cell0.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
  27. cell0.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);//然并卵
  28. cell0.setPaddingTop(-2f);//把字垂直居中
  29. cell0.setPaddingBottom(8f);//把字垂直居中
  30. cell0.addElement(p);
  31. cell0.setBorder(0);
  32. table1.addCell(cell0);
  33. PdfPTable table2 = new PdfPTable(2);
  34. float border = 1.5f;
  35. for (int a = 0; a < 20; a++) {
  36. PdfPCell cell = new PdfPCell();
  37. Paragraph pp;
  38. if (a == 0 || a == 1) {
  39. pp = str2ParaByTwoFont("tableTitle" + (a + 1), 9f, BaseColor.BLACK, Font.BOLD); //小五 加粗
  40. cell.setBorderWidthBottom(border);
  41. cell.setBorderWidthTop(border);
  42. } else {
  43. if (a == 18 || a == 19) {
  44. cell.setBorderWidthTop(0);
  45. cell.setBorderWidthBottom(border);
  46. } else {
  47. cell.setBorderWidthBottom(0);
  48. cell.setBorderWidthTop(0);
  49. }
  50. pp = str2ParaByTwoFont("tableContent" + (a - 1), 9f, BaseColor.BLACK); //小五
  51. }
  52. //设置间隔的背景色
  53. if ((a + 1) % 2 == 0) {
  54. if (((a + 1) / 2) % 2 == 1) {
  55. cell.setBackgroundColor(new BaseColor(128, 128, 255));
  56. } else {
  57. cell.setBackgroundColor(new BaseColor(128, 255, 255));
  58. }
  59. } else {
  60. if (((a + 1) / 2) % 2 == 1) {
  61. cell.setBackgroundColor(new BaseColor(128, 255, 255));
  62. } else {
  63. cell.setBackgroundColor(new BaseColor(128, 128, 255));
  64. }
  65. }
  66. pp.setAlignment(1);
  67. cell.setBorderWidthLeft(0);
  68. cell.setBorderWidthRight(0);
  69. cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
  70. cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);//然并卵
  71. cell.setPaddingTop(-2f);//把字垂直居中
  72. cell.setPaddingBottom(8f);//把字垂直居中
  73. cell.addElement(pp);
  74. table2.addCell(cell);
  75. }
  76. PdfPCell c1 = new PdfPCell();
  77. c1.setBorder(0);
  78. c1.addElement(table1);
  79. PdfPCell c2 = new PdfPCell();
  80. c2.setBorder(0);
  81. c2.addElement(table2);
  82. table.addCell(c1);
  83. table.addCell(c2);
  84. document.add(table);
  85. document.close();
  86. } catch (DocumentException | FileNotFoundException e) {
  87. e.printStackTrace();
  88. }
  89. }
  90. /** * 两种字体显示文字 * @param cont * @param size * @param color * @return */
  91. private static Paragraph str2ParaByTwoFont(String cont, float size, BaseColor color) {
  92. Paragraph res = new Paragraph();
  93. FontSelector selector = new FontSelector();
  94. //非汉字字体颜色
  95. Font f1 = FontFactory.getFont(FontFactory.TIMES_ROMAN, size);
  96. f1.setColor(color);
  97. //汉字字体颜色
  98. Font f2 = FontFactory.getFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED, size);
  99. f2.setColor(color);
  100. selector.addFont(f1);
  101. selector.addFont(f2);
  102. Phrase ph = selector.process(cont);
  103. res.add(ph);
  104. return res;
  105. }
  106. /** * 两种字体显示文字 * @param cont * @param size * @param color * @param bold * @return */
  107. private static Paragraph str2ParaByTwoFont(String cont, float size, BaseColor color, int bold) {
  108. Paragraph res = new Paragraph();
  109. FontSelector selector = new FontSelector();
  110. //非汉字字体颜色
  111. Font f1 = FontFactory.getFont(FontFactory.TIMES_ROMAN, size);
  112. f1.setColor(color);
  113. f1.setStyle(bold);
  114. //汉字字体颜色
  115. Font f2 = FontFactory.getFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED, size);
  116. f2.setColor(color);
  117. f2.setStyle(bold);
  118. selector.addFont(f1);
  119. selector.addFont(f2);
  120. Phrase ph = selector.process(cont);
  121. res.add(ph);
  122. return res;
  123. }
  124. }

相关文章