KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > stefanochizzolini > clown > documents > Document


1 /*
2   Copyright © 2006 Stefano Chizzolini. http://clown.stefanochizzolini.it
3
4   Contributors:
5     * Stefano Chizzolini (original code developer, info@stefanochizzolini.it):
6       contributed code is Copyright © 2006 by Stefano Chizzolini.
7
8   This file should be part of the source code distribution of "PDF Clown library"
9   (the Program): see the accompanying README files for more info.
10
11   This Program is free software; you can redistribute it and/or modify it under
12   the terms of the GNU General Public License as published by the Free Software
13   Foundation; either version 2 of the License, or (at your option) any later version.
14
15   This Program is distributed in the hope that it will be useful, but WITHOUT ANY
16   WARRANTY, either expressed or implied; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more details.
18
19   You should have received a copy of the GNU General Public License along with this
20   Program (see README files); if not, go to the GNU website (http://www.gnu.org/).
21
22   Redistribution and use, with or without modification, are permitted provided that such
23   redistributions retain the above copyright notice, license and disclaimer, along with
24   this list of conditions.
25 */

26
27 package it.stefanochizzolini.clown.documents;
28
29 import it.stefanochizzolini.clown.documents.contents.Resources;
30 import it.stefanochizzolini.clown.documents.interaction.ViewerPreferences;
31 import it.stefanochizzolini.clown.documents.interaction.navigation.document.Bookmarks;
32 import it.stefanochizzolini.clown.documents.interchange.metadata.Information;
33 import it.stefanochizzolini.clown.files.File;
34 import it.stefanochizzolini.clown.objects.IPdfNumber;
35 import it.stefanochizzolini.clown.objects.PdfArray;
36 import it.stefanochizzolini.clown.objects.PdfDictionary;
37 import it.stefanochizzolini.clown.objects.PdfDirectObject;
38 import it.stefanochizzolini.clown.objects.PdfInteger;
39 import it.stefanochizzolini.clown.objects.PdfName;
40 import it.stefanochizzolini.clown.objects.PdfObjectWrapper;
41 import it.stefanochizzolini.clown.objects.PdfReal;
42 import it.stefanochizzolini.clown.objects.PdfReference;
43 import it.stefanochizzolini.clown.util.NotImplementedException;
44
45 import java.awt.geom.Dimension2D JavaDoc;
46 import java.util.ArrayList JavaDoc;
47 import java.util.Collection JavaDoc;
48
49 /**
50   PDF document.
51 */

