# carota-file-util **Repository Path**: awyoo/carota-file-util ## Basic Information - **Project Name**: carota-file-util - **Description**: 文件链式文件操作 实现pdf文件模板动态填充+word模板填充+word转pdf+电子签章+防伪二维码+水印+PDF文件加密+PDF文件改密等 - **Primary Language**: Unknown - **License**: AGPL-3.0 - **Default Branch**: master - **Homepage**: https://gitee.com/awyoo - **GVP Project**: No ## Statistics - **Stars**: 41 - **Forks**: 16 - **Created**: 2023-04-08 - **Last Updated**: 2025-06-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## carota-file-util #### 📚简介 carota-file-util是一个基于itextpdf 5封装实现的一个操作PDF的工具类,itextpdf 7还在实现中... 通过封装,降低相关API的学习成本,提高工作效率。 文件链式文件操作 实现pdf文件模板动态填充+word模板填充+word转pdf+电子签章+防伪二维码+水印+PDF文件加密+PDF文件改密等 #### 📦安装教程 ###### 在项目的pom.xml的dependencies中加入以下内容: io.gitee.awyoo carota-file-util 1.0.1 #### 使用说明 #### 1.填充PDF模板 @Test public void fillTemplate() { PdfProcessor .builder () .template (getFilePath ("test.pdf")) //模板文件 .output (getOutputPath ("3.pdf")) //输出位置 .fillTemplate () .data (getParam ()) //模板填充的参数 .and () .build () .generate (); } ###### 案例模板文件是项目resources目录下的`test.pdf` 输出位置想项目`src`平级目录`file`文件夹 ###### 模板文件:(使用Adobe Acrobat PDF软件制作的) ![](https://oscimg.oschina.net/oscnet/up-3ca16439f72bd1ed5fa84dc5d1dd04325cb.png) ###### 填充数据 public static Map getParam() { Map map = new HashMap<> (); map.put ("agentName", "张三"); map.put ("idNo", "34242519962415712"); map.put ("company", "测试签章科技有限公司"); map.put ("business", "买牙刷的采购业务"); map.put ("time", DateUtil.formatDate (new Date ())); return map; } ###### 填充完毕的文件 ![](https://oscimg.oschina.net/oscnet/up-bd36f87b7115decda1d22604622339c24b1.png) 是不是很方便 除了支持PDF模板之外,还支持word模板文件动态填充 #### 2.填充word模板 @Test public void fillDocxTemplate() { PdfProcessor .builder () .template (getFilePath ("word.docx")) .output (getOutputPath ("2.pdf")) .fillTemplate (new FillDocxTemplateConfigurer ()) .data (getParam1 ()) .and () .build () .generate (); } ###### 模板文件 ![](https://oscimg.oschina.net/oscnet/up-871db5dcdea440554e97cb0fe4aae325fd1.png) ###### 填充数据 public static Map getParam1() { Map map = new HashMap<> (); map.put ("Name1", "服务注册与发现"); map.put ("Name2", "服务调用"); map.put ("Name3", "负载均衡"); map.put ("Name4", "断路器"); map.put ("Name5", "网关"); map.put ("Name6", "配置中心"); map.put ("time", DateUtil.formatDate (new Date ())); return map; } ###### 填充完毕的文件 ![](https://oscimg.oschina.net/oscnet/up-40552fb58e5b1c4bc0b21d87872018c9771.png) #### 3.电子签章 PdfProcessor .builder () .template (getFilePath ("test.pdf")) .output (getOutputPath ("6.pdf")) .signature () .p12 (getFilePath ("test.p12"), "123456") .chapterImg (getFilePath ("img.png")) .append (true) .reason ("数据不可变更") .location ("CHINA") .page (1) .keyword ("单位盖章") .and () .build () .generate (); #### 4.防伪二维码 PdfProcessor .builder () .template (getFilePath ("test.pdf")) .output (getOutputPath ("6.pdf")) .antiFake () .image (bufferedImage) .and () .build () .generate (); #### 5.水印 PdfProcessor .builder () .template (getFilePath ("test.pdf")) .output (getOutputPath ("6.pdf")) .waterMark () .fontSize (30) .rotation (45) .watermarkText ("内部材料") .type (WaterMarkConfigurer.WatermarkType.TILED) .and () .build () .generate (); #### 6.文件加密 ,改密 PdfProcessor .builder () .template (getFilePath ("test.pdf")) .output (getOutputPath ("6.pdf")) .encrypt () .ownerPassword ("666666").permissions (PdfWriter.ALLOW_PRINTING) .and () .build () .generate (); #### 除了每个功能单独使用之外,支持上述所有功能组合使用 PdfProcessor .builder () .template (getFilePath ("test.pdf")) .output (getOutputPath ("1.pdf")) .fillTemplate (new FillPdfTemplateConfigurer ().data (getParam ())) .and () .antiFake (new AntiFakeConfigurer ().image (bufferedImage)) .and () .encrypt (new EncryptConfigurer ().ownerPassword ("666666").permissions (PdfWriter.ALLOW_PRINTING)) .and () .signature (new SignatureConfigurer () .p12 (getFilePath ("test.p12"), "123456") .chapterImg (getFilePath ("img.png")) .append (true) .reason ("数据不可变更") .location ("CHINA") .page (1) .keyword ("单位盖章") ) .and () .waterMark ().type (WaterMarkConfigurer.WatermarkType.TILED) .fontSize (30) .rotation (45) .and () .build () .generate (); ![img_6.png](https://oscimg.oschina.net/oscnet/up-479b1e019ebb2454f4142346d9325891be0.png) ## 功能不仅仅于此,更加容易的扩展接口,自定义实现的方式去注入, 欢迎小伙伴来使用