org.apache.fop.pdf

Class PDFObject

Implemented Interfaces:
PDFWritable
Known Direct Subclasses:
PDFAction, PDFAnnotList, PDFArray, PDFCharProcs, PDFCIDFont, PDFCIDSystemInfo, PDFDestination, PDFDictionary, PDFEncryptionJCE, PDFFileSpec, PDFFunction, PDFGState, PDFICCBasedColorSpace, PDFInfo, PDFLink, PDFName, PDFNumber, PDFNumsArray, PDFOutline, PDFOutputIntent, PDFPages, PDFPathPaint, PDFResources, PDFShading, PDFText

public abstract class PDFObject
extends java.lang.Object
implements PDFWritable

generic PDF object. A PDF Document is essentially a collection of these objects. A PDF Object has a number and a generation (although the generation will always be 0 in new documents).

Field Summary

protected static SimpleDateFormat
DATE_FORMAT
Formatting pattern for PDF date
protected static Log
log
logger for all PDFObjects (and descendants)

Constructor Summary

PDFObject()
Default constructor.
PDFObject(PDFObject parent)
Constructor for direct objects.

Method Summary

static byte[]
encode(String text)
Converts text to a byte array for writing to a PDF file.
protected void
encodeBinaryToHexString(byte[] data, OutputStream out)
Encodes binary data as hexadecimal string object.
protected byte[]
encodeString(String string)
Encodes a String (3.2.3 in PDF 1.4 specs)
protected byte[]
encodeText(String text)
Encodes a Text String (3.8.1 in PDF 1.4 specs)
protected String
formatDateTime(Date time)
Formats a date/time according to the PDF specification.
protected String
formatDateTime(Date time, TimeZone tz)
Formats a date/time according to the PDF specification (D:YYYYMMDDHHmmSSOHH'mm').
protected void
formatObject(Object obj, OutputStream out, Writer writer)
Formats an object for serialization to PDF.
PDFDocument
getDocument()
Returns the parent PDFDocument if assigned.
PDFDocument
getDocumentSafely()
Returns the parent PDFDocument, but unlike getDocument() it throws an informative Exception if the parent document is unavailable instead of having a NullPointerException somewhere without a message.
int
getGeneration()
Returns the object's generation.
String
getObjectID()
Returns the PDF representation of the Object ID.
int
getObjectNumber()
Returns the object's number.
PDFObject
getParent()
Returns this objects's parent.
boolean
hasObjectNumber()
Indicates whether this PDFObject has already been assigned an object number.
PDFReference
makeReference()
Creates and returns a reference to this object.
protected int
output(OutputStream stream)
Write the PDF represention of this object
void
outputInline(OutputStream out, Writer writer)
String
referencePDF()
Returns the PDF representation of a reference to this object.
void
setDocument(PDFDocument doc)
Sets the parent PDFDocument.
void
setObjectNumber(int objnum)
Sets the object number
void
setParent(PDFObject parent)
Sets the direct parent object.
protected byte[]
toPDF()
Encodes the object as a byte array for output to a PDF file.
protected String
toPDFString()
This method returns a String representation of the PDF object.

Field Details

DATE_FORMAT

protected static final SimpleDateFormat DATE_FORMAT
Formatting pattern for PDF date

log

protected static Log log
logger for all PDFObjects (and descendants)

Constructor Details

PDFObject

public PDFObject()
Default constructor.

PDFObject

public PDFObject(PDFObject parent)
Constructor for direct objects.
Parameters:
parent - the containing PDFObject instance

Method Details

encode

public static final byte[] encode(String text)
Converts text to a byte array for writing to a PDF file.
Parameters:
text - text to convert/encode
Returns:
byte[] the resulting byte array

encodeBinaryToHexString

protected void encodeBinaryToHexString(byte[] data,
                                       OutputStream out)
            throws IOException
Encodes binary data as hexadecimal string object.
Parameters:
data - the binary data
out - the OutputStream to write the encoded object to

encodeString

protected byte[] encodeString(String string)
Encodes a String (3.2.3 in PDF 1.4 specs)
Parameters:
string - the string to encode
Returns:
byte[] the encoded string

encodeText

protected byte[] encodeText(String text)
Encodes a Text String (3.8.1 in PDF 1.4 specs)
Parameters:
text - the text to encode
Returns:
byte[] the encoded text

formatDateTime

protected String formatDateTime(Date time)
Formats a date/time according to the PDF specification. (D:YYYYMMDDHHmmSSOHH'mm').
Parameters:
time - date/time value to format
Returns:
the requested String representation

formatDateTime

protected String formatDateTime(Date time,
                                TimeZone tz)
Formats a date/time according to the PDF specification (D:YYYYMMDDHHmmSSOHH'mm').
Parameters:
time - date/time value to format
tz - the time zone
Returns:
the requested String representation

formatObject

protected void formatObject(Object obj,
                            OutputStream out,
                            Writer writer)
            throws IOException
Formats an object for serialization to PDF.
Parameters:
obj - the object
out - the OutputStream to write to
writer - a Writer for text content (will always be a wrapper around the above OutputStream. Make sure flush is called when mixing calls)

getDocument

public final PDFDocument getDocument()
Returns the parent PDFDocument if assigned.
Returns:
the parent PDFDocument (May be null if the parent PDFDocument has not been assigned)

getDocumentSafely

public final PDFDocument getDocumentSafely()
Returns the parent PDFDocument, but unlike getDocument() it throws an informative Exception if the parent document is unavailable instead of having a NullPointerException somewhere without a message.
Returns:
the parent PDFDocument

getGeneration

public int getGeneration()
Returns the object's generation.
Returns:
the PDF Object generation

getObjectID

public String getObjectID()
Returns the PDF representation of the Object ID.
Returns:
the Object ID

getObjectNumber

public int getObjectNumber()
Returns the object's number.
Returns:
the PDF Object number

getParent

public PDFObject getParent()
Returns this objects's parent. The parent is null if it is a "direct object".
Returns:
the parent or null if there's no parent (or it hasn't been set)

hasObjectNumber

public boolean hasObjectNumber()
Indicates whether this PDFObject has already been assigned an object number.
Returns:
True if it has an object number

makeReference

public PDFReference makeReference()
Creates and returns a reference to this object.
Returns:
the object reference

output

protected int output(OutputStream stream)
            throws IOException
Write the PDF represention of this object
Parameters:
stream - the stream to write the PDF to
Returns:
the number of bytes written

outputInline

public void outputInline(OutputStream out,
                         Writer writer)
            throws IOException
Specified by:
outputInline in interface PDFWritable

referencePDF

public String referencePDF()
Returns the PDF representation of a reference to this object.
Returns:
the reference string

setDocument

public void setDocument(PDFDocument doc)
Sets the parent PDFDocument.
Parameters:
doc - the PDFDocument.

setObjectNumber

public void setObjectNumber(int objnum)
Sets the object number
Parameters:
objnum - the object number

setParent

public void setParent(PDFObject parent)
Sets the direct parent object.
Parameters:
parent - the direct parent

toPDF

protected byte[] toPDF()
Encodes the object as a byte array for output to a PDF file.
Returns:
PDF string

toPDFString

protected String toPDFString()
This method returns a String representation of the PDF object. The result is normally converted/encoded to a byte array by toPDF(). Only use this method to implement the serialization if the object can be fully represented as text. If the PDF representation of the object contains binary content use toPDF() or output(OutputStream) instead. This applies to any object potentially containing a string object because string object are encrypted and therefore need to be binary.
Returns:
String the String representation

Copyright 1999-2008 The Apache Software Foundation. All Rights Reserved.