From 6025b6016517c6d898d8957d1d7e03ba71431912 Mon Sep 17 00:00:00 2001 From: tknall Date: Fri, 1 Dec 2006 12:20:24 +0000 Subject: Initial import of release 2.2. git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@4 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- src/main/java/org/pdfbox/cos/COSArray.java | 492 ++++++++++ src/main/java/org/pdfbox/cos/COSBase.java | 86 ++ src/main/java/org/pdfbox/cos/COSBoolean.java | 161 ++++ src/main/java/org/pdfbox/cos/COSDictionary.java | 1167 +++++++++++++++++++++++ src/main/java/org/pdfbox/cos/COSDocument.java | 518 ++++++++++ src/main/java/org/pdfbox/cos/COSFloat.java | 173 ++++ src/main/java/org/pdfbox/cos/COSInteger.java | 190 ++++ src/main/java/org/pdfbox/cos/COSName.java | 572 +++++++++++ src/main/java/org/pdfbox/cos/COSNull.java | 88 ++ src/main/java/org/pdfbox/cos/COSNumber.java | 115 +++ src/main/java/org/pdfbox/cos/COSObject.java | 226 +++++ src/main/java/org/pdfbox/cos/COSStream.java | 495 ++++++++++ src/main/java/org/pdfbox/cos/COSString.java | 403 ++++++++ src/main/java/org/pdfbox/cos/ICOSVisitor.java | 132 +++ src/main/java/org/pdfbox/cos/package.html | 12 + 15 files changed, 4830 insertions(+) create mode 100644 src/main/java/org/pdfbox/cos/COSArray.java create mode 100644 src/main/java/org/pdfbox/cos/COSBase.java create mode 100644 src/main/java/org/pdfbox/cos/COSBoolean.java create mode 100644 src/main/java/org/pdfbox/cos/COSDictionary.java create mode 100644 src/main/java/org/pdfbox/cos/COSDocument.java create mode 100644 src/main/java/org/pdfbox/cos/COSFloat.java create mode 100644 src/main/java/org/pdfbox/cos/COSInteger.java create mode 100644 src/main/java/org/pdfbox/cos/COSName.java create mode 100644 src/main/java/org/pdfbox/cos/COSNull.java create mode 100644 src/main/java/org/pdfbox/cos/COSNumber.java create mode 100644 src/main/java/org/pdfbox/cos/COSObject.java create mode 100644 src/main/java/org/pdfbox/cos/COSStream.java create mode 100644 src/main/java/org/pdfbox/cos/COSString.java create mode 100644 src/main/java/org/pdfbox/cos/ICOSVisitor.java create mode 100644 src/main/java/org/pdfbox/cos/package.html (limited to 'src/main/java/org/pdfbox/cos') diff --git a/src/main/java/org/pdfbox/cos/COSArray.java b/src/main/java/org/pdfbox/cos/COSArray.java new file mode 100644 index 0000000..cb7d278 --- /dev/null +++ b/src/main/java/org/pdfbox/cos/COSArray.java @@ -0,0 +1,492 @@ +/** + * Copyright (c) 2003-2005, 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.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + + + +import org.pdfbox.exceptions.COSVisitorException; +import org.pdfbox.pdmodel.common.COSObjectable; + +/** + * An array of PDFBase objects as part of the PDF document. + * + * @author Ben Litchfield (ben@benlitchfield.com) + * @version $Revision: 1.22 $ + */ +public class COSArray extends COSBase +{ + private List objects = new ArrayList(); + + /** + * Constructor. + */ + public COSArray() + { + //default constructor + } + + /** + * This will add an object to the array. + * + * @param object The object to add to the array. + */ + public void add( COSBase object ) + { + objects.add( object ); + } + + /** + * This will add an object to the array. + * + * @param object The object to add to the array. + */ + public void add( COSObjectable object ) + { + objects.add( object.getCOSObject() ); + } + + /** + * Add the specified object at the ith location and push the rest to the + * right. + * + * @param i The index to add at. + * @param object The object to add at that index. + */ + public void add( int i, COSBase object) + { + objects.add( i, object ); + } + + /** + * This will remove all of the objects in the collection. + */ + public void clear() + { + objects.clear(); + } + + /** + * This will remove all of the objects in the collection. + * + * @param objectsList The list of objects to remove from the collection. + */ + public void removeAll( Collection objectsList ) + { + objects.removeAll( objectsList ); + } + + /** + * This will retain all of the objects in the collection. + * + * @param objectsList The list of objects to retain from the collection. + */ + public void retainAll( Collection objectsList ) + { + objects.retainAll( objectsList ); + } + + /** + * This will add an object to the array. + * + * @param objectsList The object to add to the array. + */ + public void addAll( Collection objectsList ) + { + objects.addAll( objectsList ); + } + + /** + * This will add all objects to this array. + * + * @param objectList The objects to add. + */ + public void addAll( COSArray objectList ) + { + objects.addAll( objectList.objects ); + } + + /** + * Add the specified object at the ith location and push the rest to the + * right. + * + * @param i The index to add at. + * @param objectList The object to add at that index. + */ + public void addAll( int i, Collection objectList ) + { + objects.addAll( i, objectList ); + } + + /** + * This will set an object at a specific index. + * + * @param index zero based index into array. + * @param object The object to set. + */ + public void set( int index, COSBase object ) + { + objects.set( index, object ); + } + + /** + * This will set an object at a specific index. + * + * @param index zero based index into array. + * @param intVal The object to set. + */ + public void set( int index, int intVal ) + { + objects.set( index, new COSInteger( intVal ) ); + } + + /** + * This will set an object at a specific index. + * + * @param index zero based index into array. + * @param object The object to set. + */ + public void set( int index, COSObjectable object ) + { + COSBase base = null; + if( object != null ) + { + base = object.getCOSObject(); + } + objects.set( index, base ); + } + + /** + * This will get an object from the array. This will dereference the object. + * If the object is COSNull then null will be returned. + * + * @param index The index into the array to get the object. + * + * @return The object at the requested index. + */ + public COSBase getObject( int index ) + { + Object obj = objects.get( index ); + if( obj instanceof COSObject ) + { + obj = ((COSObject)obj).getObject(); + } + if( obj instanceof COSNull ) + { + obj = null; + } + return (COSBase)obj; + } + + /** + * This will get an object from the array. This will NOT derefernce + * the COS object. + * + * @param index The index into the array to get the object. + * + * @return The object at the requested index. + */ + public COSBase get( int index ) + { + return (COSBase)objects.get( index ); + } + + /** + * Get the value of the array as an integer. + * + * @param index The index into the list. + * + * @return The value at that index or -1 if it is null. + */ + public int getInt( int index ) + { + return getInt( index, -1 ); + } + + /** + * Get the value of the array as an integer, return the default if it does + * not exist. + * + * @param index The value of the array. + * @param defaultValue The value to return if the value is null. + * @return The value at the index or the defaultValue. + */ + public int getInt( int index, int defaultValue ) + { + int retval = defaultValue; + if( defaultValue < size() ) + { + COSNumber number = (COSNumber)get( index ); + if( number != null ) + { + retval = number.intValue(); + } + } + return retval; + } + + /** + * Set the value in the array as an integer. + * + * @param index The index into the array. + * @param value The value to set. + */ + public void setInt( int index, int value ) + { + set( index, new COSInteger( value ) ); + } + + /** + * Set the value in the array as a name. + * @param index The index into the array. + * @param name The name to set in the array. + */ + public void setName( int index, String name ) + { + set( index, COSName.getPDFName( name ) ); + } + + /** + * Get the value of the array as a string. + * + * @param index The index into the array. + * @return The name converted to a string or null if it does not exist. + */ + public String getName( int index ) + { + return getName( index, null ); + } + + /** + * Get an entry in the array that is expected to be a COSName. + * @param index The index into the array. + * @param defaultValue The value to return if it is null. + * @return The value at the index or defaultValue if none is found. + */ + public String getName( int index, String defaultValue ) + { + String retval = defaultValue; + if( index < size() ) + { + COSName name = (COSName)get( index ); + if( name != null ) + { + retval = name.getName(); + } + } + return retval; + } + + /** + * Set the value in the array as a string. + * @param index The index into the array. + * @param string The string to set in the array. + */ + public void setString( int index, String string ) + { + set( index, new COSString( string ) ); + } + + /** + * Get the value of the array as a string. + * + * @param index The index into the array. + * @return The string or null if it does not exist. + */ + public String getString( int index ) + { + return getString( index, null ); + } + + /** + * Get an entry in the array that is expected to be a COSName. + * @param index The index into the array. + * @param defaultValue The value to return if it is null. + * @return The value at the index or defaultValue if none is found. + */ + public String getString( int index, String defaultValue ) + { + String retval = defaultValue; + if( index < size() ) + { + COSString string = (COSString)get( index ); + if( string != null ) + { + retval = string.getString(); + } + } + return retval; + } + + /** + * This will get the size of this array. + * + * @return The number of elements in the array. + */ + public int size() + { + return objects.size(); + } + + /** + * This will remove an element from the array. + * + * @param i The index of the object to remove. + * + * @return The object that was removed. + */ + public COSBase remove( int i ) + { + return (COSBase)objects.remove( i ); + } + + /** + * This will remove an element from the array. + * + * @param o The object to remove. + * + * @return The object that was removed. + */ + public boolean remove( COSBase o ) + { + return objects.remove( o ); + } + + /** + * @see Object#toString() + */ + public String toString() + { + return "COSArray{" + objects + "}"; + } + + /** + * Get access to the list. + * + * @return an iterator over the array elements + */ + public Iterator iterator() + { + return objects.iterator(); + } + + /** + * This will return the index of the entry or -1 if it is not found. + * + * @param object The object to search for. + * @return The index of the object or -1. + */ + public int indexOf( COSBase object ) + { + int retval = -1; + for( int i=0; retval < 0 && i> 32)); + } + + /** + * @see Object#toString() + */ + public String toString() + { + return "COSInt{" + value + "}"; + } + + /** + * Change the value of this reference. + * + * @param newValue The new value. + */ + public void setValue( long newValue ) + { + value = newValue; + } + + + + /** + * polymorphic access to value as float. + * + * @return The float value of this object. + */ + public float floatValue() + { + return (float)value; + } + + /** + * polymorphic access to value as float. + * + * @return The double value of this object. + */ + public double doubleValue() + { + return (double)value; + } + + /** + * Polymorphic access to value as int + * This will get the integer value of this object. + * + * @return The int value of this object, + */ + public int intValue() + { + return (int)value; + } + + /** + * Polymorphic access to value as int + * This will get the integer value of this object. + * + * @return The int value of this object, + */ + public long longValue() + { + return value; + } + + /** + * visitor pattern double dispatch method. + * + * @param visitor The object to notify when visiting this object. + * @return any object, depending on the visitor implementation, or null + * @throws COSVisitorException If an error occurs while visiting this object. + */ + public Object accept(ICOSVisitor visitor) throws COSVisitorException + { + return visitor.visitFromInt(this); + } + + /** + * This will output this string as a PDF object. + * + * @param output The stream to write to. + * @throws IOException If there is an error writing to the stream. + */ + public void writePDF( OutputStream output ) throws IOException + { + output.write(String.valueOf(value).getBytes()); + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/cos/COSName.java b/src/main/java/org/pdfbox/cos/COSName.java new file mode 100644 index 0000000..8532cc4 --- /dev/null +++ b/src/main/java/org/pdfbox/cos/COSName.java @@ -0,0 +1,572 @@ +/** + * Copyright (c) 2003-2005, 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.IOException; +import java.io.OutputStream; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import org.pdfbox.exceptions.COSVisitorException; +import org.pdfbox.persistence.util.COSHEXTable; + + +/** + * This class represents a PDF named object. + * + * @author Ben Litchfield (ben@benlitchfield.com) + * @version $Revision: 1.38 $ + */ +public final class COSName extends COSBase implements Comparable +{ + /** + * Note: This is synchronized because a HashMap must be synchronized if accessed by + * multiple threads. + */ + private static Map nameMap = Collections.synchronizedMap( new HashMap(8192) ); + + + /** + * A common COSName value. + */ + public static final COSName AA = new COSName( "AA" ); + /** + * A common COSName value. + */ + public static final COSName ACRO_FORM = new COSName( "AcroForm" ); + /** + * A common COSName value. + */ + public static final COSName ANNOTS = new COSName( "Annots" ); + /** + * A common COSName value. + */ + public static final COSName ART_BOX = new COSName("ArtBox" ); + /** + * A common COSName value. + */ + public static final COSName ASCII85_DECODE = new COSName( "ASCII85Decode" ); + /** + * A common COSName value. + */ + public static final COSName ASCII85_DECODE_ABBREVIATION = new COSName( "A85" ); + /** + * A common COSName value. + */ + public static final COSName ASCII_HEX_DECODE = new COSName( "ASCIIHexDecode" ); + /** + * A common COSName value. + */ + public static final COSName ASCII_HEX_DECODE_ABBREVIATION = new COSName( "AHx" ); + /** + * A common COSName value. + */ + public static final COSName AP = new COSName( "AP" ); + /** + * A common COSName value. + */ + public static final COSName B = new COSName( "B" ); + /** + * A common COSName value. + */ + public static final COSName BASE_ENCODING = new COSName( "BaseEncoding" ); + /** + * A common COSName value. + */ + public static final COSName BASE_FONT = new COSName( "BaseFont" ); + /** + * A common COSName value. + */ + public static final COSName BBOX = new COSName( "BBox" ); + /** + * A common COSName value. + */ + public static final COSName BLEED_BOX = new COSName("BleedBox" ); + /** + * A common COSName value. + */ + public static final COSName CATALOG = new COSName( "Catalog" ); + /** + * A common COSName value. + */ + public static final COSName CALGRAY = new COSName( "CalGray" ); + /** + * A common COSName value. + */ + public static final COSName CALRGB = new COSName( "CalRGB" ); + /** + * A common COSName value. + */ + public static final COSName CCITTFAX_DECODE = new COSName( "CCITTFaxDecode" ); + /** + * A common COSName value. + */ + public static final COSName CCITTFAX_DECODE_ABBREVIATION = new COSName( "CCF" ); + /** + * A common COSName value. + */ + public static final COSName COLORSPACE = new COSName( "ColorSpace" ); + /** + * A common COSName value. + */ + public static final COSName CONTENTS = new COSName( "Contents" ); + /** + * A common COSName value. + */ + public static final COSName COUNT = new COSName( "Count" ); + /** + * A common COSName value. + */ + public static final COSName CROP_BOX = new COSName( "CropBox" ); + /** + * A common COSName value. + */ + public static final COSName DESCENDANT_FONTS = new COSName( "DescendantFonts" ); + /** + * A common COSName value. + */ + public static final COSName DIFFERENCES = new COSName( "Differences" ); + /** + * A common COSName value. + */ + public static final COSName DCT_DECODE = new COSName( "DCTDecode" ); + /** + * A common COSName value. + */ + public static final COSName DCT_DECODE_ABBREVIATION = new COSName( "DCT" ); + /** + * A common COSName value. + */ + public static final COSName DEVICECMYK = new COSName( "DeviceCMYK" ); + /** + * A common COSName value. + */ + public static final COSName DEVICEGRAY = new COSName( "DeviceGray" ); + /** + * A common COSName value. + */ + public static final COSName DEVICEN = new COSName( "DeviceN" ); + /** + * A common COSName value. + */ + public static final COSName DEVICERGB = new COSName( "DeviceRGB" ); + /** + * A common COSName value. + */ + public static final COSName DV = new COSName( "DV" ); + /** + * A common COSName value. + */ + public static final COSName ENCODING = new COSName( "Encoding" ); + /** + * A common COSName value. + */ + public static final COSName FIELDS = new COSName( "Fields" ); + /** + * A common COSName value. + */ + public static final COSName FILTER = new COSName( "Filter" ); + /** + * A common COSName value. + */ + public static final COSName FIRST_CHAR = new COSName( "FirstChar" ); + /** + * A common COSName value. + */ + public static final COSName FLATE_DECODE = new COSName( "FlateDecode" ); + /** + * A common COSName value. + */ + public static final COSName FLATE_DECODE_ABBREVIATION = new COSName( "Fl" ); + /** + * A common COSName value. + */ + public static final COSName FONT = new COSName( "Font" ); + /** + * A common COSName value. + */ + public static final COSName FONT_FILE = new COSName("FontFile"); + /** + * A common COSName value. + */ + public static final COSName FONT_FILE2 = new COSName("FontFile2"); + /** + * A common COSName value. + */ + public static final COSName FONT_FILE3 = new COSName("FontFile3"); + /** + * A common COSName value. + */ + public static final COSName FONT_DESC = new COSName("FontDescriptor"); + /** + * A common COSName value. + */ + public static final COSName FONT_MATRIX = new COSName("FontMatrix" ); + /** + * A common COSName value. + */ + public static final COSName FORMTYPE = new COSName( "FormType" ); + /** + * A common COSName value. + */ + public static final COSName FRM = new COSName( "FRM" ); + /** + * A common COSName value. + */ + public static final COSName HEIGHT = new COSName( "Height" ); + /** + * A common COSName value. + */ + public static final COSName ICCBASED = new COSName( "ICCBased" ); + /** + * A common COSName value. + */ + public static final COSName IDENTITY_H = new COSName( "Identity-H" ); + /** + * A common COSName value. + */ + public static final COSName IMAGE = new COSName( "Image" ); + /** + * A common COSName value. + */ + public static final COSName INDEXED = new COSName( "Indexed" ); + /** + * A common COSName value. + */ + public static final COSName INFO = new COSName( "Info" ); + /** + * A common COSName value. + */ + public static final COSName JPX_DECODE = new COSName( "JPXDecode" ); + /** + * A common COSName value. + */ + public static final COSName KIDS = new COSName( "Kids" ); + /** + * A common COSName value. + */ + public static final COSName LAB = new COSName( "Lab" ); + /** + * A common COSName value. + */ + public static final COSName LAST_CHAR = new COSName( "LastChar" ); + /** + * A common COSName value. + */ + public static final COSName LENGTH = new COSName( "Length" ); + /** + * A common COSName value. + */ + public static final COSName LENGTH1 = new COSName( "Length1" ); + /** + * A common COSName value. + */ + public static final COSName LZW_DECODE = new COSName( "LZWDecode" ); + /** + * A common COSName value. + */ + public static final COSName LZW_DECODE_ABBREVIATION = new COSName( "LZW" ); + /** + * A common COSName value. + */ + public static final COSName MAC_ROMAN_ENCODING = new COSName( "MacRomanEncoding" ); + /** + * A common COSName value. + */ + public static final COSName MATRIX = new COSName( "Matrix" ); + /** + * A common COSName value. + */ + public static final COSName MEDIA_BOX = new COSName( "MediaBox" ); + /** + * A common COSName value. + */ + public static final COSName METADATA = new COSName( "Metadata" ); + /** + * A common COSName value. + */ + public static final COSName N = new COSName( "N" ); + /** + * A common COSName value. + */ + public static final COSName NAME = new COSName( "Name" ); + /** + * A common COSName value. + */ + public static final COSName P = new COSName( "P" ); + /** + * A common COSName value. + */ + public static final COSName PAGE = new COSName( "Page" ); + /** + * A common COSName value. + */ + public static final COSName PAGES = new COSName( "Pages" ); + /** + * A common COSName value. + */ + public static final COSName PARENT = new COSName( "Parent" ); + /** + * A common COSName value. + */ + public static final COSName PATTERN = new COSName( "Pattern" ); + /** + * A common COSName value. + */ + public static final COSName PDF_DOC_ENCODING = new COSName( "PDFDocEncoding" ); + /** + * A common COSName value. + */ + public static final COSName PREV = new COSName( "Prev" ); + /** + * A common COSName value. + */ + public static final COSName R = new COSName( "R" ); + /** + * A common COSName value. + */ + public static final COSName RESOURCES = new COSName( "Resources" ); + /** + * A common COSName value. + */ + public static final COSName ROOT = new COSName( "Root" ); + /** + * A common COSName value. + */ + public static final COSName ROTATE = new COSName( "Rotate" ); + /** + * A common COSName value. + */ + public static final COSName RUN_LENGTH_DECODE = new COSName( "RunLengthDecode" ); + /** + * A common COSName value. + */ + public static final COSName RUN_LENGTH_DECODE_ABBREVIATION = new COSName( "RL" ); + /** + * A common COSName value. + */ + public static final COSName SEPARATION = new COSName( "Separation" ); + /** + * A common COSName value. + */ + public static final COSName STANDARD_ENCODING = new COSName( "StandardEncoding" ); + /** + * A common COSName value. + */ + public static final COSName SUBTYPE = new COSName( "Subtype" ); + /** + * A common COSName value. + */ + public static final COSName TRIM_BOX = new COSName("TrimBox" ); + /** + * A common COSName value. + */ + public static final COSName TRUE_TYPE = new COSName("TrueType" ); + /** + * A common COSName value. + */ + public static final COSName TO_UNICODE = new COSName( "ToUnicode" ); + /** + * A common COSName value. + */ + public static final COSName TYPE = new COSName( "Type" ); + /** + * A common COSName value. + */ + public static final COSName TYPE0 = new COSName( "Type0" ); + /** + * A common COSName value. + */ + public static final COSName V = new COSName( "V" ); + /** + * A common COSName value. + */ + public static final COSName VERSION = new COSName( "Version" ); + /** + * A common COSName value. + */ + public static final COSName WIDTHS = new COSName( "Widths" ); + /** + * A common COSName value. + */ + public static final COSName WIN_ANSI_ENCODING = new COSName( "WinAnsiEncoding" ); + /** + * A common COSName value. + */ + public static final COSName XOBJECT = new COSName( "XObject" ); + + /** + * The prefix to a PDF name. + */ + public static final byte[] NAME_PREFIX = new byte[] { 47 }; // The / character + /** + * The escape character for a name. + */ + public static final byte[] NAME_ESCAPE = new byte[] { 35 }; //The # character + + private String name; + private int hashCode; + + /** + * This will get a COSName object with that name. + * + * @param aName The name of the object. + * + * @return A COSName with the specified name. + */ + public static final COSName getPDFName( String aName ) + { + COSName name = null; + if( aName != null ) + { + name = (COSName)nameMap.get( aName ); + if( name == null ) + { + //name is added to map in the constructor + name = new COSName( aName ); + } + } + return name; + } + + /** + * Private constructor. This will limit the number of COSName objects. + * that are created. + * + * @param aName The name of the COSName object. + */ + private COSName( String aName ) + { + name = aName; + nameMap.put( aName, this ); + hashCode = name.hashCode(); + } + + /** + * This will get the name of this COSName object. + * + * @return The name of the object. + */ + public String getName() + { + return name; + } + + /** + * @see Object#toString() + */ + public String toString() + { + return "COSName{" + name + "}"; + } + + /** + * @see Object#equals( Object ) + */ + public boolean equals( Object o ) + { + boolean retval = this == o; + if( !retval && o instanceof COSName ) + { + COSName other = (COSName)o; + retval = name == other.name || name.equals( other.name ); + } + return retval; + } + + /** + * @see Object#hashCode() + */ + public int hashCode() + { + return hashCode; + } + + /** + * @see Comparable#compareTo( Object ) + */ + public int compareTo(Object o) + { + COSName other = (COSName)o; + return this.name.compareTo( other.name ); + } + + + + /** + * visitor pattern double dispatch method. + * + * @param visitor The object to notify when visiting this object. + * @return any object, depending on the visitor implementation, or null + * @throws COSVisitorException If an error occurs while visiting this object. + */ + public Object accept(ICOSVisitor visitor) throws COSVisitorException + { + return visitor.visitFromName(this); + } + + /** + * This will output this string as a PDF object. + * + * @param output The stream to write to. + * @throws IOException If there is an error writing to the stream. + */ + public void writePDF( OutputStream output ) throws IOException + { + output.write(NAME_PREFIX); + byte[] bytes = getName().getBytes(); + for (int i = 0; i < bytes.length;i++) + { + int current = ((bytes[i]+256)%256); + + if(current <= 32 || current >= 127 || + current == '(' || + current == ')' || + current == '[' || + current == ']' || + current == '/' || + current == '%' || + current == '<' || + current == '>' || + current == NAME_ESCAPE[0] ) + { + output.write(NAME_ESCAPE); + output.write(COSHEXTable.TABLE[current]); + } + else + { + output.write(current); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/cos/COSNull.java b/src/main/java/org/pdfbox/cos/COSNull.java new file mode 100644 index 0000000..15356b5 --- /dev/null +++ b/src/main/java/org/pdfbox/cos/COSNull.java @@ -0,0 +1,88 @@ +/** + * Copyright (c) 2003-2004, 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.IOException; +import java.io.OutputStream; + +import org.pdfbox.exceptions.COSVisitorException; + +/** + * This class represents a null PDF object. + * + * @author Ben Litchfield (ben@csh.rit.edu) + * @version $Revision: 1.12 $ + */ +public class COSNull extends COSBase +{ + /** + * The null token. + */ + public static final byte[] NULL_BYTES = new byte[] {110, 117, 108, 108}; //"null".getBytes( "ISO-8859-1" ); + + /** + * The one null object in the system. + */ + public static final COSNull NULL = new COSNull(); + + /** + * Constructor. + */ + private COSNull() + { + //limit creation to one instance. + } + + /** + * visitor pattern double dispatch method. + * + * @param visitor The object to notify when visiting this object. + * @return any object, depending on the visitor implementation, or null + * @throws COSVisitorException If an error occurs while visiting this object. + */ + public Object accept( ICOSVisitor visitor ) throws COSVisitorException + { + return visitor.visitFromNull( this ); + } + + /** + * This will output this string as a PDF object. + * + * @param output The stream to write to. + * @throws IOException If there is an error writing to the stream. + */ + public void writePDF( OutputStream output ) throws IOException + { + output.write(NULL_BYTES); + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/cos/COSNumber.java b/src/main/java/org/pdfbox/cos/COSNumber.java new file mode 100644 index 0000000..c629f66 --- /dev/null +++ b/src/main/java/org/pdfbox/cos/COSNumber.java @@ -0,0 +1,115 @@ +/** + * Copyright (c) 2003, 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.IOException; + +import java.util.HashMap; +import java.util.Map; + +/** + * This class represents an abstract number in a PDF document. + * + * @author Ben Litchfield (ben@csh.rit.edu) + * @version $Revision: 1.9 $ + */ +public abstract class COSNumber extends COSBase +{ + /** + * ZERO. + */ + public static final COSInteger ZERO = new COSInteger( 0 ); + /** + * ONE. + */ + public static final COSInteger ONE = new COSInteger( 1 ); + private static final Map COMMON_NUMBERS = new HashMap(); + + static + { + COMMON_NUMBERS.put( "0", ZERO ); + COMMON_NUMBERS.put( "1", ONE ); + } + + /** + * This will get the float value of this number. + * + * @return The float value of this object. + */ + public abstract float floatValue(); + + /** + * This will get the double value of this number. + * + * @return The double value of this number. + */ + public abstract double doubleValue(); + + /** + * This will get the integer value of this number. + * + * @return The integer value of this number. + */ + public abstract int intValue(); + + /** + * This will get the long value of this number. + * + * @return The long value of this number. + */ + public abstract long longValue(); + + /** + * This factory method will get the appropriate number object. + * + * @param number The string representation of the number. + * + * @return A number object, either float or int. + * + * @throws IOException If the string is not a number. + */ + public static COSNumber get( String number ) throws IOException + { + COSNumber result = (COSNumber)COMMON_NUMBERS.get( number ); + if( result == null ) + { + if (number.indexOf('.') >= 0) + { + result = new COSFloat( number ); + } + else + { + result = new COSInteger( number ); + } + } + return result; + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/cos/COSObject.java b/src/main/java/org/pdfbox/cos/COSObject.java new file mode 100644 index 0000000..28f2316 --- /dev/null +++ b/src/main/java/org/pdfbox/cos/COSObject.java @@ -0,0 +1,226 @@ +/** + * Copyright (c) 2003, 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 org.pdfbox.exceptions.COSVisitorException; + +import java.io.IOException; + +import org.apache.log4j.Logger; + +/** + * This class represents a PDF object. + * + * @author Ben Litchfield (ben@csh.rit.edu) + * @version $Revision: 1.35 $ + */ +public class COSObject extends COSBase +{ + private static Logger log = Logger.getLogger(COSObject.class); + + private COSBase baseObject; + private COSInteger objectNumber; + private COSInteger generationNumber; + + /** + * Constructor. + * + * @param object The object that this encapsulates. + * + * @throws IOException If there is an error with the object passed in. + */ + public COSObject( COSBase object ) throws IOException + { + setObject( object ); + } + + /** + * This will get the dictionary object in this object that has the name key and + * if it is a pdfobjref then it will dereference that and return it. + * + * @param key The key to the value that we are searching for. + * + * @return The pdf object that matches the key. + */ + public COSBase getDictionaryObject( COSName key ) + { + COSBase retval =null; + if( baseObject instanceof COSDictionary ) + { + retval = ((COSDictionary)baseObject).getDictionaryObject( key ); + } + return retval; + } + + /** + * This will get the dictionary object in this object that has the name key. + * + * @param key The key to the value that we are searching for. + * + * @return The pdf object that matches the key. + */ + public COSBase getItem( COSName key ) + { + COSBase retval =null; + if( baseObject instanceof COSDictionary ) + { + retval = ((COSDictionary)baseObject).getItem( key ); + } + return retval; + } + + /** + * This will get the object that this object encapsulates. + * + * @return The encapsulated object. + */ + public COSBase getObject() + { + return baseObject; + } + + /** + * This will set the object that this object encapsulates. + * + * @param object The new object to encapsulate. + * + * @throws IOException If there is an error setting the updated object. + */ + public void setObject( COSBase object ) throws IOException + { + baseObject = object; + /*if( baseObject == null ) + { + baseObject = object; + } + else + { + //This is for when an object appears twice in the + //pdf file we really want to replace it such that + //object references still work correctly. + //see owcp-as-received.pdf for an example + if( baseObject instanceof COSDictionary ) + { + COSDictionary dic = (COSDictionary)baseObject; + COSDictionary dicObject = (COSDictionary)object; + dic.clear(); + dic.addAll( dicObject ); + } + else if( baseObject instanceof COSArray ) + { + COSArray array = (COSArray)baseObject; + COSArray arrObject = (COSArray)object; + array.clear(); + for( int i=0; i 0) ) +// { +// retval = new RandomAccessFileInputStream( file, +// 0, +// file.length() ); +// } +// else +// { + //if there is no stream data then simply return an empty stream. + retval = new ByteArrayInputStream( new byte[0] ); +// } + } + return retval; + } + + /** + * visitor pattern double dispatch method. + * + * @param visitor The object to notify when visiting this object. + * @return any object, depending on the visitor implementation, or null + * @throws COSVisitorException If an error occurs while visiting this object. + */ + public Object accept(ICOSVisitor visitor) throws COSVisitorException + { + return visitor.visitFromStream(this); + } + + /** + * This will decode the physical byte stream applying all of the filters to the stream. + * + * @throws IOException If there is an error applying a filter to the stream. + */ + private void doDecode() throws IOException + { + if( log.isDebugEnabled() ) + { + log.debug("doDecode() start"); + } +// FIXME: We shouldn't keep the same reference? + unFilteredStream = filteredStream; + + COSBase filters = getFilters(); + if( filters == null ) + { + if( log.isDebugEnabled() ) + { + log.debug("doDecode() - No filter to apply"); + } + } + else if( filters instanceof COSName ) + { + if( log.isDebugEnabled() ) + { + log.debug("doDecode( COSName )"); + } + doDecode( (COSName)filters ); + } + else if( filters instanceof COSArray ) + { + if( log.isDebugEnabled() ) + { + log.debug("doDecode( COSArray )"); + } + COSArray filterArray = (COSArray)filters; + for( int i=0; i=0; i-- ) + { + COSName filterName = (COSName)filterArray.get( i ); + doEncode( filterName ); + } + } + } + + /** + * This will encode applying a single filter on the stream. + * + * @param filterName The name of the filter. + * + * @throws IOException If there is an error parsing the stream. + */ + private void doEncode( COSName filterName ) throws IOException + { + FilterManager manager = getFilterManager(); + Filter filter = manager.getFilter( filterName ); + InputStream input; + + input = new BufferedInputStream( + new RandomAccessFileInputStream( file, filteredStream.getPosition(), + filteredStream.getLength() ), BUFFER_SIZE ); + filteredStream = new RandomAccessFileOutputStream( file ); + filter.encode( input, filteredStream, this ); + } + + /** + * This will return the filters to apply to the byte stream. + * The method will return + * - null if no filters are to be applied + * - a COSName if one filter is to be applied + * - a COSArray containing COSNames if multiple filters are to be applied + * + * @return the COSBase object representing the filters + */ + public COSBase getFilters() + { + return getDictionaryObject(COSName.FILTER); + } + + /** + * This will create a new stream for which filtered byte should be + * written to. You probably don't want this but want to use the + * createUnfilteredStream, which is used to write raw bytes to. + * + * @return A stream that can be written to. + * + * @throws IOException If there is an error creating the stream. + */ + public OutputStream createFilteredStream() throws IOException + { + filteredStream = new RandomAccessFileOutputStream( file ); + unFilteredStream = null; + return new BufferedOutputStream( filteredStream, BUFFER_SIZE ); + } + + /** + * This will create a new stream for which filtered byte should be + * written to. You probably don't want this but want to use the + * createUnfilteredStream, which is used to write raw bytes to. + * + * @param expectedLength An entry where a length is expected. + * + * @return A stream that can be written to. + * + * @throws IOException If there is an error creating the stream. + */ + public OutputStream createFilteredStream( COSBase expectedLength ) throws IOException + { + filteredStream = new RandomAccessFileOutputStream( file ); + filteredStream.setExpectedLength( expectedLength ); + unFilteredStream = null; + return new BufferedOutputStream( filteredStream, BUFFER_SIZE ); + } + + /** + * set the filters to be applied to the stream. + * + * @param filters The filters to set on this stream. + * + * @throws IOException If there is an error clearing the old filters. + */ + public void setFilters(COSBase filters) throws IOException + { + setItem(COSName.FILTER, filters); + // kill cached filtered streams + filteredStream = null; + } + + /** + * This will create an output stream that can be written to. + * + * @return An output stream which raw data bytes should be written to. + * + * @throws IOException If there is an error creating the stream. + */ + public OutputStream createUnfilteredStream() throws IOException + { + unFilteredStream = new RandomAccessFileOutputStream( file ); + filteredStream = null; + return new BufferedOutputStream( unFilteredStream, BUFFER_SIZE ); + } + + /** + * This will print a byte array as a hex string to standard output. + * + * @param data The array to print. + */ + private static void printHexString( byte[] data ) + { + for( int i=0; i".getBytes( "ISO-8859-1" ); + /** + * the escape character in strings. + */ + public static final byte[] ESCAPE = new byte[]{ 92 }; //"\\".getBytes( "ISO-8859-1" ); + + /** + * CR escape characters. + */ + public static final byte[] CR_ESCAPE = new byte[]{ 92, 114 }; //"\\r".getBytes( "ISO-8859-1" ); + /** + * LF escape characters. + */ + public static final byte[] LF_ESCAPE = new byte[]{ 92, 110 }; //"\\n".getBytes( "ISO-8859-1" ); + /** + * HT escape characters. + */ + public static final byte[] HT_ESCAPE = new byte[]{ 92, 116 }; //"\\t".getBytes( "ISO-8859-1" ); + /** + * BS escape characters. + */ + public static final byte[] BS_ESCAPE = new byte[]{ 92, 98 }; //"\\b".getBytes( "ISO-8859-1" ); + /** + * FF escape characters. + */ + public static final byte[] FF_ESCAPE = new byte[]{ 92, 102 }; //"\\f".getBytes( "ISO-8859-1" ); + + private ByteArrayOutputStream out = new ByteArrayOutputStream(); + + /** + * Constructor. + */ + public COSString() + { + } + + /** + * Explicit constructor for ease of manual PDF construction. + * + * @param value The string value of the object. + */ + public COSString( String value ) + { + try + { + boolean unicode16 = false; + char[] chars = value.toCharArray(); + for( int i=0; i 255 ) + { + unicode16 = true; + } + } + if( unicode16 ) + { + out.write( 0xFE ); + out.write( 0xFF ); + out.write( value.getBytes( "UTF-16BE" ) ); + } + else + { + out.write(value.getBytes()); + } + } + catch (IOException ignore) + { + ignore.printStackTrace(); + //should never happen + } + } + + /** + * Explicit constructor for ease of manual PDF construction. + * + * @param value The string value of the object. + */ + public COSString( byte[] value ) + { + try + { + out.write( value ); + } + catch (IOException ignore) + { + ignore.printStackTrace(); + //should never happen + } + } + + /** + * This will create a COS string from a string of hex characters. + * + * @param hex A hex string. + * @return A cos string with the hex characters converted to their actual bytes. + * @throws IOException If there is an error with the hex string. + */ + public static COSString createFromHexString( String hex ) throws IOException + { + COSString retval = new COSString(); + StringBuffer hexBuffer = new StringBuffer( hex.trim() ); + //if odd number then the last hex digit is assumed to be 0 + if( hexBuffer.length() % 2 == 1 ) + { + hexBuffer.append( "0" ); + } + for( int i=0; i 2 ) + { + if( data[0] == (byte)0xFF && data[1] == (byte)0xFE ) + { + encoding = "UTF-16LE"; + start=2; + } + else if( data[0] == (byte)0xFE && data[1] == (byte)0xFF ) + { + encoding = "UTF-16BE"; + start=2; + } + } + try + { + if( encoding != null ) + { + retval = new String( getBytes(), start, data.length-start, encoding ); + } + else + { + retval = new String( getBytes() ); + } + } + catch( UnsupportedEncodingException e ) + { + //should never happen + e.printStackTrace(); + retval = new String( getBytes() ); + } + return retval; + } + + /** + * This will append a byte[] to the string. + * + * @param data The byte[] to add to this string. + * + * @throws IOException If an IO error occurs while writing the byte. + */ + public void append( byte[] data ) throws IOException + { + out.write( data ); + } + + /** + * This will append a byte to the string. + * + * @param in The byte to add to this string. + * + * @throws IOException If an IO error occurs while writing the byte. + */ + public void append( int in ) throws IOException + { + out.write( in ); + } + + /** + * This will reset the internal buffer. + */ + public void reset() + { + out.reset(); + } + + /** + * This will get the bytes of the string. + * + * @return A byte array that represents the string. + */ + public byte[] getBytes() + { + return out.toByteArray(); + } + + /** + * @see Object#toString() + */ + public String toString() + { + return "COSString{" + new String( getBytes() ) + "}"; + } + + /** + * This will output this string as a PDF object. + * + * @param output The stream to write to. + * @throws IOException If there is an error writing to the stream. + */ + public void writePDF( OutputStream output ) throws IOException + { + boolean outsideASCII = false; + //Lets first check if we need to escape this string. + byte[] bytes = getBytes(); + for( int i=0; i + + + + + +These are the low level objects that make up a PDF document. +

+ +See the PDF Reference 1.4. + + -- cgit v1.2.3