当前位置: 首页 > news >正文

openpdf

1、简介

2、示例

2.1 引入依赖

        <dependency><groupId>com.github.librepdf</groupId><artifactId>openpdf</artifactId><version>1.3.34</version></dependency><dependency><groupId>com.github.librepdf</groupId><artifactId>openpdf-fonts-extra</artifactId><version>1.3.34</version></dependency>

2.2 代码

2.2.1 主程序

String filePath = "/Users/xingyu/Documents/tmp/a.pdf";FileOutputStream fos = new FileOutputStream(filePath);PdfWriter pdfWriter = null;Document document = null;try {document = new Document();document.setPageSize(PageSize.A4);pdfWriter = PdfWriter.getInstance(document, fos);document.open();Image rightTopIcon = getRightTopIcon("4");document.add(rightTopIcon);Paragraph titleParagraph = createParagraph("新模版002", FONT_TITLE, Element.ALIGN_CENTER, 0);document.add(titleParagraph);ProcessPdfVO processPdf = new ProcessPdfVO();processPdf.setCompanyName("xxx公司");processPdf.setCreateTime("2024-05-27");processPdf.setProcessId("202405270004");processPdf.setPrintDateTime("2024-05-28 17:25:09");processPdf.setPrinter("张三");PdfPTable firstRow = createFirstRow(processPdf);document.add(firstRow);PdfPTable mainTable = createMainTable();PdfPCell cell = new PdfPCell(createParagraph("审批流程", FONT_MAIN_TABLE, Element.ALIGN_CENTER, 0));cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);mainTable.addCell(cell);PdfPTable subTable = new PdfPTable(2);subTable.setWidths(new float[]{30,70});subTable.setWidthPercentage(100);  // 使得子表铺满单元格PdfPCell subCell1 = new PdfPCell(createParagraph("subCell1", FONT_MAIN_TABLE, Element.ALIGN_CENTER, 0));subCell1.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);subCell1.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);subCell1.setBorder(Cell.BOX);subTable.addCell(subCell1);PdfPCell subCell2 = new PdfPCell(createParagraph("subCell2", FONT_MAIN_TABLE, Element.ALIGN_CENTER, 0));subCell2.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);subCell2.setBorder(Cell.BOX);subTable.addCell(subCell2);PdfPCell subCell3 = new PdfPCell(createParagraph("subCell3", FONT_MAIN_TABLE, Element.ALIGN_CENTER, 0));subCell3.setBorder(Cell.BOX);subCell3.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);subTable.addCell(subCell3);PdfPCell subCell4 = new PdfPCell(createParagraph("subCell4", FONT_MAIN_TABLE, Element.ALIGN_CENTER, 0));subCell4.setBorder(Cell.BOX);subCell4.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);subTable.addCell(subCell4);subTable.setComplete(true);PdfPCell subCell5 = new PdfPCell();subCell5.setBorder(Cell.BOX);subCell5.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);subCell5.setPadding(0);subCell5.addElement(subTable);mainTable.addCell(subCell5);mainTable.addCell(new PdfPCell(createParagraph("审批流程1", FONT_MAIN_TABLE, Element.ALIGN_CENTER, 0)));mainTable.addCell(new PdfPCell(createParagraph("审批流程2", FONT_MAIN_TABLE, Element.ALIGN_CENTER, 0)));mainTable.addCell(new PdfPCell(createParagraph("审批流程3", FONT_MAIN_TABLE, Element.ALIGN_CENTER, 0)));mainTable.addCell(subCell5);mainTable.setComplete(true);document.add(mainTable);PdfPTable lastTable = createLastTable(processPdf);document.add(lastTable);} finally {IoUtil.close(document);IoUtil.close(pdfWriter);IoUtil.close(fos);}

2.2.2 表格

        private static PdfPTable createMainTable() {PdfPTable table = new PdfPTable(2);float[] width = getUnitValues(table.getNumberOfColumns(), new float[]{30f, 70f});table.setWidths(width);return table;}private static float[] getUnitValues(int columnNum, float[] percents) {float[] unitValues = new float[columnNum];for (int i = 0; i < columnNum; i++) {float percentValue;if (Objects.nonNull(percents) && columnNum == percents.length) {percentValue = percents[i];} else {percentValue = BigDecimal.valueOf(100).divide(BigDecimal.valueOf(columnNum), 10, RoundingMode.HALF_UP).floatValue();}unitValues[i] = percentValue;}return unitValues;}

