PDF文件添加图片、文字合成(java)

导入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 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());
    }

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值