Spire.PDF for Android via Java 10.8.5 现已正式发布。该版本新增支持使用 SHA-256/SHA-512 算法证书对 PDF 文档进行签名。详情如下。
新功能:
- 新增对 SHA-256/SHA-512 算法证书签名的支持。
public static void main(String[] args) throws IOException {
// Create pdf document PdfDocument doc = new PdfDocument();
// Load file from disk doc.loadFromFile("Sample.pdf");
// Load the X509 certificate for signature PdfPKCS7Formatter formatter = new PdfPKCS7Formatter(new PdfCertificate("gary.pfx", "e-iceblue"), false);
// Create an instance of PdfOrdinarySignatureMaker using the loaded document and certificate PdfOrdinarySignatureMaker signatureMaker = new PdfOrdinarySignatureMaker(doc, formatter);
// Create an instance of PdfCustomSignatureAppearance as the appearance for the signature IPdfSignatureAppearance signatureAppearance = new PdfCustomSignatureAppearance();
// Make the signature with a specified name and the custom appearance signatureMaker.makeSignature("Signature", signatureAppearance);
// Iterate through all hash algorithm types for (HashAlgorithmType hashAlg : HashAlgorithmType.values()) {
// Skip SM3 (case-insensitive) if ("SM3".equalsIgnoreCase(hashAlg.name())) {
continue;
}
try {
// Set the current hash algorithm formatter.getProperties().setHashAlgorithm(hashAlg);
String filePath = "AddImageSignature_" + hashAlg.name() + ".pdf";
doc.saveToFile(filePath, FileFormat.PDF);
System.out.println("成功:" + hashAlg.name());
} catch (Exception ex) {
System.out.println("失败,报错的 HashAlgorithmType:" + hashAlg.name());
System.out.println("错误详情:" + ex.getMessage());
}
}
// Close the document doc.close();
}
// Custom signature appearance implementation public static class PdfCustomSignatureAppearance implements IPdfSignatureAppearance {
@Override
public void generate(PdfCanvas pdfCanvas) {
// Set font size int fontSize = 10;
// Create Arial font PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.PLAIN, fontSize), true);
// Set line height float lineHeight = fontSize;
// Draw text string pdfCanvas.drawString("AAAAAAAAAAA", font, PdfBrushes.getRed(), new Point.Float(0, 0));
// Draw image at specified position pdfCanvas.drawImage(PdfImage.fromFile("E-iceblue logo.png"), new Point.Float(20, 0));
}
}
获取 Spire.PDF for Android via Java 10.8.5 请点击:
https://www.e-iceblue.cn/Downloads/pdf-for-android-via-java.html







