导入jar包

<!-- itextpdf --><dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.4.3</version></dependency><dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version></dependency>

具体实现代码

import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.*;import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;/*** @author wjl* @version 1.0* @date 2022/7/1 下午3:19*/
public class UpdatePdfUtil {/*** @param inputPDFFile 文件流* @param contentText  添加的文字* @param page         添加的第几页* @param x            添加位置x坐标* @param y            添加位置y坐标* @return* @throws Exception*/public static InputStream addText(InputStream inputPDFFile, String contentText, Integer page, Integer x, Integer y)throws Exception {PdfReader reader = new PdfReader(inputPDFFile);ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();PdfStamper stamper = new PdfStamper(reader, byteArrayOutputStream);int total = reader.getNumberOfPages() + 1;PdfContentByte content;BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);PdfGState gs = new PdfGState();//content = stamper.getOverContent(i);// 在内容上方加水印content = stamper.getUnderContent(page);//在内容下方加水印gs.setFillOpacity(0.2f);content.beginText();//字体大小content.setFontAndSize(base, 9);//content.setTextMatrix(70, 200);//内容居中,横纵坐标,偏移量content.showTextAligned(Element.ALIGN_CENTER, contentText, x, y, 0);// //添加图片// Image image = Image.getInstance("D:\\测试图片.jpg");//// /*//   img.setAlignment(Image.LEFT | Image.TEXTWRAP);//   img.setBorder(Image.BOX); img.setBorderWidth(10);//   img.setBorderColor(BaseColor.WHITE); img.scaleToFit(100072);//大小//   img.setRotationDegrees(-30);//旋转//  */// //图片的位置(坐标)// image.setAbsolutePosition(520, 786);// // image of the absolute// image.scaleToFit(200, 200);// image.scalePercent(15);//依照比例缩放// content.addImage(image);content.setFontAndSize(base, 8);content.endText();stamper.close();//关闭打开的原来PDF文件,不执行reader.close()删除不了(必须先执行stamper.close(),否则会报错)reader.close();//删除原来的PDF文件// File targetTemplePDF = new File(inputPDFFilePath);// targetTemplePDF.delete();inputPDFFile.close();return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());}public static InputStream addImage(InputStream inputPDFFile, InputStream imgFile, Integer page, Integer x, Integer y)throws Exception {PdfReader reader = new PdfReader(inputPDFFile);ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();PdfStamper stamper = new PdfStamper(reader, byteArrayOutputStream);int total = reader.getNumberOfPages() + 1;PdfContentByte content;BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);PdfGState gs = new PdfGState();//content = stamper.getOverContent(i);// 在内容上方加水印content = stamper.getUnderContent(page);//在内容下方加水印gs.setFillOpacity(0.2f);content.beginText();//添加图片Image image = Image.getInstance(imgFile.readAllBytes());/*img.setAlignment(Image.LEFT | Image.TEXTWRAP);img.setBorder(Image.BOX); img.setBorderWidth(10);img.setBorderColor(BaseColor.WHITE); img.scaleToFit(100072);//大小img.setRotationDegrees(-30);//旋转*///图片的位置(坐标)image.setAbsolutePosition(x, y);// image of the absoluteimage.scaleToFit(200, 200);image.scalePercent(15);//依照比例缩放content.addImage(image);content.setFontAndSize(base, 8);content.endText();stamper.close();//关闭打开的原来PDF文件,不执行reader.close()删除不了(必须先执行stamper.close(),否则会报错)reader.close();//删除原来的PDF文件// File targetTemplePDF = new File(inputPDFFilePath);// targetTemplePDF.delete();inputPDFFile.close();return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());}

PDF文件添加图片、文字合成(java)相关推荐

  1. java实现给PDF文件添加图片水印,java实现给PDF文件添加文字水印

