aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/apache/fop/pdf/PDFDocument.java
blob: 042f8b1a8357cba3146c37807e83beb6e1bae3eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*-- $Id$ -- 

 ============================================================================
                   The Apache Software License, Version 1.1
 ============================================================================
 
    Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
 
 Redistribution and use in source and binary forms, with or without modifica-
 tion, 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. The end-user documentation included with the redistribution, if any, must
    include  the following  acknowledgment:  "This product includes  software
    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
    Alternately, this  acknowledgment may  appear in the software itself,  if
    and wherever such third-party acknowledgments normally appear.
 
 4. The names "Fop" and  "Apache Software Foundation"  must not be used to
    endorse  or promote  products derived  from this  software without  prior
    written permission. For written permission, please contact
    apache@apache.org.
 
 5. Products  derived from this software may not  be called "Apache", nor may
    "Apache" appear  in their name,  without prior written permission  of the
    Apache Software Foundation.
 
 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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
 APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
 INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
 DING, 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.
 
 This software  consists of voluntary contributions made  by many individuals
 on  behalf of the Apache Software  Foundation and was  originally created by
 James Tauber <jtauber@jtauber.com>. For more  information on the Apache 
 Software Foundation, please see <http://www.apache.org/>.
 
 */
/* image support modified from work of BoBoGi */

package org.apache.fop.pdf;

// images are the one place that FOP classes outside this package get
// referenced and I'd rather not do it
import org.apache.fop.image.FopImage;

// Java
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;
							   
/**
 * class representing a PDF document. 
 *
 * The document is built up by calling various methods and then finally
 * output to given filehandle using output method.
 *
 * A PDF document consists of a series of numbered objects preceded by a
 * header and followed by an xref table and trailer. The xref table
 * allows for quick access to objects by listing their character
 * positions within the document. For this reason the PDF document must
 * keep track of the character position of each object.  The document
 * also keeps direct track of the /Root, /Info and /Resources objects.
 */
public class PDFDocument {

    /** the version of PDF supported */
    protected static final String pdfVersion = "1.2";

    /** the current character position */
    protected int position = 0;

    /** the character position of each object */
    protected Vector location = new Vector();

    /** the counter for object numbering */
    protected int objectcount = 0;

    /** the objects themselves */
    protected Vector objects = new Vector();

    /** character position of xref table */
    protected int xref;

    /** the /Root object */
    protected PDFRoot root;

    /** the /Info object */
    protected PDFInfo info;

    /** the /Resources object */
    protected PDFResources resources;
    
    protected int xObjectCount = 0;
    protected Vector xObjects = new Vector();

    /**
     * creates an empty PDF document
     */
    public PDFDocument() {

	/* create the /Root, /Info and /Resources objects */
	this.root = makeRoot();
	this.info = makeInfo();
	this.resources = makeResources();
    }
    
    /**
     * set the producer of the document
     *
     * @param producer string indicating application producing the PDF
     */
    public void setProducer(String producer) {
	this.info.setProducer(producer);
    }

    /**
     * make /Root object as next object
     *
     * @return the created /Root object
     */
    protected PDFRoot makeRoot() {

	/* create a PDFRoot with the next object number and add to
	   list of objects */
	PDFRoot pdfRoot = new PDFRoot(++this.objectcount);
	this.objects.addElement(pdfRoot);

	/* create a new /Pages object to be root of Pages hierarchy
	   and add to list of objects */
	PDFPages rootPages = new PDFPages(++this.objectcount);
	this.objects.addElement(rootPages);
	
	/* inform the /Root object of the /Pages root */
	pdfRoot.setRootPages(rootPages);
	return pdfRoot;
    }

    /**
     * make an /Info object
     *
     * @param producer string indicating application producing the PDF
     * @return the created /Info object
     */
    protected PDFInfo makeInfo() {

	/* create a PDFInfo with the next object number and add to
	   list of objects */
	PDFInfo pdfInfo = new PDFInfo(++this.objectcount);
	this.objects.addElement(pdfInfo);
	return pdfInfo;
    }

    /**
     * make a /Resources object
     *
     * @return the created /Resources object
     */
    private PDFResources makeResources() {

	/* create a PDFResources with the next object number and add
	   to list of objects */
	PDFResources pdfResources = new PDFResources(++this.objectcount);
	this.objects.addElement(pdfResources);
	return pdfResources;
    }

