}
else
{
String password = args[0];
String infile = args[1];
PDDocument document = null;
try
{
document = PDDocument.load( infile );
if( document.isEncrypted() )
{
document.decrypt( password );
}
else
{
System.err.println( "Warning: Document is not encrypted." );
}
COSDictionary trailer = document.getDocument().getTrailer();
COSDictionary root = (COSDictionary)trailer.getDictionaryObject( COSName.ROOT );
COSDictionary acroForm = (COSDictionary)root.getDictionaryObject( COSName.getPDFName( "AcroForm" ) );
COSArray fields = (COSArray)acroForm.getDictionaryObject( COSName.getPDFName( "Fields" ) );
for( int i=0; i<fields.size(); i++ )
{
COSDictionary field = (COSDictionary)fields.getObject( i );
String type = field.getNameAsString( "FT" );
if( "Sig".equals( type ) )
{
COSDictionary cert = (COSDictionary)field.getDictionaryObject( COSName.getPDFName( "V" ) );
if( cert != null )
{
System.out.println( "Certificate found" );
System.out.println( "Name=" + cert.getDictionaryObject( COSName.getPDFName( "Name" ) ) );
System.out.println( "Modified=" + cert.getDictionaryObject( COSName.getPDFName( "M" ) ) );
COSName subFilter = (COSName)cert.getDictionaryObject( COSName.getPDFName( "SubFilter" ) );
if( subFilter != null )
{
if( subFilter.getName().equals( "adbe.x509.rsa_sha1" ) )
{
COSString certString = (COSString)cert.getDictionaryObject(
COSName.getPDFName( "Cert" ) );
byte[] certData = certString.getBytes();
CertificateFactory factory = CertificateFactory.getInstance( "X.509" );
ByteArrayInputStream certStream = new ByteArrayInputStream( certData );
Collection certs = factory.generateCertificates( certStream );
System.out.println( "certs=" + certs );
}
else if( subFilter.getName().equals( "adbe.pkcs7.sha1" ) )
{
COSString certString = (COSString)cert.getDictionaryObject(
COSName.getPDFName( "Contents" ) );
byte[] certData = certString.getBytes();
CertificateFactory factory = CertificateFactory.getInstance( "X.509" );
ByteArrayInputStream certStream = new ByteArrayInputStream( certData );
Collection certs = factory.generateCertificates( certStream );
System.out.println( "certs=" + certs );
}
else
{
System.err.println( "Unknown certificate type:" + subFilter );
}
}
else
{
throw new IOException( "Missing subfilter for cert dictionary" );
}
}
else
{
System.out.println( "Signature found, but no certificate" );
}
}
}
}
finally
{
if( document != null )
{
document.close();
}
}
}
}