This is the in-memory representation of the PDF document. You need to call
close() on this object when you are done using it!!
Method from org.apache.pdfbox.cos.COSDocument Detail: |
public Object accept(ICOSVisitor visitor) throws COSVisitorException {
return visitor.visitFromDocument( this );
}
visitor pattern double dispatch method. |
public void close() throws IOException {
if( scratchFile != null )
{
scratchFile.close();
scratchFile = null;
}
if( tmpFile != null )
{
tmpFile.delete();
tmpFile = null;
}
}
This will close all storage and delete the tmp files. |
public void dereferenceObjectStreams() throws IOException {
for( COSObject objStream : getObjectsByType( "ObjStm" ) )
{
COSStream stream = (COSStream)objStream.getObject();
PDFObjectStreamParser parser = new PDFObjectStreamParser( stream, this );
parser.parse();
for( COSObject next : parser.getObjects() )
{
COSObjectKey key = new COSObjectKey( next );
COSObject obj = getObjectFromPool( key );
obj.setObject( next.getObject() );
}
}
}
This method will search the list of objects for types of ObjStm. If it finds
them then it will parse out all of the objects from the stream that is contains. |
protected void finalize() throws IOException {
if( this.warnMissingClose && ( tmpFile != null || scratchFile != null ) )
{
Throwable t = new Throwable( "Warning: You did not close the PDF Document" );
t.printStackTrace();
}
close();
}
Warn the user in the finalizer if he didn't close the PDF document. The method also
closes the document just in case, to avoid abandoned temporary files. It's still a good
idea for the user to close the PDF document at the earliest possible to conserve resources. |
public COSObject getCatalog() throws IOException {
COSObject catalog = getObjectByType( COSName.CATALOG );
if( catalog == null )
{
throw new IOException( "Catalog cannot be found" );
}
return catalog;
}
This will get the document catalog.
Maybe this should move to an object at PDFEdit level |
public COSArray getDocumentID() {
return (COSArray) getTrailer().getItem(COSName.ID);
}
This will get the document ID. |
public COSDictionary getEncryptionDictionary() {
return (COSDictionary)trailer.getDictionaryObject( COSName.ENCRYPT );
}
This will get the encryption dictionary if the document is encrypted or null
if the document is not encrypted. |
public String getHeaderString() {
return headerString;
}
|
public COSObject getObjectByType(String type) throws IOException {
return getObjectByType( COSName.getPDFName( type ) );
}
This will get the first dictionary object by type. |
public COSObject getObjectByType(COSName type) throws IOException {
for( COSObject object : objectPool.values() )
{
COSBase realObject = object.getObject();
if( realObject instanceof COSDictionary )
{
try
{
COSDictionary dic = (COSDictionary)realObject;
COSName objectType = (COSName)dic.getItem( COSName.TYPE );
if( objectType != null && objectType.equals( type ) )
{
return object;
}
}
catch (ClassCastException e)
{
log.warn(e, e);
}
}
}
return null;
}
This will get the first dictionary object by type. |
public COSObject getObjectFromPool(COSObjectKey key) throws IOException {
COSObject obj = null;
if( key != null )
{
obj = (COSObject) objectPool.get(key);
}
if (obj == null)
{
// this was a forward reference, make "proxy" object
obj = new COSObject(null);
if( key != null )
{
obj.setObjectNumber( COSInteger.get( key.getNumber() ) );
obj.setGenerationNumber( COSInteger.get( key.getGeneration() ) );
objectPool.put(key, obj);
}
}
return obj;
}
This will get an object from the pool. |
public List<COSObject> getObjects() {
return new ArrayList< COSObject >(objectPool.values());
}
This will get a list of all available objects. |
public List<COSObject> getObjectsByType(String type) throws IOException {
return getObjectsByType( COSName.getPDFName( type ) );
}
This will get all dictionary objects by type. |
public List<COSObject> getObjectsByType(COSName type) throws IOException {
List< COSObject > retval = new ArrayList< COSObject >();
for( COSObject object : objectPool.values() )
{
COSBase realObject = object.getObject();
if( realObject instanceof COSDictionary )
{
try
{
COSDictionary dic = (COSDictionary)realObject;
COSName objectType = (COSName)dic.getItem( COSName.TYPE );
if( objectType != null && objectType.equals( type ) )
{
retval.add( object );
}
}
catch (ClassCastException e)
{
log.warn(e, e);
}
}
}
return retval;
}
This will get a dictionary object by type. |
public RandomAccess getScratchFile() {
return scratchFile;
}
This will get the scratch file for this document. |
public COSDictionary getTrailer() {
return trailer;
}
This will get the document trailer. |
public float getVersion() {
return version;
}
This will get the version of this PDF document. |
public Map<COSObjectKey, Integer> getXrefTable() {
return xrefTable;
}
Returns the xrefTable which is a mapping of ObjectKeys
to byte offsets in the file. |
public boolean isEncrypted() {
boolean encrypted = false;
if( trailer != null )
{
encrypted = trailer.getDictionaryObject( COSName.ENCRYPT ) != null;
}
return encrypted;
}
This will tell if this is an encrypted document. |
public void parseXrefStreams() throws IOException {
COSDictionary trailerDict = new COSDictionary();
for( COSObject xrefStream : getObjectsByType( "XRef" ) )
{
COSStream stream = (COSStream)xrefStream.getObject();
trailerDict.addAll(stream);
PDFXrefStreamParser parser = new PDFXrefStreamParser(stream, this);
parser.parse();
}
setTrailer( trailerDict );
}
This method will search the list of objects for types of XRef and
uses the parsed data to populate the trailer information as well as
the xref Map. |
public void print() {
for( COSObject object : objectPool.values() )
{
System.out.println( object);
}
}
This will print contents to stdout. |
public void setDocumentID(COSArray id) {
getTrailer().setItem(COSName.ID, id);
}
This will set the document ID. |
public void setEncryptionDictionary(COSDictionary encDictionary) {
trailer.setItem( COSName.ENCRYPT, encDictionary );
}
This will set the encryption dictionary, this should only be called when
encypting the document. |
public void setHeaderString(String header) {
headerString = header;
}
|
public void setTrailer(COSDictionary newTrailer) {
trailer = newTrailer;
}
// MIT added, maybe this should not be supported as trailer is a persistence construct.
This will set the document trailer. |
public void setVersion(float versionValue) {
version = versionValue;
}
This will set the version of this PDF document. |
public void setWarnMissingClose(boolean warn) {
this.warnMissingClose = warn;
}
Controls whether this instance shall issue a warning if the PDF document wasn't closed
properly through a call to the #close() method. If the PDF document is held in
a cache governed by soft references it is impossible to reliably close the document
before the warning is raised. By default, the warning is enabled. |
public void setXRef(COSObjectKey objKey,
int offset) {
xrefTable.put(objKey, offset);
}
Used to populate the XRef HashMap. Will add an Xreftable entry
that maps ObjectKeys to byte offsets in the file. |