Java使用iTextPDF生成PDF文件的实现方法

时间:2022-11-06 14:35:31

iText介绍和说明

因为项目需要生成PDF文件,所以去找了一下能够生成PDF的Java工具,看到了iText可以说好评如潮。

如果你想通过java操作PDF文件,那么 iText 绝对是你的首选。

引入依赖

这里使用的是iText5

  1. <dependency>
  2. <groupId>com.itextpdf</groupId>
  3. <artifactId>itextpdf</artifactId>
  4. <version>5.5.10</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.itextpdf</groupId>
  8. <artifactId>itext-asian</artifactId>
  9. <version>5.2.0</version>
  10. </dependency>

使用步骤简单介绍

快速入手iText拢共需要5步:

  1. 创建文档实例 Document
  2. 获取PdfWriter实例 (需要指定Document实例 和pdf 生成的磁盘路径)
  3. 打开文档
  4. 添加段落内容
  5. 关闭操作文档实例 (操作完成后必须执行文档关闭操作)

创建工具类

  1. public class PdfUtil {
  2. // 标准字体
  3. public static Font NORMALFONT;
  4. // 加粗字体
  5. public static Font BOLDFONT;
  6. //固定高
  7. public static float fixedHeight = 27f;
  8. //间距
  9. public static int spacing = 5;
  10.  
  11. static {
  12. try {
  13. BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
  14. NORMALFONT = new Font(bfChinese, 10, Font.NORMAL);
  15. BOLDFONT = new Font(bfChinese, 14, Font.BOLD);
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }
  19.  
  20. }
  21.  
  22. public static Document createDocument() {
  23. //生成pdf
  24. Document document = new Document();
  25. // 页面大小
  26. Rectangle rectangle = new Rectangle(PageSize.A4);
  27. // 页面背景颜色
  28. rectangle.setBackgroundColor(BaseColor.WHITE);
  29. document.setPageSize(rectangle);
  30. // 页边距 左,右,上,下
  31. document.setMargins(20, 20, 20, 20);
  32. return document;
  33. }
  34.  
  35. /**
  36. * @param text 段落内容
  37. * @return
  38. */
  39. public static Paragraph createParagraph(String text, Font font) {
  40. Paragraph elements = new Paragraph(text, font);
  41. elements.setSpacingBefore(5);
  42. elements.setSpacingAfter(5);
  43. elements.setSpacingAfter(spacing);
  44. return elements;
  45. }
  46.  
  47. public static Font createFont(int fontNumber, int fontSize, BaseColor fontColor) {
  48. //中文字体 ----不然中文会乱码
  49. BaseFont bf = null;
  50. try {
  51. bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
  52. return new Font(bf, fontNumber, fontSize, fontColor);
  53. } catch (Exception e) {
  54. e.printStackTrace();
  55. }
  56. return new Font(bf, Font.DEFAULTSIZE, Font.NORMAL, BaseColor.BLACK);
  57. }
  58.  
  59. /**
  60. * 隐藏表格边框线
  61. *
  62. * @param cell 单元格
  63. */
  64. public static void disableBorderSide(PdfPCell cell) {
  65. if (cell != null) {
  66. cell.disableBorderSide(1);
  67. cell.disableBorderSide(2);
  68. cell.disableBorderSide(4);
  69. cell.disableBorderSide(8);
  70. }
  71. }
  72.  
  73. /**
  74. * 创建居中得单元格
  75. *
  76. * @return
  77. */
  78. public static PdfPCell createCenterPdfPCell() {
  79. PdfPCell cell = new PdfPCell();
  80. cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  81. cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  82. cell.setFixedHeight(fixedHeight);
  83. return cell;
  84. }
  85.  
  86. /**
  87. * 创建指定文字得单元格
  88. *
  89. * @param text
  90. * @return
  91. */
  92. public static PdfPCell createCenterPdfPCell(String text, int rowSpan, int colSpan, Font font) {
  93. PdfPCell cell = new PdfPCell(new Paragraph(text, font));
  94. cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  95. cell.setHorizontalAlignment(Element.ALIGN_LEFT);
  96. cell.setFixedHeight(fixedHeight);
  97. cell.setRowspan(rowSpan);
  98. cell.setColspan(colSpan);
  99. return cell;
  100. }
  101.  
  102. /**
  103. * @param len 表格列数
  104. * @return
  105. */
  106. public static PdfPTable createPdfPTable(int len) {
  107. PdfPTable pdfPTable = new PdfPTable(len);
  108. pdfPTable.setSpacingBefore(5);
  109. pdfPTable.setHorizontalAlignment(Element.ALIGN_CENTER);
  110. return pdfPTable;
  111. }
  112. }

创建controller进行测试

  1. /**
  2. * @author Wang Guolong
  3. * @version 1.0
  4. * @date 2020/6/28 3:17 下午
  5. */
  6. @RestController
  7. @RequestMapping("/pdf")
  8. public class PdfController {
  9.  
  10. @RequestMapping("/generate")
  11. public Response generatePDF(HttpServletResponse response) throws Exception {
  12. String filename = "测试pdf";
  13. // 设置下载格式为pdf
  14. response.setContentType("application/x-download");
  15. response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8") + ".pdf");
  16. OutputStream os = new BufferedOutputStream(response.getOutputStream());
  17.  
  18. // 1. Document document = new Document();
  19. Document document = PdfUtil.createDocument();
  20. // 2. 获取writer
  21. PdfWriter.getInstance(document, os);
  22. // 3. open()
  23. document.open();
  24.  
  25. //设置字体
  26. Font blackFont = PdfUtil.createFont(10, Font.NORMAL, BaseColor.BLACK);
  27. Font blueFont = PdfUtil.createFont(10, Font.NORMAL, BaseColor.BLUE);
  28. Font bigFont = PdfUtil.createFont(14, Font.NORMAL, BaseColor.BLACK);
  29. Font littleFont = PdfUtil.createFont(10, Font.NORMAL, BaseColor.BLACK);
  30.  
  31. Paragraph title = PdfUtil.createParagraph("测试pdf", bigFont);
  32. title.setAlignment(Element.ALIGN_CENTER);
  33. // 4. 添加段落内容
  34. document.add(title);
  35. // 5. close()
  36. document.close();
  37. os.close();
  38. return new Response().setContent("success");
  39. }
  40. }

运行结果

下载页面:

Java使用iTextPDF生成PDF文件的实现方法

下载的文件效果:

Java使用iTextPDF生成PDF文件的实现方法

到此这篇关于Java使用iTextPDF生成PDF文件的实现方法的文章就介绍到这了,更多相关Java使用iTextPDF生成PDF内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/MCmango/article/details/113933761