当前位置: 首页 > 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.mrgr.cn/news/47685.html

相关文章:

  • 国产信创实践(国能磐石服务器操作系统CEOS +东方通TongHttpServer)
  • LLaMA-Factory web微调大模型并导出大模型
  • 计算机毕业设计Python机器学习农作物健康识别系统 人工智能 图像识别 机器学习 大数据毕业设计 算法
  • 【文件I/O】 总表和分表
  • 简单易用的PDF工具箱
  • stringRedisTemplate.execute执行lua脚本
  • 10.10每日作业
  • [Git] Git下载及使用 从入门到精通 详解(附下载链接)
  • k8s部署jenkins集群,配置集群kubernetes plugin的pod模板
  • x-cmd pkg | fastfetch: 轻松获取系统信息,打造个性化输出!
  • uniapp的相关知识(1)
  • centos7执行yum命令时报:Could not resolve host: mirrorlist.centos.org; Unknown error
  • 网友反馈:移动套餐只能升不能降怎么办?
  • Java多线程面试题
  • 融入、成长与创造 - 数字经济浪潮下的个人转型
  • 论文阅读笔记-Incorporating Copying Mechanism in Sequence-to-Sequence Learning
  • QFontComboBox Class
  • Redis 数据类型list(列表)
  • 系统架构设计师教程 第16章 16.2 嵌入式系统软件架构原理与特征 笔记
  • 十、索引优化与查询优化
  • 解决 IntelliJ IDEA 运行时 “Command line is too long“ 问题
  • 地级市-专利申请与获得情况(1990-2022年)
  • 2024年编程资料【9月份部分】
  • Keil生成lst文件,creating preprocessor file
  • 【分布式技术】简单聊聊什么是区块链
  • 【拥抱AIGC】应该如何衡量AI辅助编程带来的收益