if(doc.isAllSecurityToBeRemoved())
{
this.willEncrypt = false;
// also need to get rid of the "Encrypt" in the trailer so readers
// don't try to decrypt a document which is not encrypted
COSDocument cosDoc = doc.getDocument();
COSDictionary trailer = cosDoc.getTrailer();
trailer.removeItem(COSName.getPDFName("Encrypt"));
}
else
{
SecurityHandler securityHandler = document.getSecurityHandler();
if(securityHandler != null)
{
try
{
securityHandler.prepareDocumentForEncryption(document);
this.willEncrypt = true;
}
catch(IOException e)
{
throw new COSVisitorException( e );
}
catch(CryptographyException e)
{
throw new COSVisitorException( e );
}
}
else
{
this.willEncrypt = false;
}
}
COSDocument cosDoc = document.getDocument();
COSDictionary trailer = cosDoc.getTrailer();
COSArray idArray = (COSArray)trailer.getDictionaryObject( "ID" );
if( idArray == null )
{
try
{
//algothim says to use time/path/size/values in doc to generate
//the id. We don't have path or size, so do the best we can
MessageDigest md = MessageDigest.getInstance( "MD5" );
md.update( Long.toString( System.currentTimeMillis()).getBytes() );
COSDictionary info = (COSDictionary)trailer.getDictionaryObject( "Info" );
if( info != null )
{
Iterator values = info.getValues().iterator();
while( values.hasNext() )
{
md.update( values.next().toString().getBytes() );
}
}
idArray = new COSArray();
COSString id = new COSString( md.digest() );
idArray.add( id );
idArray.add( id );
trailer.setItem( "ID", idArray );
}
catch( NoSuchAlgorithmException e )
{
throw new COSVisitorException( e );
}
}
/*
List objects = doc.getObjects();
Iterator iter = objects.iterator();
long maxNumber = 0;
while( iter.hasNext() )
{
COSObject object = (COSObject)iter.next();
if( object.getObjectNumber() != null &&
object.getGenerationNumber() != null )
{
COSObjectKey key = new COSObjectKey( object.getObjectNumber().longValue(),
object.getGenerationNumber().longValue() );
objectKeys.put( object.getObject(), key );
objectKeys.put( object, key );
maxNumber = Math.max( key.getNumber(), maxNumber );
setNumber( maxNumber );
}
}*/
cosDoc.accept(this);
}