pdf 添加电子签名

 用到的依赖:

        <!-- 签名包-->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcpkix-jdk15on</artifactId>
            <version>1.64</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.20</version>
        </dependency>
        <!-- 签名包-->

 直接上代码:

package com.secxiot.common.utils;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * 用户签名书
 */
public class SignWriteToPDF {
    /**
     * signImagePath 签名图片
     * inputPDFPath 原文件路径
     * outPDFPath 输出文件路径
     */
    public static void setWaterPrint(String outPDFPath, String inputPDFPath, String signImagePath) throws DocumentException, IOException {
        BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        InputStream input = new FileInputStream(new File(inputPDFPath));
        PdfReader reader = new PdfReader(input);

        OutputStream output = new FileOutputStream(new File(outPDFPath));
        PdfStamper stamper = new PdfStamper(reader, output);
        PdfContentByte page = stamper.getOverContent(1);

        //将文字贴入pdf
       /* page.beginText();
        page.setFontAndSize(baseFont,12);
        BaseColor coler = new BaseColor(0, 0, 0);
        page.setColorFill(coler);
        page.setTextMatrix(100,500); //设置文字在页面中的坐标
        page.showText("66666666666666666");
        page.endText();*/

        //将图片贴入pdf
        Image image = Image.getInstance(signImagePath);
        image.setAbsolutePosition(480, 130); //设置图片在页面中的坐标
        //image.scalePercent(20);//依照比例缩放
        image.setRotationDegrees(90);//旋转 角度

        image.scaleAbsolute(image.getWidth()/10, image.getWidth()/10);
        page.addImage(image);

        stamper.close();
        reader.close();
        input.close();
    }

    public static void main(String[] args) throws DocumentException, IOException {
        setWaterPrint("H:\\photo\\spatient_sign_48.pdf", "H:\\photo\\spatient_sign.pdf", "H:\\photo\\711664989302785.png");
    }

    /**
     *
     * @param pdfPath pdf 路径
     * @param imgPath 图片路径
     * @param imgType 图片类型
     */
    public static void pdfTOimg(String pdfPath, String imgPath, String imgType) {
        File file = new File(pdfPath);
        try {
            PDDocument doc = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();
            for(int i=0;i<pageCount;i++) {
                BufferedImage image = renderer.renderImageWithDPI(i, 296);
                //          BufferedImage image = renderer.renderImage(i, 2.5f);
                ImageIO.write(image, imgType, new File(imgPath));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除文件
     * @param fileName 文件路径
     * @return
     */
    public static boolean deleteFile(String fileName) {
        File file = new File(fileName);
        if (file.exists() && file.isFile()) {
            if (file.delete()) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值