📄 coswriter.java
字号:
if( value instanceof COSDictionary )
{
addObjectToWrite( value );
writeReference( value );
}
else if( value instanceof COSObject )
{
COSBase subValue = ((COSObject)value).getObject();
if( subValue instanceof COSDictionary || subValue == null )
{
addObjectToWrite( value );
writeReference( value );
}
else
{
subValue.accept( this );
}
}
else
{
value.accept(this);
}
getStandardOutput().writeEOL();
}
else
{
//then we won't write anything, there are a couple cases
//were the value of an entry in the COSDictionary will
//be a dangling reference that points to nothing
//so we will just not write out the entry if that is the case
}
}
getStandardOutput().write(DICT_CLOSE);
getStandardOutput().writeEOL();
return null;
}
catch( IOException e )
{
throw new COSVisitorException(e);
}
}
/**
* The visit from document method.
*
* @param doc The object that is being visited.
*
* @throws COSVisitorException If there is an exception while visiting this object.
*
* @return null
*/
public Object visitFromDocument(COSDocument doc) throws COSVisitorException
{
try
{
doWriteHeader(doc);
doWriteBody(doc);
doWriteXRef(doc);
doWriteTrailer(doc);
return null;
}
catch (IOException e)
{
throw new COSVisitorException(e);
}
}
/**
* visitFromFloat method comment.
*
* @param obj The object that is being visited.
*
* @throws COSVisitorException If there is an exception while visiting this object.
*
* @return null
*/
public Object visitFromFloat(COSFloat obj) throws COSVisitorException
{
try
{
obj.writePDF( getStandardOutput() );
return null;
}
catch (IOException e)
{
throw new COSVisitorException(e);
}
}
/**
* visitFromFloat method comment.
*
* @param obj The object that is being visited.
*
* @throws COSVisitorException If there is an exception while visiting this object.
*
* @return null
*/
public Object visitFromInt(COSInteger obj) throws COSVisitorException
{
try
{
obj.writePDF( getStandardOutput() );
return null;
}
catch (IOException e)
{
throw new COSVisitorException(e);
}
}
/**
* visitFromName method comment.
*
* @param obj The object that is being visited.
*
* @throws COSVisitorException If there is an exception while visiting this object.
*
* @return null
*/
public Object visitFromName(COSName obj) throws COSVisitorException
{
try
{
obj.writePDF( getStandardOutput() );
return null;
}
catch (IOException e)
{
throw new COSVisitorException(e);
}
}
/**
* visitFromNull method comment.
*
* @param obj The object that is being visited.
*
* @throws COSVisitorException If there is an exception while visiting this object.
*
* @return null
*/
public Object visitFromNull(COSNull obj) throws COSVisitorException
{
try
{
obj.writePDF( getStandardOutput() );
return null;
}
catch (IOException e)
{
throw new COSVisitorException(e);
}
}
/**
* visitFromObjRef method comment.
*
* @param obj The object that is being visited.
*
* @throws COSVisitorException If there is an exception while visiting this object.
*/
public void writeReference(COSBase obj) throws COSVisitorException
{
try
{
COSObjectKey key = getObjectKey(obj);
getStandardOutput().write(String.valueOf(key.getNumber()).getBytes());
getStandardOutput().write(SPACE);
getStandardOutput().write(String.valueOf(key.getGeneration()).getBytes());
getStandardOutput().write(SPACE);
getStandardOutput().write(REFERENCE);
}
catch (IOException e)
{
throw new COSVisitorException(e);
}
}
/**
* visitFromStream method comment.
*
* @param obj The object that is being visited.
*
* @throws COSVisitorException If there is an exception while visiting this object.
*
* @return null
*/
public Object visitFromStream(COSStream obj) throws COSVisitorException
{
try
{
if(willEncrypt)
{
document.getSecurityHandler().decryptStream(
obj,
currentObjectKey.getNumber(),
currentObjectKey.getGeneration());
}
InputStream input = obj.getFilteredStream();
// set the length of the stream and write stream dictionary
COSObject lengthObject = new COSObject( null );
obj.setItem(COSName.LENGTH, lengthObject);
//obj.accept(this);
// write the stream content
visitFromDictionary( obj );
getStandardOutput().write(STREAM);
getStandardOutput().writeCRLF();
byte[] buffer = new byte[1024];
int amountRead = 0;
int totalAmountWritten = 0;
while( (amountRead = input.read(buffer,0,1024)) != -1 )
{
getStandardOutput().write( buffer, 0, amountRead );
totalAmountWritten += amountRead;
}
lengthObject.setObject( new COSInteger( totalAmountWritten ) );
getStandardOutput().writeCRLF();
getStandardOutput().write(ENDSTREAM);
getStandardOutput().writeEOL();
return null;
}
catch( Exception e )
{
throw new COSVisitorException(e);
}
}
/**
* visitFromString method comment.
*
* @param obj The object that is being visited.
*
* @return null
*
* @throws COSVisitorException If there is an exception while visiting this object.
*/
public Object visitFromString(COSString obj) throws COSVisitorException
{
try
{
if(willEncrypt)
{
document.getSecurityHandler().decryptString(
obj,
currentObjectKey.getNumber(),
currentObjectKey.getGeneration());
}
obj.writePDF( getStandardOutput() );
}
catch (Exception e)
{
throw new COSVisitorException(e);
}
return null;
}
/**
* This will write the pdf document.
*
* @param doc The document to write.
*
* @throws COSVisitorException If an error occurs while generating the data.
*/
public void write(COSDocument doc) throws COSVisitorException
{
PDDocument pdDoc = new PDDocument( doc );
write( pdDoc );
}
/**
* This will write the pdf document.
*
* @param doc The document to write.
*
* @throws COSVisitorException If an error occurs while generating the data.
*/
public void write(PDDocument doc) throws COSVisitorException
{
document = doc;
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);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -