📄 cosdocument.java
字号:
/**
* Copyright (c) 2003-2006, www.pdfbox.org
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of pdfbox; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://www.pdfbox.org
*
*/
package org.pdfbox.cos;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.pdfbox.exceptions.COSVisitorException;
import org.pdfbox.io.RandomAccess;
import org.pdfbox.io.RandomAccessFile;
import org.pdfbox.pdfparser.PDFObjectStreamParser;
import org.pdfbox.persistence.util.COSObjectKey;
/**
* This is the in-memory representation of the PDF document. You need to call
* close() on this object when you are done using it!!
*
* @author <a href="ben@benlitchfield.com">Ben Litchfield</a>
* @version $Revision: 1.28 $
*/
public class COSDocument extends COSBase
{
private float version;
/**
* added objects (actually preserving original sequence).
*/
private List objects = new ArrayList();
/**
* a pool of objects read/referenced so far
* used to resolve indirect object references.
*/
private Map objectPool = new HashMap();
/**
* Document trailer dictionary.
*/
private COSDictionary trailer;
/**
* This file will store the streams in order to conserve memory.
*/
private RandomAccess scratchFile = null;
private File tmpFile = null;
private String headerString = "%PDF-1.4";
/**
* Constructor. Uses the java.io.tmpdir value to create a file
* to store the streams.
*
* @throws IOException If there is an error creating the tmp file.
*/
public COSDocument() throws IOException
{
this( new File( System.getProperty( "java.io.tmpdir" ) ) );
}
/**
* Constructor that will create a create a scratch file in the
* following directory.
*
* @param scratchDir The directory to store a scratch file.
*
* @throws IOException If there is an error creating the tmp file.
*/
public COSDocument( File scratchDir ) throws IOException
{
tmpFile = File.createTempFile( "pdfbox", "tmp", scratchDir );
scratchFile = new RandomAccessFile( tmpFile, "rw" );
}
/**
* Constructor that will use the following random access file for storage
* of the PDF streams. The client of this method is responsible for deleting
* the storage if necessary that this file will write to. The close method
* will close the file though.
*
* @param file The random access file to use for storage.
*/
public COSDocument( RandomAccess file )
{
scratchFile = file;
}
/**
* This will get the scratch file for this document.
*
* @return The scratch file.
*/
public RandomAccess getScratchFile()
{
return scratchFile;
}
/**
* This will get the first dictionary object by type.
*
* @param type The type of the object.
*
* @return This will return an object with the specified type.
*/
public COSObject getObjectByType( String type )
{
return getObjectByType( COSName.getPDFName( type ) );
}
/**
* This will get the first dictionary object by type.
*
* @param type The type of the object.
*
* @return This will return an object with the specified type.
*/
public COSObject getObjectByType( COSName type )
{
COSObject retval = null;
Iterator iter = objects.iterator();
while( iter.hasNext() && retval == null)
{
COSObject object = (COSObject)iter.next();
COSBase realObject = object.getObject();
if( realObject instanceof COSDictionary )
{
COSDictionary dic = (COSDictionary)realObject;
COSName objectType = (COSName)dic.getItem( COSName.TYPE );
if( objectType != null && objectType.equals( type ) )
{
retval = object;
}
}
}
return retval;
}
/**
* This will get all dictionary objects by type.
*
* @param type The type of the object.
*
* @return This will return an object with the specified type.
*/
public List getObjectsByType( String type )
{
return getObjectsByType( COSName.getPDFName( type ) );
}
/**
* This will get a dictionary object by type.
*
* @param type The type of the object.
*
* @return This will return an object with the specified type.
*/
public List getObjectsByType( COSName type )
{
List retval = new ArrayList();
Iterator iter = objects.iterator();
while( iter.hasNext() )
{
COSObject object = (COSObject)iter.next();
COSBase realObject = object.getObject();
if( realObject instanceof COSDictionary )
{
COSDictionary dic = (COSDictionary)realObject;
COSName objectType = (COSName)dic.getItem( COSName.TYPE );
if( objectType != null && objectType.equals( type ) )
{
retval.add( object );
}
}
}
return retval;
}
/**
* This will print contents to stdout.
*/
public void print()
{
Iterator iter = objects.iterator();
while( iter.hasNext() )
{
COSObject object = (COSObject)iter.next();
System.out.println( object);
}
}
/**
* This will set the version of this PDF document.
*
* @param versionValue The version of the PDF document.
*/
public void setVersion( float versionValue )
{
version = versionValue;
}
/**
* This will get the version of this PDF document.
*
* @return This documents version.
*/
public float getVersion()
{
return version;
}
/**
* This will tell if this is an encrypted document.
*
* @return true If this document is encrypted.
*/
public boolean isEncrypted()
{
boolean encrypted = false;
if( trailer != null )
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -