71
71
* @see PdfIndirectReference
74
73
public abstract class PdfObject {
76
// static membervariables (all the possible types of a PdfObject)
78
/** a possible type of <CODE>PdfObject</CODE> */
77
/** A possible type of <CODE>PdfObject</CODE> */
79
78
public static final int BOOLEAN = 1;
81
/** a possible type of <CODE>PdfObject</CODE> */
80
/** A possible type of <CODE>PdfObject</CODE> */
82
81
public static final int NUMBER = 2;
84
/** a possible type of <CODE>PdfObject</CODE> */
83
/** A possible type of <CODE>PdfObject</CODE> */
85
84
public static final int STRING = 3;
87
/** a possible type of <CODE>PdfObject</CODE> */
86
/** A possible type of <CODE>PdfObject</CODE> */
88
87
public static final int NAME = 4;
90
/** a possible type of <CODE>PdfObject</CODE> */
89
/** A possible type of <CODE>PdfObject</CODE> */
91
90
public static final int ARRAY = 5;
93
/** a possible type of <CODE>PdfObject</CODE> */
92
/** A possible type of <CODE>PdfObject</CODE> */
94
93
public static final int DICTIONARY = 6;
96
/** a possible type of <CODE>PdfObject</CODE> */
95
/** A possible type of <CODE>PdfObject</CODE> */
97
96
public static final int STREAM = 7;
99
/** a possible type of <CODE>PdfObject</CODE> */
98
/** A possible type of <CODE>PdfObject</CODE> */
100
99
public static final int NULL = 8;
102
/** a possible type of <CODE>PdfObject</CODE> */
103
public static final int INDIRECT = 10;
105
/** This is an empty string used for the <CODE>PdfNull</CODE>-object and for an empty <CODE>PdfString</CODE>-object. */
101
/** A possible type of <CODE>PdfObject</CODE> */
102
public static final int INDIRECT = 10;
104
/** An empty string used for the <CODE>PdfNull</CODE>-object and for an empty <CODE>PdfString</CODE>-object. */
106
105
public static final String NOTHING = "";
108
/** This is the default encoding to be used for converting Strings into bytes and vice versa.
109
* The default encoding is PdfDocEncoding.
108
* This is the default encoding to be used for converting Strings into
109
* bytes and vice versa. The default encoding is PdfDocEncoding.
111
111
public static final String TEXT_PDFDOCENCODING = "PDF";
113
/** This is the encoding to be used to output text in Unicode. */
113
/** This is the encoding to be used to output text in Unicode. */
114
114
public static final String TEXT_UNICODE = "UnicodeBig";
118
/** the content of this <CODE>PdfObject</CODE> */
118
/** The content of this <CODE>PdfObject</CODE> */
119
119
protected byte[] bytes;
121
/** the type of this <CODE>PdfObject</CODE> */
121
/** The type of this <CODE>PdfObject</CODE> */
122
122
protected int type;
124
/** Holds the indirect reference. */
125
protected PdfIndirectReference indRef;
125
* Holds value of property indRef.
130
* Constructs a <CODE>PdfObject</CODE> of a certain <VAR>type</VAR>
131
* without any <VAR>content</VAR>.
133
* @param type type of the new <CODE>PdfObject</CODE>
127
protected PRIndirectReference indRef;
132
* Constructs a <CODE>PdfObject</CODE> of a certain <VAR>type</VAR> without any <VAR>content</VAR>.
134
* @param type type of the new <CODE>PdfObject</CODE>
137
135
protected PdfObject(int type) {
138
136
this.type = type;
142
* Constructs a <CODE>PdfObject</CODE> of a certain <VAR>type</VAR> with a certain <VAR>content</VAR>.
144
* @param type type of the new <CODE>PdfObject</CODE>
145
* @param content content of the new <CODE>PdfObject</CODE> as a <CODE>String</CODE>.
140
* Constructs a <CODE>PdfObject</CODE> of a certain <VAR>type</VAR>
141
* with a certain <VAR>content</VAR>.
143
* @param type type of the new <CODE>PdfObject</CODE>
144
* @param content content of the new <CODE>PdfObject</CODE> as a
145
* <CODE>String</CODE>.
148
147
protected PdfObject(int type, String content) {
149
148
this.type = type;
150
149
bytes = PdfEncodings.convertToBytes(content, null);
154
* Constructs a <CODE>PdfObject</CODE> of a certain <VAR>type</VAR> with a certain <VAR>content</VAR>.
156
* @param type type of the new <CODE>PdfObject</CODE>
157
* @param bytes content of the new <CODE>PdfObject</CODE> as an array of <CODE>byte</CODE>.
153
* Constructs a <CODE>PdfObject</CODE> of a certain <VAR>type</VAR>
154
* with a certain <VAR>content</VAR>.
156
* @param type type of the new <CODE>PdfObject</CODE>
157
* @param bytes content of the new <CODE>PdfObject</CODE> as an array of
160
160
protected PdfObject(int type, byte[] bytes) {
161
161
this.bytes = bytes;
162
162
this.type = type;
165
165
// methods dealing with the content of this object
168
* Writes the PDF representation of this <CODE>PdfObject</CODE> as an array of <CODE>byte</CODE>s to the writer.
169
* @param writer for backwards compatibility
170
* @param os the outputstream to write the bytes to.
171
* @throws IOException
168
* Writes the PDF representation of this <CODE>PdfObject</CODE> as an
169
* array of <CODE>byte</CODE>s to the writer.
171
* @param writer for backwards compatibility
172
* @param os The <CODE>OutputStream</CODE> to write the bytes to.
173
* @throws IOException
174
175
public void toPdf(PdfWriter writer, OutputStream os) throws IOException {
175
176
if (bytes != null)
181
* Returns the <CODE>String</CODE>-representation of this
182
* <CODE>PdfObject</CODE>.
184
* @return a <CODE>String</CODE>
186
public String toString() {
188
return super.toString();
189
return PdfEncodings.convertToString(bytes, null);
180
193
* Gets the presentation of this object in a byte array
181
195
* @return a byte array
183
197
public byte[] getBytes() {
188
* Can this object be in an object stream?
189
* @return true if this object can be in an object stream.
202
* Whether this object can be contained in an object stream.
204
* PdfObjects of type STREAM OR INDIRECT can not be contained in an
207
* @return <CODE>true</CODE> if this object can be in an object stream.
208
* Otherwise <CODE>false</CODE>
191
210
public boolean canBeInObjStm() {
192
return (type >= 1 && type <= 6) || type == 8;
196
* Returns the length of the PDF representation of the <CODE>PdfObject</CODE>.
198
* In some cases, namely for <CODE>PdfString</CODE> and <CODE>PdfStream</CODE>,
199
* this method differs from the method <CODE>length</CODE> because <CODE>length</CODE>
200
* returns the length of the actual content of the <CODE>PdfObject</CODE>.</P>
202
* Remark: the actual content of an object is in most cases identical to its representation.
203
* The following statement is always true: length() >= pdfLength().</P>
228
* Returns the length of the PDF representation of the <CODE>PdfObject</CODE>.
230
* In some cases, namely for <CODE>PdfString</CODE> and <CODE>PdfStream</CODE>,
231
* this method differs from the method <CODE>length</CODE> because <CODE>length</CODE>
232
* returns the length of the actual content of the <CODE>PdfObject</CODE>.</P>
234
* Remark: the actual content of an object is in most cases identical to its representation.
235
* The following statement is always true: length() >= pdfLength().</P>
208
239
// public int pdfLength() {
209
240
// return toPdf(null).length;
213
* Returns the <CODE>String</CODE>-representation of this <CODE>PdfObject</CODE>.
215
* @return a <CODE>String</CODE>
218
public String toString() {
220
return super.toString();
222
return PdfEncodings.convertToString(bytes, null);
226
* Returns the length of the actual content of the <CODE>PdfObject</CODE>.
228
* In some cases, namely for <CODE>PdfString</CODE> and <CODE>PdfStream</CODE>,
229
* this method differs from the method <CODE>pdfLength</CODE> because <CODE>pdfLength</CODE>
230
* returns the length of the PDF representation of the object, not of the actual content
231
* as does the method <CODE>length</CODE>.</P>
233
* Remark: the actual content of an object is in some cases identical to its representation.
234
* The following statement is always true: length() >= pdfLength().</P>
244
* Returns the length of the actual content of the <CODE>PdfObject</CODE>.
246
* In some cases, namely for <CODE>PdfString</CODE> and <CODE>PdfStream</CODE>,
247
* this method differs from the method <CODE>pdfLength</CODE> because <CODE>pdfLength</CODE>
248
* returns the length of the PDF representation of the object, not of the actual content
249
* as does the method <CODE>length</CODE>.</P>
251
* Remark: the actual content of an object is in some cases identical to its representation.
252
* The following statement is always true: length() >= pdfLength().</P>
254
* @return The length as <CODE>int</CODE>
239
256
public int length() {
240
257
return toString().length();
244
* Changes the content of this <CODE>PdfObject</CODE>.
246
* @param content the new content of this <CODE>PdfObject</CODE>
261
* Changes the content of this <CODE>PdfObject</CODE>.
263
* @param content the new content of this <CODE>PdfObject</CODE>
249
265
protected void setContent(String content) {
250
266
bytes = PdfEncodings.convertToBytes(content, null);
253
269
// methods dealing with the type of this object
256
* Returns the type of this <CODE>PdfObject</CODE>.
272
* Returns the type of this <CODE>PdfObject</CODE>.
275
* - <VAR>NULL</VAR>: A <CODE>PdfNull</CODE>
276
* - <VAR>BOOLEAN</VAR>: A <CODE>PdfBoolean</CODE>
277
* - <VAR>NUMBER</VAR>: A <CODE>PdfNumber</CODE>
278
* - <VAR>STRING</VAR>: A <CODE>PdfString</CODE>
279
* - <VAR>NAME</VAR>: A <CODE>PdfName</CODE>
280
* - <VAR>ARRAY</VAR>: A <CODE>PdfArray</CODE>
281
* - <VAR>DICTIONARY</VAR>: A <CODE>PdfDictionary</CODE>
282
* - <VAR>STREAM</VAR>: A <CODE>PdfStream</CODE>
283
* - <VAR>INDIRECT</VAR>: ><CODE>PdfIndirectObject</CODE>
261
287
public int type() {
266
* Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfNull</CODE>.
268
* @return <CODE>true</CODE> or <CODE>false</CODE>
292
* Checks if this <CODE>PdfObject</CODE> is of the type
293
* <CODE>PdfNull</CODE>.
295
* @return <CODE>true</CODE> or <CODE>false</CODE>
271
297
public boolean isNull() {
272
return (this.type == NULL);
298
return (type == NULL);
276
* Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfBoolean</CODE>.
278
* @return <CODE>true</CODE> or <CODE>false</CODE>
302
* Checks if this <CODE>PdfObject</CODE> is of the type
303
* <CODE>PdfBoolean</CODE>.
305
* @return <CODE>true</CODE> or <CODE>false</CODE>
281
307
public boolean isBoolean() {
282
return (this.type == BOOLEAN);
308
return (type == BOOLEAN);
286
* Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfNumber</CODE>.
288
* @return <CODE>true</CODE> or <CODE>false</CODE>
312
* Checks if this <CODE>PdfObject</CODE> is of the type
313
* <CODE>PdfNumber</CODE>.
315
* @return <CODE>true</CODE> or <CODE>false</CODE>
291
317
public boolean isNumber() {
292
return (this.type == NUMBER);
318
return (type == NUMBER);
296
* Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfString</CODE>.
298
* @return <CODE>true</CODE> or <CODE>false</CODE>
322
* Checks if this <CODE>PdfObject</CODE> is of the type
323
* <CODE>PdfString</CODE>.
325
* @return <CODE>true</CODE> or <CODE>false</CODE>
301
327
public boolean isString() {
302
return (this.type == STRING);
328
return (type == STRING);
306
* Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfName</CODE>.
308
* @return <CODE>true</CODE> or <CODE>false</CODE>
332
* Checks if this <CODE>PdfObject</CODE> is of the type
333
* <CODE>PdfName</CODE>.
335
* @return <CODE>true</CODE> or <CODE>false</CODE>
311
337
public boolean isName() {
312
return (this.type == NAME);
338
return (type == NAME);
316
* Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfArray</CODE>.
318
* @return <CODE>true</CODE> or <CODE>false</CODE>
342
* Checks if this <CODE>PdfObject</CODE> is of the type
343
* <CODE>PdfArray</CODE>.
345
* @return <CODE>true</CODE> or <CODE>false</CODE>
321
347
public boolean isArray() {
322
return (this.type == ARRAY);
348
return (type == ARRAY);
326
* Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfDictionary</CODE>.
328
* @return <CODE>true</CODE> or <CODE>false</CODE>
352
* Checks if this <CODE>PdfObject</CODE> is of the type
353
* <CODE>PdfDictionary</CODE>.
355
* @return <CODE>true</CODE> or <CODE>false</CODE>
331
357
public boolean isDictionary() {
332
return (this.type == DICTIONARY);
358
return (type == DICTIONARY);
336
* Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfStream</CODE>.
338
* @return <CODE>true</CODE> or <CODE>false</CODE>
362
* Checks if this <CODE>PdfObject</CODE> is of the type
363
* <CODE>PdfStream</CODE>.
365
* @return <CODE>true</CODE> or <CODE>false</CODE>
341
367
public boolean isStream() {
342
return (this.type == STREAM);
368
return (type == STREAM);
346
* Checks if this is an indirect object.
347
* @return true if this is an indirect object
372
* Checks if this <CODE>PdfObject</CODE> is of the type
373
* <CODE>PdfIndirectObject</CODE>.
375
* @return <CODE>true</CODE> if this is an indirect object,
376
* otherwise <CODE>false</CODE>
349
378
public boolean isIndirect() {
350
return (this.type == INDIRECT);
354
* Getter for property indRef.
355
* @return Value of property indRef.
357
public PRIndirectReference getIndRef() {
362
* Setter for property indRef.
363
* @param indRef New value of property indRef.
365
public void setIndRef(PRIndirectReference indRef) {
379
return (type == INDIRECT);
383
* Get the indirect reference
385
* @return A <CODE>PdfIndirectReference</CODE>
387
public PdfIndirectReference getIndRef() {
392
* Set the indirect reference
394
* @param indRef New value as a <CODE>PdfIndirectReference</CODE>
396
public void setIndRef(PdfIndirectReference indRef) {
366
397
this.indRef = indRef;
398
indRef.setDirectObject(this);
402
* Simply here to be overridden by indirect references.
407
public PdfObject getDirectObject() {