2.2.3 单元格

/*** 创建单元格** @param horizontalAlignment 水平位置* @param verticalAlignment   垂直位置* @param border              边框* @param borderColor         边框颜色* @return 单元格*/private static PdfPCell createCell(Integer horizontalAlignment, Integer verticalAlignment, Integer border,Color borderColor) {PdfPCell cell = new PdfPCell();if (Objects.nonNull(horizontalAlignment)) {cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);}if (Objects.nonNull(verticalAlignment)) {cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);}if (Objects.nonNull(border)) {cell.setBorder(border);}if (Objects.nonNull(borderColor)) {cell.setBorderColor(borderColor);}return cell;}

2.2.4 创建单元格

/*** 创建单元格** @param horizontalAlignment 水平位置* @param verticalAlignment   垂直位置* @param border              边框* @param borderColor         边框颜色* @return 单元格*/private static PdfPCell createCell(Integer horizontalAlignment, Integer verticalAlignment, Integer border,Color borderColor) {PdfPCell cell = new PdfPCell();if (Objects.nonNull(horizontalAlignment)) {cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);}if (Objects.nonNull(verticalAlignment)) {cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);}if (Objects.nonNull(border)) {cell.setBorder(border);}if (Objects.nonNull(borderColor)) {cell.setBorderColor(borderColor);}return cell;}

2.2.5 图片

private static Image getRightTopIcon(String processStatus) {Image image = null;ProcessStatusEnum processStatusEnum = ProcessStatusEnum.getEnum(processStatus);if (Objects.isNull(processStatusEnum)) {return image;}if (StringUtils.isBlank(processStatusEnum.getIcon())) {return image;}String iconPath = String.format(Locale.ROOT, "%s%s%s", "images", File.separator, processStatusEnum.getIcon());InputStream is = null;ByteArrayOutputStream bos = null;try {ClassPathResource classPathResource = new ClassPathResource(iconPath);is = classPathResource.getInputStream();bos = new ByteArrayOutputStream();IoUtil.copy(is, bos);is.close();image = Image.getInstance(bos.toByteArray());image.scalePercent(50);image.setAbsolutePosition(500,700);} catch (Exception e) {throw new RuntimeException(e);} finally {IoUtil.close(is);IoUtil.close(bos);}return image;}

2.2.6 预览图

在这里插入图片描述

http://www.lryc.cn/news/458491.html

相关文章:

  • C#垃圾回收机制详解
  • 身份证二要素核验操作指南
  • 量子数字签名概述
  • 算法题——合并 k 个升序的链表
  • 智能制造与精益制造的模型搭建
  • 快速生成生产级Go应用的利器——Cgapp
  • MySQL基本语法、高级语法知识总结以及常用语法案例
  • 单片机(学习)2024.10.11
  • Java创建型模式(二)——工厂模式(简单工厂模式、工厂方法模式、抽象工厂模式、工厂模式扩展等完整详解,附有代码——案例)
  • C++学习,容器类 <set>
  • Cisco Catalyst 9000 交换产品系列 IOS XE 17.15.1 发布下载,新增功能概览
  • Python知识点:基于Python技术,如何使用MMDetection进行目标检测
  • Chromium HTML Tags与c++接口对应关系分析
  • React Fiber 解析:前端性能提升密码
  • 【吊打面试官系列-微服务面试题】微服务架构如何运作?
  • Windows系统编程 - 目录操作、磁盘、卷信息
  • 搭建SaaS知识库:优化教育机构的在线教学效能
  • CSS中backdrop-filter详解
  • AI测试入门:理解 LLM 的基准测试(Benchmark)
  • InternVid:用于多模态视频理解与生成的大规模视频-文本数据集 | ICLR Spotlight
  • Hive数仓操作(十)
  • Android 扩大View的点击区域
  • [Qt学习笔记] 解决QTextEdit数据过多UI卡死问题
  • OgreNext高级材质中增加线宽,点大小,虚线模式绘制支持
  • STM32中的DMA数据转运——下篇
  • 51单片机的智能小区安防系统【proteus仿真+程序+报告+原理图+演示视频】
  • 数仓建模流程
  • Neo4j CQL语句 使用教程
  • STM32-HAL库 驱动DS18B20温度传感器 -- 2024.10.8
  • HTML 符号