    接上一篇,pdf跟tif 是一起做的 java实现 1.给PDF文件添加图片水印: public static void waterMark1(String inputFile,String outp ...

  2. 如何实现给PDF文件添加图片

    给PDF文件添加图片,屏幕前的你知不知道应该如何操作呢?很多人虽然有接触过PDF文件的经历,但并不知道PDF文件应该如何修改,自然也不清楚该如何给PDF文件添加图片了.所以,今天小编就给大家带来给PD ...

  3. itext 5.3.0实现对pdf文件添加(文字和图片)水印

    在itext 较新的版本中, 对中文的支持还是存在着问题,在网络上得到的信息和多方尝试下,将字体文件xx.TTF放到项目里面,然后加载到BaseFont 中,可行.如下: BaseFont font ...

  4. java 生成pdf文件,添加图片

    所需要的包: 1.  iText    下载地址:  http://nchc.dl.sourceforge.net/sourceforge/itext/itext-2.0.3.jar 2.  iTex ...

  5. 咖啡汪日志——JAVA导出pdf文件加水印 文字+图片、文字

    咖啡汪日志--JAVA导出pdf文件加水印 文字和图片.文字 hello,又大家见面了! 作为一只不是在戏精就是在戏精路上的哈士奇,今天要展示给大家的就是如何快捷地给pdf文件增加各种水印.嗷呜呜,前 ...

  6. PDF文件怎么编辑,怎么在PDF中添加图片

    现在我们会经常使用到PDF文件,有的时候我们还需要制作PDF文件,给PDF文件添加文字这种基本的操作还会知道如何操作,或许有小伙伴知道怎么在PDF中添加图片吗?不会的小伙伴就来看看下面的文章吧. 1. ...

  7. 如何批量给pdf文件添加文字水印?

    工作中我们会给重要的办公文件文件水印,给文件加上公司的名称等,这样可以有效防止文件内容被别人盗用抄袭,其中就包括word.Excel.PPT.图片.PDF等文件.PDF文件由于其特殊性,越来越成为最常 ...

  8. python使用fpdf生成发票格式的pdf文件包含:文字、图片logo、表格、条形码等;

    python使用fpdf生成发票格式的pdf文件包含:文字.图片logo.表格.条形码等: pip install fpdf #python使用fpdf生成发票格式的pdf文件包含:文字.图片logo ...

  9. Java实现pdf文件转图片

    Java实现pdf文件转图片 文章顺序是按照测试类- -Service- -Service实现类- -工具类- - POM依赖. test测试类里 pdfPath:存放pdf源文件的地方 imgflo ...

  10. 怎么用迅捷PDF转换器在线为PDF文件添加文字内容

    电子文档的广泛应用导致PDF格式文件成为办公中少不了的一部分,在日常工作中,我们经常需要在PDF文档中添加文字,例如签名.批注或补充说明等.那么如何才能快速在PDF文件添加文字内容?有什么文档编辑工具 ...

最新文章

  1. 韦东奕陈杲同获达摩院青橙奖,90后数学新星光彩夺目,却说「没有黄金一代」...
  2. python中的json注意事项
  3. 基金指数温度怎么算_鱼缸加热棒必不可少的东西,我们怎么正确使用?
  4. Dex文件格式扫描器:特征API的检测和扫描-小工具一枚(转载)
  5. 使用Keil5构建GD32450i-EVAL工程
  6. 撤消git pull,如何将repos带到旧状态
  7. mysql数据库集群版_MySQL数据库集群实战
  8. Maven的下载与配置(包含阿里云镜像配置)
  9. 遇到from playsound import playsoundModuleNotFoundError: No module named ‘playsound‘解决办法
  10. 联想拯救者2020 Y7000安装Ubuntu16.04
  11. 基本知识 100176
  12. oracle数据库处于recover,oracle数据库recover和restore的区别
  13. Cpp多重继承会产生的问题
  14. 从写作到演讲,虾米君不断尝试的 2021|年终回顾
  15. 妈妈计算机英语怎么说,妈妈的英文翻译,妈妈英语怎么说
  16. crontab定时任务
  17. JQuery实现图片点击放大
  18. 联合会杯-内马尔破门锋霸2球 巴西3-0西班牙3连冠
  19. matlab rand randn 每次生成的随机数都一样的解决方案
  20. Mac下将文件复制到移动硬盘

热门文章

  1. 使用FlashFXP连接Linux乱码问题
  2. 鸿蒙和米家啥区别,魅族家居接入鸿蒙!
  3. jquery html5关系图,html5 canvas复杂人物关系图设置代码
  4. shal()函数绕过和session验证绕过
  5. gw node节点 xshell连接 访问外网
  6. PS:修复图片模糊(字体)
  7. 今日头条推荐算法原理全文详解
  8. 产品设计学习(三)——用户画像与特征工程
  9. android root 升级失败怎么办,安卓手机ROOT失败的常见原因及解决办法
  10. android 手机型号获取root,Android用代码获取手机root之后的最高权限