openpdf

embedded/2024/12/22 1:47:50/

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 主程序

java">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 表格

java">        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 单元格

java">/*** 创建单元格** @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 创建单元格

java">/*** 创建单元格** @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 图片

java">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.ppmy.cn/embedded/127905.html

相关文章

前端 | Uncaught (in promise) undefined

前端 | Uncaught (in promise) undefined 最近开发运行前端项目时&#xff0c;经常预计控制台报错 &#xff0c;如下图&#xff1a; 这里我总结下&#xff0c;这种报错的场景和原因&#xff0c;并通过实际代码案例帮助小伙伴更好理解下 。 文章目录 前端 | Uncaught (in promi…

Wordpress—一个神奇的个人博客搭建框架

wordpress简介 在当今数字化的时代&#xff0c;拥有一个属于自己的个人博客&#xff0c;不仅可以记录生活点滴、分享专业知识&#xff0c;还能展示个人风采。而在众多的博客搭建框架中&#xff0c;Wordpress 以其强大的功能和灵活性脱颖而出。今天&#xff0c;就让我们一起深入…

个人用数据挖掘笔记(待补充)

文章目录 零、复习事前准备一、绪论期末主观题 二、数据仓库&OLAP理论数据仓库数据仓库多维建模概念分层&#xff08;把底层概念映射到更高层、更一般的概念&#xff09;维度分层数值分层 建模方式数据立方体组成星形模型&#xff08;Star schema&#xff09;雪花模型&…

如何给低代码平台取名?命名策略与技巧全解析

低代码平台正日益成为企业数字化转型的重要工具。为了确保您的平台能够脱颖而出&#xff0c;吸引到更多的用户和开发者&#xff0c;选择一个恰到好处的名字至关重要。本文将探讨如何为您的低代码平台选择一个既独特又易于记忆的好名字&#xff0c;并详细介绍一系列行之有效的命…

Spring Boot教育资源库:技术精进的桥梁

2 相关技术简介 2.1Java技术 Java是一种非常常用的编程语言&#xff0c;在全球编程语言排行版上总是前三。在方兴未艾的计算机技术发展历程中&#xff0c;Java的身影无处不在&#xff0c;并且拥有旺盛的生命力。Java的跨平台能力十分强大&#xff0c;只需一次编译&#xff0c;任…

OCR文档识别技术的优缺点

OCR&#xff08;Optical Character Recognition&#xff0c;光学字符识别&#xff09;识别技术作为一种图像处理技术&#xff0c;在多个领域具有广泛的应用&#xff0c;但同时也存在一些局限性。以下是对OCR识别技术优缺点的详细分析&#xff1a; 优点 提高工作效率&#xff1…

uniapp小程序监听外接扫描枪

场景&#xff1a;uniapp打包的app在手持设备上使用&#xff0c;手持设备外接扫描枪&#xff0c;快速扫描 关键&#xff1a;扫描枪一般是触发 键盘事件keydown或keyup 无输入框式 import keymap from ./keymap export default {data() {return {inputString: ,inputCache: }}…

Redis 实现 查找附近的人 功能

文章目录 概述Redis 中 Geospatial&#xff08;地理位置&#xff09;Demo例子总结 概述 使用 Redis 实现“查找附近的人”功能&#xff0c;通常会依赖 Redis 的 Geo&#xff08;地理位置&#xff09; 数据类型来存储用户的经纬度&#xff0c;并基于此进行地理范围查询。Redis …