经过数字签名的文档,能够使作者之外的人无法对其进行修改。因此,在PDF文档中添加数字签名可以保证其安全性和真实性。同时根据添加内容的差异性,数字签名可分为文本数字签名、图片数字签名、图片及文本数字签名。本文将通过Spire.PDF for Java来演示如何在PDF文档中添加数字签名。
使用工具:Spire.PDF for Java 2.11.4
方法1:通过官网下载获取Jar包。下载后,解压文件,并将lib文件夹下的Spire.Pdf.jar文件导入Java程序。(如下图)
方法2:通过maven仓库安装导入。具体安装教程详见此网页。
示例1:添加文本数字签名
import com.spire.pdf.PdfDocument; import com.spire.pdf.graphics.PdfTrueTypeFont; import com.spire.pdf.security.GraphicMode; import com.spire.pdf.security.PdfCertificate; import com.spire.pdf.security.PdfCertificationFlags; import com.spire.pdf.security.PdfSignature; import java.awt.*; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; public class TextSignature { public static void main(String[] args) { //加载PDF文档 PdfDocument doc = new PdfDocument(); doc.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pdf"); //加载pfx证书 PdfCertificate cert = new PdfCertificate("C:\\Users\\Test1\\Desktop\\gary.pfx","abc123"); //添加数字签名,设置其位置和大小 PdfSignature signature = new PdfSignature(doc,doc.getPages().get(doc.getPages().getCount()-1),cert,"MySignature"); Rectangle2D rect = new Rectangle2D.Float(); rect.setFrame(new Point2D.Float((float) doc.getPages().get(0).getActualSize().getWidth() - 270,(float) doc.getPages().get(0).getActualSize().getHeight() - 300),new Dimension(200,110)); signature.setBounds(rect); //设置签名为文本形式 signature.setGraphicMode(GraphicMode.Sign_Detail); //设置签名的内容 signature.setNameLabel("签字者:"); signature.setName("Gary"); signature.setContactInfoLabel("联系电话:"); signature.setContactInfo("010333555"); signature.setDateLabel("日期:"); signature.setDate(new java.util.Date()); signature.setLocationInfoLabel("地点:"); signature.setLocationInfo("US"); signature.setReasonLabel("原因:"); signature.setReason("我是文档所有者"); signature.setDistinguishedNameLabel("DN: "); signature.setDistinguishedName(signature.getCertificate().get_IssuerName().getName()); //设置签名的字体 signature.setSignDetailsFont(new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,11))); //设置文档权限为禁止更改 signature.setDocumentPermissions(PdfCertificationFlags.Forbid_Changes); signature.setCertificated(true); //保存文档 doc.saveToFile("output/TextSignature.pdf"); doc.close(); } }
文本数字签名效果:
import com.spire.pdf.PdfDocument; import com.spire.pdf.graphics.PdfImage; import com.spire.pdf.graphics.PdfTrueTypeFont; import com.spire.pdf.security.GraphicMode; import com.spire.pdf.security.PdfCertificate; import com.spire.pdf.security.PdfCertificationFlags; import com.spire.pdf.security.PdfSignature; import java.awt.*; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; public class ImageSignature { public static void main(String[] args) { //加载PDF文档 PdfDocument doc = new PdfDocument(); doc.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pdf"); //加载pfx证书 PdfCertificate cert = new PdfCertificate("C:\\Users\\Test1\\Desktop\\gary.pfx","abc123"); //添加数字签名,设置其位置和大小 PdfSignature signature = new PdfSignature(doc,"MySignature"); Rectangle2D rect = new Rectangle2D.Float(); rect.setFrame(new Point2D.Float((float) doc.getPages().get(0).getActualSize().getWidth() - 250,new Dimension(170,150)); signature.setBounds(rect); //设置签名为图片形式 signature.setGraphicMode(GraphicMode.Sign_Image_Only); signature.setSignImageSource(PdfImage.fromFile("C:\\Users\\Test1\\Desktop\\Image.png")); //设置签名的字体 signature.setSignDetailsFont(new PdfTrueTypeFont(new Font("Arial Unicode MS",11))); //设置文档权限为禁止更改 signature.setDocumentPermissions(PdfCertificationFlags.Forbid_Changes); signature.setCertificated(true); //保存文档 doc.saveToFile("output/ImageSignature.pdf"); doc.close(); } }
import com.spire.pdf.PdfDocument; import com.spire.pdf.graphics.*; import com.spire.pdf.security.GraphicMode; import com.spire.pdf.security.PdfCertificate; import com.spire.pdf.security.PdfCertificationFlags; import com.spire.pdf.security.PdfSignature; import java.awt.*; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; public class TextAndImageSignature { public static void main(String[] args) { //加载PDF文档 PdfDocument doc = new PdfDocument(); doc.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pdf"); //加载pfx证书 PdfCertificate cert = new PdfCertificate("C:\\Users\\Test1\\Desktop\\gary.pfx","MySignature"); Rectangle2D rect = new Rectangle2D.Float(); rect.setFrame(new Point2D.Float((float) doc.getPages().get(0).getActualSize().getWidth() - 350,(float) doc.getPages().get(0).getActualSize().getHeight() - 170),new Dimension(300,170)); signature.setBounds(rect); //设置签名为图片加文本模式 signature.setGraphicMode(GraphicMode.Sign_Image_And_Sign_Detail); //设置签名的内容 signature.setNameLabel("签字者:"); signature.setName("Gary"); signature.setContactInfoLabel("联系电话:"); signature.setContactInfo("010333555"); signature.setDateLabel("日期:"); signature.setDate(new java.util.Date()); signature.setLocationInfoLabel("地点:"); signature.setLocationInfo("US"); signature.setReasonLabel("原因:"); signature.setReason("我是文档所有者"); signature.setDistinguishedNameLabel("DN: "); signature.setDistinguishedName(signature.getCertificate().get_IssuerName().getName()); signature.setSignImageSource(PdfImage.fromFile("C:\\Users\\Test1\\Desktop\\Image.png")); //设置签名的字体 signature.setSignDetailsFont(new PdfTrueTypeFont(new Font("Arial Unicode MS",11))); //设置文档权限为禁止更改 signature.setDocumentPermissions(PdfCertificationFlags.Forbid_Changes); signature.setCertificated(true); //保存文档 doc.saveToFile("output/TextAndImageSignature.pdf"); doc.close(); } }
原文链接:https://www.f2er.com/java/534905.html