KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > jpdf > PDFOutput


1 /*
2  * $Id: PDFOutput.java,v 1.1.1.1 2001/10/29 19:51:08 ezb Exp $
3  *
4  * $Date: 2001/10/29 19:51:08 $
5  *
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  */

22 package gnu.jpdf;
23
24 import java.io.*;
25 import java.util.*;
26
27 /**
28  * This class is used to write a PDF document. It acts as a wrapper
29  * to a real OutputStream, but is necessary for certain internal PDF
30  * structures to be built correctly.
31  *
32  * @author Peter T. Mount
33  * @author Eric Z. Beard, ericzbeard@hotmail.com
34  * @author $Author: ezb $
35  * @version $Revision: 1.1.1.1 $, $Date: 2001/10/29 19:51:08 $
36  */

37 public class PDFOutput
38 {
39   /**
40    * This is the actual OutputStream used to write to.
41    */

42   protected OutputStream os;
43   
44   /**
45    * This is the OutputStream used to write each object to.
46    *
47    * <p>We use a separate stream, because we need to keep track of how
48    * many bytes have been written for each object for the xref table to
49    * work correctly.
50    */

51   protected ByteArrayOutputStream baos;
52   
53   /**
54    * This is the current position within the stream
55    */

56   protected int offset;
57   
58   /**
59    * This vector contains offsets of each object
60    */

61   protected Vector offsets;
62   
63   /**
64    * This is used to track the /Root object (catalog)
65    */

66   protected PDFObject rootID;
67   
68   /**
69    * This is used to track the /Info object (info)
70    */

71   protected PDFObject infoID;
72   
73   /**
74    * This creates a PDF OutputStream
75    *
76    * @param os The output stream to write the PDF file to.
77    */

78   public PDFOutput(OutputStream os) throws IOException
79   {
80     this.os = os;
81     offset = 0;
82     offsets = new Vector();
83     baos = new ByteArrayOutputStream();
84     
85     // Now write the PDF header
86
//
87
// Note: As the encoding is fixed here, we use getBytes().
88
//
89
baos.write("%PDF-1.2\n".getBytes());
90     
91     // This second comment is advised in the PDF Reference manual
92
// page 61
93
baos.write("%\342\343\317\323\n".getBytes());
94     
95     offset = baos.size();
96     baos.writeTo(os);
97   }
98   
99   /**
100    * This method writes a PDFObject to the stream.
101    *
102    * @param ob PDFObject Obeject to write
103    * @exception IOException on error
104    */

105   protected void write(PDFObject ob) throws IOException
106   {
107     // Check the object to see if it's one that is needed in the trailer
108
// object
109
if(ob instanceof PDFCatalog) rootID=ob;
110     if(ob instanceof PDFInfo) infoID=ob;
111     
112     offsets.addElement(new PDFXref(ob.getSerialID(),offset));
113     baos.reset();
114     ob.write(baos);
115     offset+=baos.size();
116     baos.writeTo(os);
117   }
118   
119   /**
120    * This closes the Stream, writing the xref table
121    */

122   protected void close() throws IOException
123   {
124     // Make sure everything is written
125
os.flush();
126     
127     // we use baos to speed things up a little.
128
// Also, offset is preserved, and marks the begining of this block.
129
// This is required by PDF at the end of the PDF file.
130
baos.reset();
131     baos.write("xref\n".getBytes());
132     
133     // Now a single subsection for object 0
134
//baos.write("0 1\n0000000000 65535 f \n".getBytes());
135

136     // Now scan through the offsets list. The should be in sequence,
137
// but just in case:
138
int firstid = 0; // First id in block
139
int lastid = -1; // The last id used
140
Vector block = new Vector(); // xrefs in this block
141

142     // We need block 0 to exist
143
block.addElement(new PDFXref(0,0,65535));
144     
145     for(Enumeration en = offsets.elements(); en.hasMoreElements(); ) {
146       PDFXref x = (PDFXref)en.nextElement();
147       
148       if(firstid==-1) firstid=x.id;
149       
150       // check to see if block is in range (-1 means empty)
151
if(lastid>-1 && x.id != (lastid+1)) {
152         // no, so write this block, and reset
153
writeblock(firstid,block);
154         block.removeAllElements();
155         firstid=-1;
156       }
157       
158       // now add to block
159
block.addElement(x);
160       lastid = x.id;
161     }
162     
163     // now write the last block
164
if(firstid>-1)
165       writeblock(firstid,block);
166     
167     // now the trailer object
168
baos.write("trailer\n<<\n".getBytes());
169     
170     // the number of entries (REQUIRED)
171
baos.write("/Size ".getBytes());
172     baos.write(Integer.toString(offsets.size()+1).getBytes());
173     baos.write("\n".getBytes());
174     
175     // the /Root catalog indirect reference (REQUIRED)
176
if(rootID != null) {
177       baos.write("/Root ".getBytes());
178       baos.write(rootID.toString().getBytes());
179       baos.write("\n".getBytes());
180     } else
181       throw new IOException("Root object is not present in document");
182     
183     // the /Info reference (OPTIONAL)
184
if(infoID != null) {
185       baos.write("/Info ".getBytes());
186       baos.write(infoID.toString().getBytes());
187       baos.write("\n".getBytes());
188     }
189     
190     // end the trailer object
191
baos.write(">>\nstartxref\n".getBytes());
192     baos.write(Integer.toString(offset).getBytes());
193     baos.write("\n%%EOF\n".getBytes());
194     
195     // now flush the stream
196
baos.writeTo(os);
197     os.flush();
198   }
199   
200   /**
201    * Writes a block of references to the PDF file
202    * @param firstid ID of the first reference in this block
203    * @param block Vector containing the references in this block
204    * @exception IOException on write error
205    */

206   protected void writeblock(int firstid,Vector block) throws IOException
207   {
208     baos.write(Integer.toString(firstid).getBytes());
209     baos.write(" ".getBytes());
210     baos.write(Integer.toString(block.size()).getBytes());
211     baos.write("\n".getBytes());
212     //baos.write("\n0000000000 65535 f\n".getBytes());
213

214     for(Enumeration en=block.elements(); en.hasMoreElements(); ) {
215       baos.write(en.nextElement().toString().getBytes());
216       baos.write("\n".getBytes());
217     }
218   }
219 } // end class PDFOutput
220
Popular Tags