    /**
     * make a Type1 /Font object
     * 
     * @param fontname internal name to use for this font (eg "F1")
     * @param basefont name of the base font (eg "Helvetica")
     * @param encoding character encoding scheme used by the font
     * @return the created /Font object
     */
    public PDFFont makeFont(String fontname, String basefont,
			    String encoding) {

	/* create a PDFFont with the next object number and add to the
	   list of objects */
	PDFFont font = new PDFFont(++this.objectcount, fontname,
				   basefont, encoding);
	this.objects.addElement(font);
	return font;
    }

    public int addImage(FopImage img) {
	PDFXObject xObject = new PDFXObject(++this.objectcount,
					    ++this.xObjectCount, img);
	this.objects.addElement(xObject);
	this.xObjects.addElement(xObject);
	return xObjectCount;
    }

    /**
     * make a /Page object
     *
     * @param resources resources object to use
     * @param contents stream object with content
     * @param pagewidth width of the page in points
     * @param pageheight height of the page in points
     * @return the created /Page object
     */
    public PDFPage makePage(PDFResources resources,
			    PDFStream contents, int pagewidth,
			    int pageheight)  {

	/* create a PDFPage with the next object number, the given
	   resources, contents and dimensions */
	PDFPage page = new PDFPage(++this.objectcount, resources,
				   contents, pagewidth, pageheight);

	/* add it to the list of objects */
	this.objects.addElement(page);

	/* add the page to the Root */
	this.root.addPage(page);

	return page;
    }

    /**
     * make a stream object
     *
     * @return the stream object created
     */
    public PDFStream makeStream() {
    
	/* create a PDFStream with the next object number and add it
	   to the list of objects */
	PDFStream obj = new PDFStream(++this.objectcount);
	this.objects.addElement(obj);
	return obj;
    }

    /**
     * get the /Resources object for the document
     *
     * @return the /Resources object
     */
    public PDFResources getResources() {
	return this.resources;
    }

    /**
     * write the entire document out
     *
     * @param writer the PrinterWriter to output the document to
     */
    public void output(PrintWriter writer) throws IOException {

	/* output the header and increment the character position by
	   the header's length */
	this.position += outputHeader(writer);

	this.resources.setXObjects(xObjects);

	/* loop through the object numbers */
	for (int i=1; i <= this.objectcount; i++) {

	    /* add the position of this object to the list of object
	       locations */
	    this.location.addElement(new Integer(this.position));

	    /* retrieve the object with the current number */
	    PDFObject object = (PDFObject)this.objects.elementAt(i-1);

	    /* output the object and increment the character position
	       by the object's length */
	    this.position += object.output(writer);
	}

	/* output the xref table and increment the character position
	   by the table's length */
	this.position += outputXref(writer);

	/* output the trailer and flush the Writer */
	outputTrailer(writer);
	writer.flush();
    }

    /**
     * write the PDF header
     *
     * @param writer the PrintWriter to write the header to
     * @return the number of characters written
     */
    protected int outputHeader(PrintWriter writer) throws IOException {
	String pdf = "%PDF-" + this.pdfVersion + "\n";
	writer.write(pdf);
	return pdf.length();
    }

    /**
     * write the trailer
     *
     * @param writer the PrintWriter to write the trailer to
     */
    protected void outputTrailer(PrintWriter writer) throws IOException {

	/* construct the trailer */
	String pdf = "trailer\n<<\n/Size " + (this.objectcount+1)
	    + "\n/Root " + this.root.number + " " + this.root.generation
	    + " R\n/Info " + this.info.number + " " 
	    + this.info.generation + " R\n>>\nstartxref\n" + this.xref 
	    + "\n%%EOF\n";

	/* write the trailer */
	writer.write(pdf);
    }

    /**
     * write the xref table
     *
     * @param writer the PrintWriter to write the xref table to
     * @return the number of characters written
     */
    private int outputXref(PrintWriter writer) throws IOException {

	/* remember position of xref table */
	this.xref = this.position;

	/* construct initial part of xref */
	StringBuffer pdf = new StringBuffer("xref\n0 " + (this.objectcount+1) 
	    + "\n0000000000 65535 f \n");

	/* loop through object numbers */
	for (int i=1; i < this.objectcount+1; i++) {

	    /* contruct xref entry for object */
	    String padding = "0000000000";
	    String x = this.location.elementAt(i-1).toString();
	    String loc = padding.substring(x.length()) + x;

	    /* append to xref table */
	    pdf = pdf.append(loc + " 00000 n \n");
	}

	/* write the xref table and return the character length */
	writer.write(pdf.toString());
	return pdf.length();
    }
}