ITEXT 表与表格标题不分开,图与图片标题不分开

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

问题场景

在用ITEXT写PDF的过程中遇到表与表格标题不分开,图与图片标题不分开的需求。

分析

通常报表中的表格和图片的标题会遵循表上图下,为了方便读者阅读一般要求表格与表格标题不分开,图片也是。通过查询API发现ITEXT提供了table.setKeepTogether(true)的方法,由此可以解决上述问题。

解决思路

以表格为例:

  1. 建立一个总的table,a
  2. 设置属性
  3. 为表格标题建立一个table,b
  4. 为表格内容建立一个table,c
  5. 把b,c这两个table作为单元格嵌套到table a

图片情况:
图片当成b,把图片标题当成c即可。

效果

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. //加一些测试内容
  19. for (int i = 0; i < 35; i++) {
  20. document.add(new Paragraph("content" + (i + 1)));
  21. }
  22. PdfPTable table = new PdfPTable(1);
  23. table.setKeepTogether(true);
  24. table.setSplitLate(false);
  25. PdfPTable table1 = new PdfPTable(1);
  26. PdfPCell cell0 = new PdfPCell();
  27. Paragraph p = new Paragraph("table title sample");
  28. p.setAlignment(1);
  29. p.setSpacingBefore(15f);
  30. cell0.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
  31. cell0.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);//然并卵
  32. cell0.setPaddingTop(-2f);//把字垂直居中
  33. cell0.setPaddingBottom(8f);//把字垂直居中
  34. cell0.addElement(p);
  35. cell0.setBorder(0);
  36. table1.addCell(cell0);
  37. PdfPTable table2 = new PdfPTable(2);
  38. for (int a = 0; a < 20; a++) {
  39. PdfPCell cell = new PdfPCell();
  40. Paragraph pp;
  41. if (a == 0 || a == 1) {
  42. pp = str2ParaByTwoFont("tableTitle" + (a + 1), 9f, BaseColor.BLACK, Font.BOLD); //小五 加粗
  43. cell.setBackgroundColor(new BaseColor(128, 128, 255));
  44. } else {
  45. pp = str2ParaByTwoFont("tableContent" + (a - 1), 9f, BaseColor.BLACK); //小五
  46. }
  47. pp.setAlignment(1);
  48. cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
  49. cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);//然并卵
  50. cell.setPaddingTop(-2f);//把字垂直居中
  51. cell.setPaddingBottom(8f);//把字垂直居中
  52. cell.addElement(pp);
  53. table2.addCell(cell);
  54. }
  55. PdfPCell c1 = new PdfPCell();
  56. c1.setBorder(0);
  57. c1.addElement(table1);
  58. PdfPCell c2 = new PdfPCell();
  59. c2.setBorder(0);
  60. c2.addElement(table2);
  61. table.addCell(c1);
  62. table.addCell(c2);
  63. document.add(table);
  64. document.close();
  65. } catch (DocumentException | FileNotFoundException e) {
  66. e.printStackTrace();
  67. }
  68. }
  69. /** * 两种字体显示文字 * @param cont * @param size * @param color * @return */
  70. private static Paragraph str2ParaByTwoFont(String cont, float size, BaseColor color) {
  71. Paragraph res = new Paragraph();
  72. FontSelector selector = new FontSelector();
  73. //非汉字字体颜色
  74. Font f1 = FontFactory.getFont(FontFactory.TIMES_ROMAN, size);
  75. f1.setColor(color);
  76. //汉字字体颜色
  77. Font f2 = FontFactory.getFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED, size);
  78. f2.setColor(color);
  79. selector.addFont(f1);
  80. selector.addFont(f2);
  81. Phrase ph = selector.process(cont);
  82. res.add(ph);
  83. return res;
  84. }
  85. /** * 两种字体显示文字 * @param cont * @param size * @param color * @param bold * @return */
  86. private static Paragraph str2ParaByTwoFont(String cont, float size, BaseColor color, int bold) {
  87. Paragraph res = new Paragraph();
  88. FontSelector selector = new FontSelector();
  89. //非汉字字体颜色
  90. Font f1 = FontFactory.getFont(FontFactory.TIMES_ROMAN, size);
  91. f1.setColor(color);
  92. f1.setStyle(bold);
  93. //汉字字体颜色
  94. Font f2 = FontFactory.getFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED, size);
  95. f2.setColor(color);
  96. f2.setStyle(bold);
  97. selector.addFont(f1);
  98. selector.addFont(f2);
  99. Phrase ph = selector.process(cont);
  100. res.add(ph);
  101. return res;
  102. }
  103. }

相关文章