CommonPdfUtil.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package vip.xiaonuo.common.util;
  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. public class CommonPdfUtil {
  7. /** 标准字体 */
  8. public static Font NORMALFONT;
  9. /** 加粗字体 */
  10. public static Font BOLDFONT;
  11. /** 固定高 */
  12. public static float fixedHeight = 27f;
  13. /** 间距 */
  14. public static int spacing = 5;
  15. /**
  16. * 设置中文格式防止乱码
  17. */
  18. static {
  19. try {
  20. BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
  21. NORMALFONT = new Font(bfChinese, 10, Font.NORMAL);
  22. BOLDFONT = new Font(bfChinese, 14, Font.BOLD);
  23. } catch (Exception e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. /**
  28. *
  29. * @return
  30. */
  31. public static Document createDocument() {
  32. //生成pdf
  33. Document document = new Document();
  34. // 页面大小
  35. Rectangle rectangle = new Rectangle(PageSize.A4);
  36. // 页面背景颜色
  37. rectangle.setBackgroundColor(BaseColor.WHITE);
  38. document.setPageSize(rectangle);
  39. // 页边距 左,右,上,下
  40. document.setMargins(20, 20, 20, 20);
  41. return document;
  42. }
  43. /**
  44. * @param len 表格列数
  45. * @return
  46. */
  47. public static PdfPTable createPdfPTable(int len) {
  48. PdfPTable pdfPTable = new PdfPTable(len);
  49. pdfPTable.setSpacingBefore(5);
  50. pdfPTable.setHorizontalAlignment(Element.ALIGN_CENTER);
  51. return pdfPTable;
  52. }
  53. /**
  54. * 创建指定文字得单元格
  55. * @param text
  56. * @return
  57. */
  58. public static PdfPCell createCenterPdfPCell(String text, int rowSpan, int colSpan, Font font) {
  59. PdfPCell cell = new PdfPCell(new Paragraph(text, font));
  60. cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  61. //默认文字居中显示
  62. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  63. cell.setFixedHeight(fixedHeight);
  64. cell.setRowspan(rowSpan);
  65. cell.setColspan(colSpan);
  66. return cell;
  67. }
  68. /**
  69. * @param text 段落内容
  70. * @return
  71. */
  72. public static Paragraph createParagraph(String text, Font font) {
  73. Paragraph elements = new Paragraph(text, font != null ? font :createFont(10, Font.NORMAL,BaseColor.BLACK) );
  74. elements.setSpacingBefore(5);
  75. elements.setSpacingAfter(5);
  76. elements.setSpacingAfter(spacing);
  77. return elements;
  78. }
  79. public static Font createFont(int fontSize, int fontStyle, BaseColor fontColor) {
  80. //中文字体 ----不然中文会乱码
  81. BaseFont bf = null;
  82. try {
  83. bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
  84. return new Font(bf, fontSize, fontStyle, fontColor);
  85. } catch (Exception e) {
  86. e.printStackTrace();
  87. }
  88. return new Font(bf, Font.DEFAULTSIZE, Font.NORMAL, BaseColor.BLACK);
  89. }
  90. }