52 public class Document
53   extends PdfObjectWrapper<PdfDictionary>
54 {
55   // <class>
56
// <dynamic>
57
// <fields>
58
/**
59     <h3>Remarks</h3>
60     <p>For internal use only.</p>
61   */

62   public java.util.Hashtable JavaDoc<PdfReference,Object JavaDoc> cache = new java.util.Hashtable JavaDoc<PdfReference,Object JavaDoc>();
63   // </fields>
64

65   // <constructors>
66
/**
67     <h3>Remarks</h3>
68     <p>For internal use only.</p>
69   */

70   public Document(
71     File context
72     )
73   {
74     super(
75       context,
76       new PdfDictionary(
77         new PdfName[]{PdfName.Type},
78         new PdfDirectObject[]{PdfName.Catalog}
79         ) // Document catalog [PDF:1.6:3.6.1].
80
);
81
82     /*
83       NOTE: Here is just the minimal initialization.
84       Any further customization is upon client's responsibility.
85     */

86     // Link the document to the file!
87
// NOTE: Attach a catalog reference to the file trailer.
88
context.getTrailer().put(PdfName.Root,getBaseObject());
89
90     // Initialize the pages collection (page-tree root node)!
91
/*
92       NOTE: The page-tree root node is required [PDF:1.6:3.6.1].
93     */

94     setPages(new Pages(this));
95   }
96
97   /**
98     <h3>Remarks</h3>
99     <p>For internal use only.</p>
100   */

101   public Document(
102     PdfDirectObject baseObject // Catalog.
103
)
104   {
105     super(
106       baseObject,
107       null // NO container (catalog MUST be an indirect object [PDF:1.6:3.4.4]).
108
);
109   }
110   // </constructors>
111

112   // <interface>
113
// <public>
114
public Object JavaDoc clone(
115     Document context
116     )
117   {throw new NotImplementedException();}
118
119   /**
120     Forces the object to belong to the document context.
121   */

122   public Object JavaDoc contextualize(
123     PdfObjectWrapper<?> object
124     )
125   {
126     if(object.getFile() == getFile())
127       return object;
128
129     return object.clone(this);
130   }
131
132   /**
133     Forces the collection's objects to belong to the document context.
134   */

135   public Collection JavaDoc<? extends PdfObjectWrapper<?>> contextualize(
136     Collection JavaDoc<? extends PdfObjectWrapper<?>> objects
137     )
138   {
139     ArrayList JavaDoc<PdfObjectWrapper<?>> contextualizedObjects = new ArrayList JavaDoc<PdfObjectWrapper<?>>(objects.size());
140     for(PdfObjectWrapper<?> object : objects)
141     {contextualizedObjects.add((PdfObjectWrapper<?>)contextualize(object));}
142
143     return contextualizedObjects;
144   }
145
146   /**
147     Forces the object to be dropped from the document context.
148   */

149   public void decontextualize(
150     PdfObjectWrapper<?> object
151     )
152   {
153     if(object.getFile() != getFile())
154       return;
155
156     object.delete();
157   }
158
159   /**
160     Forces the collection's objects to be dropped from the document context.
161   */

162   public void decontextualize(
163     Collection JavaDoc<? extends PdfObjectWrapper<?>> objects
164     )
165   {
166     for(PdfObjectWrapper<?> object : objects)
167     {decontextualize(object);}
168   }
169
170   public Bookmarks getBookmarks(
171     )
172   {
173     PdfDirectObject bookmarks = getBaseDataObject().get(PdfName.Outlines);
174     if(bookmarks == null)
175       return null;
176     else
177       return new Bookmarks(bookmarks);
178   }
179
180   public Information getInformation(
181     )
182   {
183     PdfDirectObject info = getFile().getTrailer().get(PdfName.Info);
184     if(info == null)
185       return null;
186     else
187       return new Information(info);
188   }
189
190   public double getPageHeight(
191     )
192   {
193     /*
194       NOTE: Due to the contract, we cannot force the existence of the default
195       media box at document level.
196     */

197     PdfDirectObject box = getMediaBox();
198     if(box == null)
199       return 0;
200
201     return ((IPdfNumber)((PdfArray)File.resolve(box)).get(3)).getNumberValue();
202   }
203
204   public PageLayoutEnum getPageLayout(
205     )
206   {
207     PdfName value = (PdfName)getBaseDataObject().get(PdfName.PageLayout);
208     if(value.equals(PdfName.OneColumn))
209       return PageLayoutEnum.OneColumn;
210     else if(value.equals(PdfName.TwoColumnLeft))
211       return PageLayoutEnum.TwoColumns;
212     else
213       return PageLayoutEnum.SinglePage;
214   }
215
216   public PageModeEnum getPageMode(
217     )
218   {
219     PdfName value = (PdfName)getBaseDataObject().get(PdfName.PageMode);
220     if(value.equals(PdfName.UseOutlines))
221       return PageModeEnum.Outlines;
222     else if(value.equals(PdfName.UseThumbs))
223       return PageModeEnum.Thumbnails;
224     else if(value.equals(PdfName.FullScreen))
225       return PageModeEnum.FullScreen;
226     else
227       return PageModeEnum.Simple;
228   }
229
230   public Pages getPages(
231     )
232   {return new Pages(getBaseDataObject().get(PdfName.Pages));}
233
234   public double getPageWidth(
235     )
236   {
237     /*
238       NOTE: Due to the contract, we cannot force the existence of the default
239       media box at document level.
240     */

241     PdfDirectObject box = getMediaBox();
242     if(box == null)
243       return 0;
244
245     return ((IPdfNumber)((PdfArray)File.resolve(box)).get(2)).getNumberValue();
246   }
247
248   public Resources getResources(
249     )
250   {
251     PdfReference pages = (PdfReference)getBaseDataObject().get(PdfName.Pages);
252     PdfDirectObject resources = ((PdfDictionary)File.resolve(pages)).get(PdfName.Resources);
253     if(resources == null)
254       return null;
255     else
256       return new Resources(
257         resources,
258         pages.getIndirectObject()
259         );
260   }
261
262   public ViewerPreferences getViewerPreferences(
263     )
264   {
265     PdfDirectObject viewerPreferences = getBaseDataObject().get(PdfName.ViewerPreferences);
266     if(viewerPreferences == null)
267       return null;
268     else
269       return new ViewerPreferences(
270         viewerPreferences,
271         ((PdfReference)getBaseObject()).getIndirectObject()
272         );
273   }
274
275   public void setBookmarks(
276     Bookmarks value
277     )
278   {getBaseDataObject().put(PdfName.Outlines,value.getBaseObject());}
279
280   public void setInformation(
281     Information value
282     )
283   {getFile().getTrailer().put(PdfName.Info,value.getBaseObject());}
284
285   public void setPageHeight(
286     double value
287     )
288   {ensureMediaBoxData().set(3,new PdfReal(value));}
289
290   public void setPageLayout(
291     PageLayoutEnum value
292     )
293   {
294     switch(value)
295     {
296       case SinglePage:
297         getBaseDataObject().put(PdfName.PageLayout,PdfName.SinglePage);
298         break;
299       case OneColumn:
300         getBaseDataObject().put(PdfName.PageLayout,PdfName.OneColumn);
301         break;
302       case TwoColumns:
303         getBaseDataObject().put(PdfName.PageLayout,PdfName.TwoColumnLeft);
304         break;
305     }
306   }
307
308   public void setPageMode(
309     PageModeEnum value
310     )
311   {
312     switch(value)
313     {
314       case Simple:
315         getBaseDataObject().put(PdfName.PageMode,PdfName.UseNone);
316         break;
317       case Outlines:
318         getBaseDataObject().put(PdfName.PageMode,PdfName.UseOutlines);
319         break;
320       case Thumbnails:
321         getBaseDataObject().put(PdfName.PageMode,PdfName.UseThumbs);
322         break;
323       case FullScreen:
324         getBaseDataObject().put(PdfName.PageMode,PdfName.FullScreen);
325         break;
326     }
327   }
328
329   public void setPages(
330     Pages value
331     )
332   {getBaseDataObject().put(PdfName.Pages,value.getBaseObject());}
333
334   public void setPageSize(
335     Dimension2D JavaDoc size
336     )
337   {setPageSize(size.getWidth(),size.getHeight());}
338
339   public void setPageSize(
340     double width,
341     double height
342     )
343   {
344     PdfArray mediaBox = ensureMediaBoxData();
345     mediaBox.set(2,new PdfReal(width));
346     mediaBox.set(3,new PdfReal(height));
347   }
348
349   public void setPageWidth(
350     double value
351     )
352   {ensureMediaBoxData().set(2,new PdfReal(value));}
353
354   public void setResources(
355     Resources value
356     )
357   {
358     PdfReference pages = (PdfReference)getBaseDataObject().get(PdfName.Pages);
359     ((PdfDictionary)File.resolve(pages)).put(
360       PdfName.Resources,
361       value.getBaseObject()
362       );
363     value.setContainer(
364       pages.getIndirectObject()
365       ); // Resources object could be directly inside a container.
366
}
367
368   public void setViewerPreferences(
369     ViewerPreferences value
370     )
371   {
372     getBaseDataObject().put(
373       PdfName.ViewerPreferences,
374       value.getBaseObject()
375       );
376     value.setContainer(
377       ((PdfReference)getBaseObject()).getIndirectObject()
378       ); // ViewerPreferences object could be directly inside a container.
379
}
380   // </public>
381

382   // <internal>
383
/**
384     Gets the document media box base object ensuring its existence.
385   */

386   PdfDirectObject ensureMediaBox(
387     )
388   {
389     PdfDirectObject mediaBox = getMediaBox();
390     if(mediaBox == null)
391     {
392       // Create default media box (A4)!
393
//TODO:IMPL if the document has already had some pages, it'd be better to retrieve media
394
//box from the first page, in order to improve consistency!!!
395
mediaBox = getFile().getIndirectObjects().add(
396         new PdfArray(
397           new PdfInteger[]
398           {
399             new PdfInteger(0),
400             new PdfInteger(0),
401             new PdfInteger(595),
402             new PdfInteger(842)
403           }
404           )
405         ).getReference();
406       // Assign the media box to the document!
407
setMediaBox(mediaBox);
408     }
409
410     return mediaBox;
411   }
412
413   /**
414     Gets the document media box base data object ensuring its existence.
415   */

416   PdfArray ensureMediaBoxData(
417     )
418   {
419     return (PdfArray)File.resolve(ensureMediaBox());
420   }
421
422   /**
423     Gets the document media box.
424   */

425   PdfDirectObject getMediaBox(
426     )
427   {
428     /*
429       NOTE: Document media box MUST be associated with the page-tree root node
430       in order to be inheritable by all the pages.
431     */

432     return ((PdfDictionary)File.resolve(
433       getBaseDataObject().get(PdfName.Pages)
434       )).get(PdfName.MediaBox);
435   }
436
437   void setMediaBox(
438     PdfDirectObject value
439     )
440   {
441     ((PdfDictionary)File.resolve(
442       getBaseDataObject().get(PdfName.Pages)
443       )).put(PdfName.MediaBox,value);
444   }
445   // </internal>
446
// </interface>
447
// </dynamic>
448
// </class>
449
}
Popular Tags