ItextPdf给PDF批量插入图片

String InPdfFile = "D:\\导入资料\\a.pdf";
String outPdfFile = "D:\\导出资料\\b.pdf";
String imgPath = "C:\\Users\\tang\\Desktop\\微信图片_20210621092925.png";
try {
    PdfReader reader = new PdfReader(InPdfFile, "PDF".getBytes());
    PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(outPdfFile));

    int pages = reader.getNumberOfPages();

    // 设置透明度
    PdfGState gs = new PdfGState();
    gs.setStrokeOpacity(0.8f);
    gs.setFillOpacity(0.8f);

    for (int i = 1;i <= pages; i++){
        Rectangle cropBox = reader.getCropBox(i);

        Image img = Image.getInstance(imgPath);// 插入业务章   
        //设置缩放比例
        img.scaleAbsolute(115,115);
         //设置业务章的位置
        img.setAbsolutePosition((float)(cropBox.getWidth()*0.2),(float)(cropBox.getHeight()*0.6));
        PdfContentByte over = stamp.getOverContent(i);
        over.setGState(gs);
        over.addImage(img);
    }

    stamp.close();// 关闭          
    File tempfile = new File(InPdfFile);

    if (tempfile.exists()) {
        tempfile.delete();
    }
} catch (Exception e) {
    e.printStackTrace();
}

你可能感兴趣的:(Java,java,itext)