2016-05-24 4 views
1

PDDocumentにデジタル署名した後、Apache PDFBoxでPDDocumentを閉じました。私は警告を得る:You did not close PDF Document私は私のインスタンスをシャットダウンします。 PDDocumentが作成され、正しく閉じられている場所は1つだけです。Apache PDFBox 2.0にサインインするときに「PDF文書を閉じなかった」

コード:

private byte[] buildDocument(File pdfToSign, PDVisibleSigProperties visibleSigProperties) throws Exception 
{ 
    FileOutputStream fos = null; 
    PDDocument doc = null; 
    try 
    { 
     String signedPdfName = pdfToSign.getName().substring(0, pdfToSign.getName().indexOf(".")); 
     File signedFile = File.createTempFile(signedPdfName + "_signed", null); 
     signedFile.deleteOnExit(); 

     fos = new FileOutputStream(signedFile); 
     doc = PDDocument.load(pdfToSign); 

     // create signature dictionary 
     PDSignature signature = new PDSignature(); 
     signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE); 

     // subfilter for basic and PAdES Part 2 signatures 
     signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED); 
     signature.setName(visibleSigProperties.getSignerName()); 
     signature.setLocation(visibleSigProperties.getSignerLocation()); 
     signature.setReason(visibleSigProperties.getSignatureReason()); 

     // the signing date, needed for valid signature 
     signature.setSignDate(Calendar.getInstance()); 

     // register signature dictionary and sign interface 
     SignatureOptions options = new SignatureOptions(); 
     options.setVisualSignature(visibleSigProperties); 
     options.setPage(visibleSigProperties.getPage() - 1); 
     doc.addSignature(signature, this, options); 
     byte[] pdfInBytes = IOUtils.toByteArray(new FileInputStream(signedFile)); 
     return pdfInBytes; 
    } 
    finally 
    { 
     if(doc != null) 
     { 
      // write incremental (only for signing purpose) 
      doc.saveIncremental(fos); 
      doc.close(); 
     } 
     if(fos != null) 
     { 
      fos.flush(); 
      fos.close(); 
     } 
    } 

} 

署名インタフェースの実装

/** 
* Signature Interface implementation 
* This is called by pdf Box 
*/ 
public byte[] sign(InputStream content) throws IOException 
{ 
    try 
    { 
     List<Certificate> certList = new ArrayList<Certificate>(); 
     certList.add(getCertificate()); 
     Store certs = new JcaCertStore(certList); 
     CMSSignedDataGenerator gen = new CMSSignedDataGenerator(); 
     org.bouncycastle.asn1.x509.Certificate cert = org.bouncycastle.asn1.x509.Certificate.getInstance(ASN1Primitive.fromByteArray(getCertificate().getEncoded())); 
     ContentSigner sha1Signer = new JcaContentSignerBuilder("SHA256WithRSA").build(getPrivateKey()); 
     gen.addSignerInfoGenerator(new JcaSignerInfoGeneratorBuilder(new JcaDigestCalculatorProviderBuilder().build()).build(sha1Signer, new X509CertificateHolder(cert))); 
     gen.addCertificates(certs); 
     CMSProcessableInputStream msg = new CMSProcessableInputStream(content); 
     CMSSignedData signedData = gen.generate(msg, false); 
     return signedData.getEncoded(); 
    } 
    catch (GeneralSecurityException e) 
    { 
     throw new IOException(e); 
    } 
    catch (CMSException e) 
    { 
     throw new IOException(e); 
    } 
    catch (OperatorCreationException e) 
    { 
     throw new IOException(e); 
    } 
} 
+1

閉じるオプションを(ドキュメントを閉じた後に)試してください。それは役に立ちますか? (例も見てください) –

+0

それはそうでした。ありがとう。 – Cybermonk

答えて

1

文書を閉じた後optionsを閉じてください:

if(doc != null) 
    { 
     // write incremental (only for signing purpose) 
     doc.saveIncremental(fos); 
     doc.close(); 
     IOUtils.closeQuietly(options); 
    } 

理由はoptionsビジュアル署名テンプレートが含まれていることです。

関連する問題