java提取pdf证书信息

时间:2024-03-10 10:56:11

1.导入jarbao

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
     </repository>
</repositories>
<dependencies>
<dependency>
        <groupId>e-iceblue</groupId>
  <artifactId>spire.pdf</artifactId>
  <version>3.11.6</version>
</dependency>
</dependencies>

2.工具类

import com.spire.pdf.PdfDocument;
import com.spire.pdf.security.PdfSignature;
import com.spire.pdf.widget.PdfFormFieldWidgetCollection;
import com.spire.pdf.widget.PdfFormWidget;
import com.spire.pdf.widget.PdfSignatureFieldWidget;
import org.junit.Test;

public class text {

    @Test
    public void text () {
        //创建pdfdocument对象
        PdfDocument pdf = new PdfDocument();

        //加载pdf文件
        pdf.loadFromFile("H:\\javac\\pdf\\src\\main\\resources\\static\\2.pdf");

        //获取域集合
        PdfFormWidget pdfFormWidget = (PdfFormWidget) pdf.getForm();
        PdfFormFieldWidgetCollection fieldsWidget = pdfFormWidget.getFieldsWidget();

        //遍历域
        for (int i = 0; i < fieldsWidget.getCount(); i++) {
            //判断域是否为签名
            if (fieldsWidget.get(i) instanceof PdfSignatureFieldWidget) {
                //获取域签名
                PdfSignatureFieldWidget pdfSignatureFieldWidget = (PdfSignatureFieldWidget) fieldsWidget.get(i);

                //获取签名
                PdfSignature signature = pdfSignatureFieldWidget.getSignature();
				
		//签名的信息
                String locationInfo = signature.getLocationInfo();
                String reason = signature.getReason();
                String data = signature.getDate().toString();
                String signatureName = signature.getSignatureName();

                System.out.println("签名位置信息:"+ locationInfo +"\n"+
                        "签名原因:" + reason +"\n"+
                        "签名日期:"+ data +"\n"+
                        "签名人:"+ signatureName +"\n"+
                        "文档中的签名坐标:X = "+ pdfSignatureFieldWidget.getLocation().getX()+ "  Y = "+ pdfSignatureFieldWidget.getLocation().getY()
                );
                

            }
        }
    }
}

参考文档:https://www.cnblogs.com/Yesi/p/14107362.html