Java在pdf文件中加入文字或图片

**

pom

导入itextpdf** 依赖

<!-- 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>

**

工具类

**
传入流返回流,因为在公司项目中用流做操作
xy坐标取文本左上角的点
注释调的内容为本地文件测试代码

import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.*;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;


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());
    }
}
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值