PDF添加文字或者图片

 引入依赖

<dependency>
	<groupId>org.apache.pdfbox</groupId>
	<artifactId>fontbox</artifactId>
	<version>2.0.4</version>
</dependency>
<dependency>
	<groupId>org.apache.pdfbox</groupId>
	<artifactId>pdfbox</artifactId>
	<version>2.0.4</version>
</dependency>

参数说明

inputPdfFilePath输入文件路径

outPutPdfFilePath输出文件路径

imagePros图片集合

pdfList文字集合

public static void writeToPdf(String inputPdfFilePath, String outPutPdfFilePath, List<ImagePro> imagePros, List<PdfPro> pdfList)
            throws Exception {
        //append 追加
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outPutPdfFilePath, false));
        PdfReader reader = new PdfReader(inputPdfFilePath);
        PdfStamper stamper = new PdfStamper(reader, bos);
        for (PdfPro pdfPro : pdfList) {
            Integer pageNum = pdfPro.getPageNum();
            int start;
            int end;
            if (pageNum == null) {
                start = 1;
                end = reader.getNumberOfPages();
            } else {
                start = pageNum;
                end = pageNum;
            }
            PdfContentByte content;
            //BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
            // "c:\\windows\\fonts\\SIMHEI.TTF" 使用windows系统的黑体
            BaseFont base = BaseFont.createFont(SsmConstant.FONT_URL, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            PdfGState gs = new PdfGState();
            for (int i = start; i <= end; i++) {
                //content = stamper.getOverContent(i);// 在内容上方加水印
                content = stamper.getUnderContent(i);//在内容下方加水印
                gs.setFillOpacity(0.2f);
                content.beginText();
                //字体大小
                content.setFontAndSize(base, 10.5F);
                //content.setTextMatrix(390, 810);
                //内容居中,横纵坐标,偏移量
                content.showTextAligned(Element.ALIGN_CENTER, pdfPro.getText(), pdfPro.getX(), pdfPro.getY(), 0);
                content.setFontAndSize(base, 8);
                content.endText();
            }
        }
        for (ImagePro imagePro : imagePros) {
            Image image = Image.getInstance(imagePro.getImgPath());
            /* 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(imagePro.getX(), imagePro.getY());
            // image of the absolute
            image.scaleToFit(200, 200);
            image.scalePercent(imagePro.getScalePercent());//依照比例缩放. 调整缩放,控制图片大小
            PdfContentByte content = stamper.getUnderContent(6);
            content.addImage(image);
        }
        stamper.close();
        //关闭打开的原来PDF文件,不执行reader.close()删除不了(必须先执行stamper.close(),否则会报错)
        reader.close();
        //删除原来的PDF文件
        /*File targetTemplePDF = new File(inputPDFFilePath); targetTemplePDF.delete();*/
}

 pdfPro

@Data
public class PdfPro {

    private String text;

    private float x;

    private float y;

    private Integer pageNum;

    public PdfPro(float x, float y, String text) {
        this.text = text;
        this.x = x;
        this.y = y;
    }

    public PdfPro(float x, float y, String text,Integer pageNum) {
        this.text = text;
        this.x = x;
        this.y = y;
        this.pageNum = pageNum;
    }

}

 ImagePro

@Data
public class ImagePro { 
    private float x; //x 坐标
    private float y; //y 坐标
    private float scalePercent;  //缩放百分比
    private String imgPath; //路径

    private Integer pageNum;

    public ImagePro() { 
    }
    public ImagePro(float x, float y, float scalePercent, String imgPath,Integer pageNum) {
        this.x = x;
        this.y = y;
        this.scalePercent = scalePercent;
        this.imgPath = imgPath;
        this.pageNum = pageNum;
    }
}

使用示例

public static String getSignPdf(String file, String signUrl, float x, float y,float scalePercent,String agreementNo0,String agreementNo1) {
        String outPutFile = SsmConstant.WRITE_TO_PDF_URL + UUID.fastUUID() + ".PDF";
        List<ImagePro> imageList = new ArrayList<>();
        //盖章
        ImagePro imagePro = new ImagePro(x, y, scalePercent, signUrl, 6);
        imageList.add(imagePro);
        List<PdfPro> pdfList = new ArrayList<>();
        // 合同编号
        if(agreementNo0 != null && agreementNo1 != null){
            pdfList.add(new PdfPro(471.99628f, 769.37f, agreementNo0));
            pdfList.add(new PdfPro(500.99628f, 769.37f, agreementNo1));
        }
        try {
            EditorPDF.writeToPdf(file, outPutFile, imageList, pdfList);
        } catch (Exception e) {
            throw new BizException("PDF生成异常");
        }
        return outPutFile;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值