Adding a Graphical Seal and Network Timestamp Electronic Signature to PDF Documents with Java and Spire

This tutorial demonstrates how to programmatically embed a graphical seal and a trusted network timestamp electronic signature into a PDF using Java, the Spire library, and a .pfx certificate, covering preparation, signature creation, positioning, timestamp configuration, visual settings, and final saving.

360 Quality & Efficiency
360 Quality & Efficiency
360 Quality & Efficiency
Adding a Graphical Seal and Network Timestamp Electronic Signature to PDF Documents with Java and Spire

Electronic seals are commonly seen on digital invoices and contracts; they combine a visual seal image with a timestamped electronic signature that complies with the Chinese Electronic Signature Law, giving the document legal effect.

The following example shows how to add such a seal and a network timestamp signature to a PDF using Java and the Spire framework. Before starting, you need a .pfx digital certificate file and a PNG image of the seal.

Step 1: Load the PDF document

PdfDocument pdf = new PdfDocument();
String filename = "~/Tmp/Window.pdf";
System.out.println(new File(filename).exists());
pdf.loadFromFile(filename);

Step 2: Load the certificate and create the signature object

String caFile = "/Users/walker/Tmp/certifation2/server.pfx";
PdfCertificate cert = new PdfCertificate(caFile, "likuo.dev");
PdfSignature signature = new PdfSignature(pdf, pdf.getPages().get(1), cert, "电子数据签名");

Step 3: Define the signature position and size on the page

PdfSignature signature = new PdfSignature(pdf, pdf.getPages().get(1), cert, "电子数据签名");
Rectangle2D rect = new Rectangle2D.Float();
float x = (float) (pdf.getPages().get(0).getActualSize().getWidth() - 340);
float y = (float) (pdf.getPages().get(0).getActualSize().getHeight() - 230);
Dimension dimension = new Dimension(300, 150);
Point2D point2D = new Point2D.Float(x, y);
rect.setFrame(point2D, dimension);
signature.setBounds(rect);

Step 4: Configure a trusted timestamp service

String timestampeServerUrl = "http://timestamp.digicert.com";
signature.configureTimestamp(timestampeServerUrl);

Step 5: Set signature visual details

signature.setGraphicMode(GraphicMode.Sign_Image_And_Sign_Detail);
signature.setNameLabel("名称:");
signature.setName("李xxx");
signature.setDateLabel("日期:");
signature.setDate(new java.util.Date());
signature.setLocationInfoLabel("地点:");
signature.setLocationInfo("北京市-北京市");
signature.setReasonLabel("原由:");
signature.setReason("电子xxx合同");
signature.setDistinguishedNameLabel("DN: ");
signature.setDistinguishedName(signature.getCertificate().get_IssuerName().getName());
signature.setDistinguishedNameLabel("序列:");
signature.setDistinguishedName("20210813第【李】23次");
signature.setSignImageSource(PdfImage.fromFile("~.png"));

Step 6: Set font and document permissions

signature.setSignDetailsFont(new PdfTrueTypeFont(new Font("Arial Unicode MS", Font.PLAIN, 9)));
signature.setDocumentPermissions(PdfCertificationFlags.Forbid_Changes);
signature.setCertificated(true);

Step 7: Save the signed PDF

File file = new File(filename);
String fileName = file.getName();
if (fileName.contains(".")) {
    pdf.saveToFile(fileName.substring(0, fileName.indexOf(".")) + "_Sign.pdf");
} else {
    pdf.saveToFile(fileName + "_Sign.pdf");
}
pdf.close();

After completing these steps, the PDF contains a network‑timestamped electronic signature, which helps verify document integrity, origin, and protects intellectual property, making it suitable for electronic invoices, contracts, and forensic evidence.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

timestampPDFdigital signatureElectronic SealSpire
360 Quality & Efficiency
Written by

360 Quality & Efficiency

360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.