Я использую PDFBox 3.0.5 и прикрепленные внешние библиотеки для генерации PDF -файла, а затем подписать его. Я могу правильно выполнить цифровое подписание, с Adobe Acrobat PDF, показывающим данные подписи на панели подписи.
Я использую PDFBox 3.0.5 и прикрепленные внешние библиотеки для генерации PDF -файла, а затем подписать его. Я могу правильно выполнить цифровое подписание, с Adobe Acrobat PDF, показывающим данные подписи на панели подписи.[code]import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.font.PDType1Font; import org.apache.pdfbox.pdmodel.font.Standard14Fonts; import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation; import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationRubberStamp; import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature; import org.apache.pdfbox.pdmodel.interactive.digitalsignature.SignatureInterface;
// Add a blank page PDPage page = new PDPage(); document.addPage(page);
// Write some text PDPageContentStream contentStream = new PDPageContentStream(document, page); contentStream.beginText(); contentStream.newLineAtOffset(100, 700); contentStream.setFont(new PDType1Font(Standard14Fonts.FontName.HELVETICA_BOLD), 12); contentStream.showText("Hello, this is a test PDF for signing!"); contentStream.endText(); contentStream.close();
// Save as input.pdf document.save("C:/Users/2588372/input16.pdf"); System.out.println("9- PDF created: input.pdf"); } catch (Exception e) { e.printStackTrace(); } String inputFile = "C:/Users/2588372/input16.pdf"; // original file String outputFile = "C:/Users/2588372/signed16.pdf"; // signed file String keystoreFile = "C:/Users/2588372/keystore.p12"; // certificate file String keystorePassword = "test2025";
// Load the PKCS12 keystore KeyStore keystore = KeyStore.getInstance("PKCS12"); try (InputStream ks = new FileInputStream(keystoreFile)) { keystore.load(ks, keystorePassword.toCharArray()); }
document.close(); System.out.println("10 - Signed PDF created: " + outputFile); } @Override public byte[] sign(InputStream content) { try {
System.out.println("5 -Signed PDF created");
// Convert InputStream to byte array ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[8192]; int n; while ((n = content.read(buffer)) != -1) { baos.write(buffer, 0, n); } byte[] data = baos.toByteArray();
// Wrap data CMSProcessableByteArray msg = new CMSProcessableByteArray(data);
// Build certificate store List certList = Arrays.asList(certificateChain); JcaCertStore certs = new JcaCertStore(certList);
// Create generator CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
ContentSigner sha256Signer = new JcaContentSignerBuilder("SHA256withRSA") .setProvider("BC") // ensure BC provider .build(privateKey);
gen.addSignerInfoGenerator( new JcaSignerInfoGeneratorBuilder( new JcaDigestCalculatorProviderBuilder().setProvider("BC").build() ).build(sha256Signer, (java.security.cert.X509Certificate) certificateChain[0]) );