A PDStream represents a stream in a PDF document. Streams are tied to a single
PDF document.
Constructor: |
protected PDStream() {
//should only be called by PDMemoryStream
}
This will create a new PDStream object. |
public PDStream(PDDocument document) {
stream = new COSStream( document.getDocument().getScratchFile() );
}
This will create a new PDStream object. Parameters:
document - The document that the stream will be part of.
|
public PDStream(COSStream str) {
stream = str;
}
Parameters:
str - The stream parameter.
|
public PDStream(PDDocument doc,
InputStream str) throws IOException {
this( doc, str, false );
}
Constructor. Reads all data from the input stream and embeds it into the
document, this will close the InputStream. Parameters:
doc - The document that will hold the stream.
str - The stream parameter.
Throws:
IOException - If there is an error creating the stream in the document.
|
public PDStream(PDDocument doc,
InputStream str,
boolean filtered) throws IOException {
OutputStream output = null;
try
{
stream = new COSStream( doc.getDocument().getScratchFile() );
if( filtered )
{
output = stream.createFilteredStream();
}
else
{
output = stream.createUnfilteredStream();
}
byte[] buffer = new byte[ 1024 ];
int amountRead = -1;
while( (amountRead = str.read(buffer)) != -1 )
{
output.write( buffer, 0, amountRead );
}
}
finally
{
if( output != null )
{
output.close();
}
if( str != null )
{
str.close();
}
}
}
Constructor. Reads all data from the input stream and embeds it into the
document, this will close the InputStream. Parameters:
doc - The document that will hold the stream.
str - The stream parameter.
filtered - True if the stream already has a filter applied.
Throws:
IOException - If there is an error creating the stream in the document.
|
Method from org.apache.pdfbox.pdmodel.common.PDStream Detail: |
public void addCompression() {
List filters = getFilters();
if( filters == null )
{
filters = new ArrayList();
filters.add( COSName.FLATE_DECODE );
setFilters( filters );
}
}
If there are not compression filters on the current stream then this
will add a compression filter, flate compression for example. |
public static PDStream createFromCOS(COSBase base) throws IOException {
PDStream retval = null;
if( base instanceof COSStream )
{
retval = new PDStream( (COSStream)base );
}
else if( base instanceof COSArray )
{
retval = new PDStream( new COSStreamArray( (COSArray)base ) );
}
else
{
if( base != null )
{
throw new IOException( "Contents are unknown type:" + base.getClass().getName() );
}
}
return retval;
}
Create a pd stream from either a regular COSStream on a COSArray of cos streams. |
public InputStream createInputStream() throws IOException {
return stream.getUnfilteredStream();
}
This will get a stream that can be read from. |
public OutputStream createOutputStream() throws IOException {
return stream.createUnfilteredStream();
}
This will get a stream that can be written to. |
public byte[] getByteArray() throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
InputStream is = null;
try
{
is = createInputStream();
int amountRead = -1;
while( (amountRead = is.read( buf )) != -1)
{
output.write( buf, 0, amountRead );
}
}
finally
{
if( is != null )
{
is.close();
}
}
return output.toByteArray();
}
This will copy the stream into a byte array. |
public COSBase getCOSObject() {
return stream;
}
Convert this standard java object to a COS object. |
public List getDecodeParms() throws IOException {
List retval = null;
COSBase dp = stream.getDictionaryObject( COSName.DECODE_PARMS );
if( dp == null )
{
//See PDF Ref 1.5 implementation note 7, the DP is sometimes used instead.
dp = stream.getDictionaryObject( COSName.DP );
}
if( dp instanceof COSDictionary )
{
Map map = COSDictionaryMap.convertBasicTypesToMap( (COSDictionary)dp );
retval = new COSArrayList(map, dp, stream, COSName.DECODE_PARMS );
}
else if( dp instanceof COSArray )
{
COSArray array = (COSArray)dp;
List actuals = new ArrayList();
for( int i=0; i< array.size(); i++ )
{
actuals.add(
COSDictionaryMap.convertBasicTypesToMap(
(COSDictionary)array.getObject( i ) ) );
}
retval = new COSArrayList(actuals, array);
}
return retval;
}
Get the list of decode parameters. Each entry in the list will refer to
an entry in the filters list. |
public int getDecodedStreamLength() {
return this.stream.getInt(COSName.DL);
}
Get the decoded stream length. |
public PDFileSpecification getFile() throws IOException {
COSBase f = stream.getDictionaryObject( COSName.F );
PDFileSpecification retval = PDFileSpecification.createFS( f );
return retval;
}
This will get the file specification for this stream. This is only
required for external files. |
public List getFileDecodeParams() throws IOException {
List retval = null;
COSBase dp = stream.getDictionaryObject( COSName.F_DECODE_PARMS );
if( dp instanceof COSDictionary )
{
Map map = COSDictionaryMap.convertBasicTypesToMap( (COSDictionary)dp );
retval = new COSArrayList(map, dp, stream, COSName.F_DECODE_PARMS );
}
else if( dp instanceof COSArray )
{
COSArray array = (COSArray)dp;
List actuals = new ArrayList();
for( int i=0; i< array.size(); i++ )
{
actuals.add(
COSDictionaryMap.convertBasicTypesToMap(
(COSDictionary)array.getObject( i ) ) );
}
retval = new COSArrayList(actuals, array);
}
return retval;
}
Get the list of decode parameters. Each entry in the list will refer to
an entry in the filters list. |
public List getFileFilters() {
List retval = null;
COSBase filters = stream.getDictionaryObject( COSName.F_FILTER );
if( filters instanceof COSName )
{
COSName name = (COSName)filters;
retval = new COSArrayList( name.getName(), name, stream, COSName.F_FILTER );
}
else if( filters instanceof COSArray )
{
retval = COSArrayList.convertCOSNameCOSArrayToList( (COSArray)filters );
}
return retval;
}
This will get the list of filters that are associated with this stream. Or
null if there are none. |
public List getFilters() {
List retval = null;
COSBase filters = stream.getFilters();
if( filters instanceof COSName )
{
COSName name = (COSName)filters;
retval = new COSArrayList( name.getName(), name, stream, COSName.FILTER );
}
else if( filters instanceof COSArray )
{
retval = COSArrayList.convertCOSNameCOSArrayToList( (COSArray)filters );
}
return retval;
}
This will get the list of filters that are associated with this stream. Or
null if there are none. |
public String getInputStreamAsString() throws IOException {
byte[] bStream = getByteArray();
return new String(bStream);
}
A convenience method to get this stream as a string. Uses
the default system encoding. |
public int getLength() {
return stream.getInt( "Length", 0 );
}
This will get the length of the filtered/compressed stream. This is readonly in the
PD Model and will be managed by this class. |
public PDMetadata getMetadata() {
PDMetadata retval = null;
COSStream mdStream = (COSStream)stream.getDictionaryObject( COSName.METADATA );
if( mdStream != null )
{
retval = new PDMetadata( mdStream );
}
return retval;
}
Get the metadata that is part of the document catalog. This will
return null if there is no meta data for this object. |
public InputStream getPartiallyFilteredStream(List stopFilters) throws IOException {
FilterManager manager = stream.getFilterManager();
InputStream is = stream.getFilteredStream();
ByteArrayOutputStream os = new ByteArrayOutputStream();
List filters = getFilters();
String nextFilter = null;
boolean done = false;
for( int i=0; i< filters.size() && !done; i++ )
{
os.reset();
nextFilter = (String)filters.get( i );
if( stopFilters.contains( nextFilter ) )
{
done = true;
}
else
{
Filter filter = manager.getFilter( COSName.getPDFName(nextFilter) );
filter.decode( is, os, stream, i );
is = new ByteArrayInputStream( os.toByteArray() );
}
}
return is;
}
This will get a stream with some filters applied but not others. This is useful
when doing images, ie filters = [flate,dct], we want to remove flate but leave dct |
public COSStream getStream() {
return stream;
}
Get the cos stream associated with this object. |
public void setDecodeParms(List decodeParams) {
stream.setItem(
COSName.DECODE_PARMS, COSArrayList.converterToCOSArray( decodeParams ) );
}
This will set the list of decode parameterss. |
public void setDecodedStreamLength(int decodedStreamLength) {
this.stream.setInt(COSName.DL, decodedStreamLength);
}
Set the decoded stream length. |
public void setFile(PDFileSpecification f) {
stream.setItem( COSName.F, f );
}
Set the file specification. |
public void setFileDecodeParams(List decodeParams) {
stream.setItem(
"FDecodeParams", COSArrayList.converterToCOSArray( decodeParams ) );
}
This will set the list of decode params. |
public void setFileFilters(List filters) {
COSBase obj = COSArrayList.convertStringListToCOSNameCOSArray( filters );
stream.setItem( COSName.F_FILTER, obj );
}
This will set the filters that are part of this stream. |
public void setFilters(List filters) {
COSBase obj = COSArrayList.convertStringListToCOSNameCOSArray( filters );
stream.setItem( COSName.FILTER, obj );
}
This will set the filters that are part of this stream. |
public void setMetadata(PDMetadata meta) {
stream.setItem( COSName.METADATA, meta );
}
Set the metadata for this object. This can be null. |