• java使用itext生成pdf


    效果:

    maven依赖

    1. com.itextpdf
    2. itextpdf
    3. 5.5.13.3

     代码:

    1. import cn.hutool.core.date.DateUtil;
    2. import com.itextpdf.text.*;
    3. import com.itextpdf.text.pdf.*;
    4. import lombok.SneakyThrows;
    5. import org.springframework.web.bind.annotation.GetMapping;
    6. import org.springframework.web.bind.annotation.RequestMapping;
    7. import org.springframework.web.bind.annotation.RestController;
    8. import javax.servlet.http.HttpServletResponse;
    9. import java.io.IOException;
    10. /**
    11. * @projectName: util-cloud
    12. * @package: com.zxw.controller
    13. * @className: PDFController
    14. * @author: zhangxuewei
    15. * @description: TODO
    16. * @date: 2023/9/8 9:52
    17. * @version: 1.0
    18. */
    19. @RestController
    20. @RequestMapping("/pdf")
    21. public class PDFController {
    22. @GetMapping("/generate-pdf3")
    23. public void generatePdf3(HttpServletResponse response) throws IOException, DocumentException {
    24. // 设置响应内容类型为PDF
    25. response.setContentType("application/pdf");
    26. response.setHeader("Content-Disposition", "attachment; filename=sample.pdf");
    27. // 设置中文字体,这里使用了 楷体常规
    28. BaseFont bf = BaseFont.createFont("c://windows//fonts//simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
    29. Font font = new Font(bf, 12);
    30. // 创建PDF文档
    31. Document document = new Document();
    32. PdfWriter pdfWriter = PdfWriter.getInstance(document, response.getOutputStream());
    33. // 添加水印
    34. pdfWriter.setPageEvent(new WatermarkPageEvent());
    35. // 打开文档
    36. document.open();
    37. // 添加中文内容
    38. Paragraph paragraph = new Paragraph("你好,iText 5!", font);
    39. document.add(paragraph);
    40. font.setColor(BaseColor.GREEN);
    41. Paragraph paragraphc = new Paragraph("这是一个字体颜色为绿色的段落", font);
    42. document.add(paragraphc);
    43. Font font2 = new Font(bf, 18, Font.BOLD);
    44. Paragraph paragraph2 = new Paragraph("这是一个字体大小为18的粗体段落", font2);
    45. document.add(paragraph2);
    46. Font fontb = new Font(bf, 12);
    47. Chunk yellowChunk = new Chunk(" 这是一个背景颜色为黄色的段落 ", fontb);
    48. yellowChunk.setBackground(BaseColor.YELLOW);
    49. Paragraph paragraphcb = new Paragraph();
    50. paragraphcb.add(yellowChunk);
    51. document.add(paragraphcb);
    52. // 创建表格
    53. PdfPTable table = new PdfPTable(3); // 3列的表格
    54. table.setWidthPercentage(100);
    55. table.setSpacingBefore(10f);
    56. table.setSpacingAfter(10f);
    57. // 第一行数据
    58. PdfPCell cell1 = new PdfPCell(new Paragraph("列1"));
    59. cell1.setBorderColor(BaseColor.BLUE);
    60. cell1.setPadding(5);
    61. PdfPCell cell2 = new PdfPCell(new Paragraph("列2"));
    62. cell2.setBorderColor(BaseColor.RED);
    63. cell2.setPadding(5);
    64. PdfPCell cell3 = new PdfPCell(new Paragraph("列3"));
    65. cell3.setBorderColor(BaseColor.GREEN);
    66. cell3.setPadding(5);
    67. table.addCell(cell1);
    68. table.addCell(cell2);
    69. table.addCell(cell3);
    70. document.add(table);
    71. // 关闭文档
    72. document.close();
    73. }
    74. // 自定义页面事件类来添加水印
    75. static class WatermarkPageEvent extends PdfPageEventHelper {
    76. @SneakyThrows
    77. @Override
    78. public void onEndPage(PdfWriter writer, Document document) {
    79. BaseFont bf = BaseFont.createFont("c://windows//fonts//simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
    80. Font font = new Font(bf, 24, Font.NORMAL, new BaseColor(128, 128, 128));
    81. // 获取页面大小
    82. Rectangle pageSize = document.getPageSize();
    83. String now = DateUtil.now();
    84. String info = " 张学伟 " + now;
    85. for (int i = 0; i < 5; i++) {
    86. info += info;
    87. }
    88. // 创建水印文本
    89. PdfContentByte content = writer.getDirectContentUnder();
    90. PdfGState gs = new PdfGState();
    91. gs.setFillOpacity(0.4f); // 设置透明度
    92. content.setGState(gs);
    93. float xInterval = 100; // 水印之间的水平间隔
    94. float yInterval = 100; // 水印之间的垂直间隔
    95. // Y坐标为0,沿X轴每间隔100生成水印信息
    96. for (float x = 0; x < pageSize.getWidth(); x = x + xInterval) {
    97. ColumnText.showTextAligned(content,
    98. Element.ALIGN_CENTER, new Phrase(info, font),
    99. x, 0, 45);
    100. }
    101. // X坐标为0,沿Y轴每间隔100生成水印信息
    102. for (float y = 100; y < pageSize.getHeight(); y = y + xInterval) {
    103. ColumnText.showTextAligned(content,
    104. Element.ALIGN_CENTER, new Phrase(info, font),
    105. 0, y, 45);
    106. }
    107. }
    108. }
    109. }

    已完善导出PDF的util请移步 

    java生成PDF的Util_Mr_ZhangAdd的博客-CSDN博客

    查看效果 

  • 相关阅读:
    客户端负载均衡策略:loadBalancer,ribbon
    `useState` 和 `useImmer` 都是 React 中用于管理状态的钩子
    第十九章总结:Java绘图
    使用 Windows 包管理器 (winget) 安装 .Net
    Linux内存泄露案例分析和内存管理分享
    记一次python 正则表达式
    Elasticsearch之索引简单应用
    FFplay文档解读-17-音频过滤器二
    java毕业设计精品在线试题库系统(附源码、数据库)
    Kubernetes:(十八)flannel网络
  • 原文地址:https://blog.csdn.net/Mr_ZhangAdd/article/details/132877685