KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > jpdf > PDFJob


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

20 package gnu.jpdf;
21
22 import java.awt.*;
23 import java.io.*;
24 import java.util.*;
25
26
27 /**
28  * <p>This class extends awt's PrintJob, to provide a simple method of writing
29  * PDF documents.</p>
30  *
31  * <p>You can use this with any code that uses Java's printing mechanism. It
32  * does include a few extra methods to provide access to some of PDF's features
33  * like annotations, or outlines.</p>
34  *
35  *
36  * @author Peter T Mount, http://www.retep.org.uk/pdf/
37  * @author Eric Z. Beard, ericzbeard@hotmail.com
38  * @author $Author: ezb $
39  * @version $Revision: 1.2 $, $Date: 2001/11/15 20:18:11 $
40  */

41 public class PDFJob extends PrintJob implements Serializable
42 {
43   /*
44    * NOTE: The original class is the work of Peter T. Mount, who released it
45    * in the uk.org.retep.pdf package. It was modified by Eric Z. Beard as
46    * follows:
47    * The package name was changed to gnu.jpdf
48    * The formatting was changed a little bit.
49    * This used to subclass an abstract class with the same name in
50    * another package to support jdk1.1. Now it's one concrete class,
51    * with no jdk1.1 support
52    * Instances of PDFJob come directly from constructors, not
53    * static methods in PDFDocument (which used to be PDF)
54    * It is still licensed under the LGPL.
55    */

56
57
58   /**
59    * This is the OutputStream the PDF file will be written to when complete
60    * Note: This is transient, as it's not valid after being Serialized.
61    */

62   protected transient OutputStream os;
63   
64   /**
65    * This is the PDF file being constructed
66    */

67   protected PDFDocument pdfDocument;
68   
69   /**
70    * This is the current page being constructed by the last getGraphics()
71    * call
72    */

73   protected PDFPage page;
74   
75   /**
76    * This is the page number of the current page
77    */

78   protected int pagenum;
79
80
81   // Constructors
82

83   /**
84    * <p>This constructs the job. This method must be used when creating a
85    * template pdf file, ie one that is Serialised by one application, and
86    * then restored by another.</p>
87    *
88    * <p>ezb 20011115 - Haven't done anything with templates yet, don't know
89    * how/if they are implemented</p>
90    */

91   public PDFJob() {
92     this(null);
93   }
94   
95   /**
96    * <p>This constructs the job. This is the primary constructor that
97    * will be used for creating pdf documents with this package. The
98    * specified output stream is a handle to the .pdf file you wish to
99    * create.</p>
100    *
101    * @param os - <code>OutputStream</code> to use for the pdf output
102    */

103   public PDFJob(OutputStream os) {
104     this(os, "PDF Doc");
105   }
106   
107   /**
108    * <p>This constructs the job. This is the primary constructor that
109    * will be used for creating pdf documents with this package. The
110    * specified output stream is a handle to the .pdf file you wish to
111    * create.</p>
112    *
113    * <p>Use this constructor if you want to give the pdf document a name
114    * other than the default of "PDF Doc"</p>
115    *
116    * @param os - <code>OutputStream</code> to use for the pdf output
117    * @param title a <code>String</code> value
118    */

119   public PDFJob(OutputStream os, String JavaDoc title) {
120     this.os = os;
121     this.pdfDocument = new PDFDocument();
122     pagenum = 0;
123     pdfDocument.getPDFInfo().setTitle(title);
124   }
125
126
127   /**
128    * <p>This returns a graphics object that can be used to draw on a page.
129    * In PDF, this will be a new page within the document.</p>
130    *
131    * @param orient - the <code>int</code> Orientation of the new page,
132    * as defined in <code>PDFPage</code>
133    * @return Graphics object to draw.
134    * @see gnu.jpdf.PDFPage#PORTRAIT
135    * @see gnu.jpdf.PDFPage#LANDSCAPE
136    * @see gnu.jpdf.PDFPage#INVERTEDPORTRAIT
137    * @see gnu.jpdf.PDFPage#SEASCAPE
138    */

139   public Graphics getGraphics(int orient) {
140     // create a new page
141
page = new PDFPage(orient);
142     pdfDocument.add(page);
143     pagenum++;
144     
145     // Now create a Graphics object to draw onto the page
146
return new graphic(page,this);
147   }
148     
149   
150   
151   
152   
153   /**
154    * <p>This writes the PDF document to the OutputStream, finishing the
155    * document.</p>
156    */

157   public void end() {
158     try {
159       pdfDocument.write(os);
160     } catch(IOException ioe) {
161       // Ideally we should throw this. However, PrintJob doesn't throw
162
// anything, so we will print the Stack Trace instead.
163
ioe.printStackTrace();
164     }
165     
166     // This should mark us as dead
167
os = null;
168     pdfDocument = null;
169   }
170  
171
172
173   /**
174    * <p>This returns a graphics object that can be used to draw on a page.
175    * In PDF, this will be a new page within the document.</p>
176    *
177    * <p>This new page will by default be oriented as a portrait</p>
178    *
179    * @return a <code>Graphics</code> object to draw to.
180    */

181   public Graphics getGraphics() {
182     return getGraphics(PDFPage.PORTRAIT);
183   }
184   
185   
186   
187   /**
188    * <p>Returns the page dimension</p>
189    *
190    * @return a <code>Dimension</code> instance, the size of the page
191    */

192   public Dimension getPageDimension() {
193     if (page == null) {
194       System.err.println("PDFJob.getPageDimension(), page is null");
195     }
196     Rectangle r = page.getMedia();
197     
198     // if landscape or seascape, then we swap the dimensions which
199
// should fool existing code.
200
int rot = page.getOrientation();
201     if(rot == 90 || rot == 270) {
202       return new Dimension(r.height-r.y,r.width-r.x);
203     }
204     return new Dimension(r.width-r.x,r.height-r.y);
205   }
206   
207   
208   /**
209    * <p>How about a setPageDimension(Rectangle media) ?? </p>
210    */

211   
212
213   /**
214    * This returns the page resolution.
215    *
216    * <p>This is the PDF (and Postscript) device resolution of 72 dpi
217    * (equivalent to 1 point).</p>
218    *
219    * @return an <code>int</code>, the resolution in pixels per inch
220    */

221   public int getPageResolution() {
222     return 72;
223   }
224   
225
226
227
228   /**
229    * <p>In AWT's PrintJob, this would return true if the user requested that the
230    * file is printed in reverse order. For PDF's this is not applicable, so
231    * it will always return false.</p>
232    *
233    * @return false
234    */

235   public boolean lastPageFirst() {
236     return false;
237   }
238   
239   //======== END OF PrintJob extension ==========
240

241
242
243   /**
244    * Returns the PDFDocument object for this document.
245    * Useful for gaining access to
246    * the internals of PDFDocument.
247    * @return the PDF object
248    */

249   public PDFDocument getPDFDocument() {
250     return pdfDocument;
251   }
252   
253   /**
254    * <p>Returns the current PDFPage being worked on. Useful for working on
255    * Annotations (like links), etc.</p>
256    *
257    * @return the <code>PDFPage</code> currently being constructed
258    */

259   public PDFPage getCurrentPage() {
260     return page;
261   }
262   
263   /**
264    * <p>Returns the current page number.
265    * Useful if you need to include one in the document</p>
266    *
267    * @return the <code>int</code> current page number
268    */

269   public int getCurrentPageNumber() {
270     return pagenum;
271   }
272  
273
274   
275   /**
276    * <p>This method attaches an outline to the current page being generated.
277    * When selected, the outline displays the top of the page.</p>
278    *
279    * @param title a <code>String</code>, the title of the Outline
280    * @return a <code>PDFOutline</code> object that was created,
281    * for adding sub-outline's if required.
282    */

283   public PDFOutline addOutline(String JavaDoc title) {
284     return page.addOutline(title);
285   }
286   
287   /**
288    * <p>This method attaches an outline to the current page being generated.
289    * When selected, the outline displays the specified region.</p>
290    *
291    * @param title Outline title to attach
292    * @param x Left coordinate of region
293    * @param y Top coordinate of region
294    * @param w width of region
295    * @param h height of region
296    * @return the <code>PDFOutline</code> object created,
297    * for adding sub-outline's if required.
298    */

299   public PDFOutline addOutline(String JavaDoc title,int x,int y,int w,int h) {
300     return page.addOutline(title,x,y,w,h);
301   }
302   
303   /**
304    * Convenience method: Adds a text note to the document.
305    * @param note Text of the note
306    * @param x Coordinate of note
307    * @param y Coordinate of note
308    * @param w Width of the note
309    * @param h Height of the note
310    * @return Returns the annotation, so other settings can be changed.
311    */

312   public PDFAnnot addNote(String JavaDoc note,int x,int y,int w,int h) {
313     return page.addNote(note,x,y,w,h);
314   }
315   
316   
317   /**
318    * <p>This inner class extends PDFGraphics for the PrintJob.</p>
319    *
320    * <p>Like with java.awt, Graphics instances created with PrintJob implement
321    * the PrintGraphics interface. Here we implement that method, and overide
322    * PDFGraphics.create() method, so all instances have this interface.</p>
323    */

324   class graphic extends PDFGraphics implements PrintGraphics {
325     /**
326      * The PDFJob we are linked with
327      */

328     private PDFJob job;
329     
330     /**
331      * @param page to attach to
332      * @param job PDFJob containing this graphic
333      */

334     graphic(PDFPage page,PDFJob job) {
335       super();
336       this.init(page);
337       this.job = job;
338     }
339     
340     /**
341      * This is used by our version of create()
342      */

343     graphic(PDFPage page,PDFJob job,PrintWriter pw) {
344       super();
345       this.init(page,pw);
346       this.job = job;
347     }
348     
349     /**
350      * This returns a child instance of this Graphics object. As with AWT,
351      * the affects of using the parent instance while the child exists,
352      * is not determined.
353      *
354      * <p>This method is used to make a new Graphics object without
355      * going to a new page</p>
356      *
357      * <p>Once complete, the child should be released with it's dispose()
358      * method which will restore the graphics state to it's parent.
359      *
360      * @return Graphics object
361      */

362     public Graphics create() {
363       closeBlock();
364       graphic g = new graphic(getPage(),job,getWriter());
365       
366       // The new instance inherits a few items
367
g.clipRectangle = new Rectangle(clipRectangle);
368       
369       return (Graphics) g;
370     }
371     
372     /**
373      * This is the PrintGraphics interface
374      * @return PrintJob for this object
375      */

376     public PrintJob getPrintJob() {
377       return (PrintJob)job;
378     }
379     
380   } // end inner class graphic
381

382 } // end class PDFJob
383

384
Popular Tags