KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > stefanochizzolini > clown > documents > contents > xObjects > FormXObject


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.contents.xObjects;
28
29 import it.stefanochizzolini.clown.documents.contents.ContentStream;
30 import it.stefanochizzolini.clown.documents.contents.IContentStreamContext;
31 import it.stefanochizzolini.clown.documents.contents.Resources;
32 import it.stefanochizzolini.clown.documents.Document;
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.PdfName;
39 import it.stefanochizzolini.clown.objects.PdfRectangle;
40 import it.stefanochizzolini.clown.objects.PdfReference;
41 import it.stefanochizzolini.clown.util.NotImplementedException;
42
43 /**
44   Form external object [PDF:1.6:4.9].
45 */

46 public class FormXObject
47   extends XObject
48   implements IContentStreamContext
49 {
50   // <class>
51
// <dynamic>
52
// <constructors>
53
public FormXObject(
54     Document context
55     )
56   {
57     super(context);
58
59     PdfDictionary header = getBaseDataObject().getHeader();
60     header.put(
61       PdfName.Subtype,
62       PdfName.Form
63       );
64     header.put(
65       PdfName.BBox,
66       new PdfRectangle(0,0,0,0)
67       );
68
69     Resources resources = context.getResources();
70     if(resources != null)
71       header.put(
72         PdfName.Resources,
73         resources.getBaseObject()
74         );
75   }
76
77   /**
78     <h3>Remarks</h3>
79     <p>For internal use only.</p>
80   */

81   public FormXObject(
82     PdfDirectObject baseObject
83     )
84   {super(baseObject);}
85   // </constructors>
86

87   // <interface>
88
// <public>
89
public Object JavaDoc clone(
90     Document context
91     )
92   {throw new NotImplementedException();}
93
94   /**
95     Gets the content stream associated to the form.
96   */

97   public ContentStream getContent(
98     )
99   {
100     return new ContentStream(
101       getBaseObject(),
102       this
103       );
104   }
105
106   public double getHeight(
107     )
108   {
109     return ((IPdfNumber)File.resolve(
110       ((PdfArray)File.resolve(
111         getBaseDataObject().getHeader().get(PdfName.BBox)
112         )).get(3)
113       )).getNumberValue();
114   }
115
116   /**
117     Gets the resources associated to the form.
118   */

119   public Resources getResources(
120     )
121   {
122     return new Resources(
123       getBaseDataObject().getHeader().get(PdfName.Resources),
124       ((PdfReference)getBaseObject()).getIndirectObject()
125       );
126   }
127
128   public double getWidth(
129     )
130   {
131     return ((IPdfNumber)File.resolve(
132       ((PdfArray)File.resolve(
133         getBaseDataObject().getHeader().get(PdfName.BBox)
134         )).get(2)
135       )).getNumberValue();
136   }
137
138   public void setHeight(
139     double value
140     )
141   {
142     // 1. Instantiate.
143
PdfDirectObject box = getBaseDataObject().getHeader().get(PdfName.BBox);
144     PdfDirectObject height = ((PdfArray)File.resolve(box)).get(3);
145
146     // 2. Change.
147
((IPdfNumber)File.resolve(height)).setNumberValue(value);
148
149     // 3. Update.
150
if(File.update(height))
151       return;
152     File.update(box);
153   }
154
155   /**
156     Sets the resources associated to the form.
157   */

158   public void setResources(
159     Resources value
160     )
161   {
162     getBaseDataObject().getHeader().put(
163       PdfName.Resources,
164       value.getBaseObject()
165       );
166   }
167
168   public void setWidth(
169     double value
170     )
171   {
172     // 1. Instantiate.
173
PdfDirectObject box = getBaseDataObject().getHeader().get(PdfName.BBox);
174     PdfDirectObject width = ((PdfArray)File.resolve(box)).get(2);
175
176     // 2. Change.
177
((IPdfNumber)File.resolve(width)).setNumberValue(value);
178
179     // 3. Update.
180
if(File.update(width))
181       return;
182     File.update(box);
183   }
184   // </public>
185

186   // <internal>
187
// <IContentStreamContext>
188
public PdfArray getContextBox(
189     )
190   {
191     return (PdfArray)File.resolve(getBaseDataObject().getHeader().get(PdfName.BBox)); // Required [PDF:1.6:4.9.1].
192
}
193
194   public PdfDictionary getContextResources(
195     )
196   {
197     PdfDirectObject resources = getBaseDataObject().getHeader().get(PdfName.Resources);
198     if(resources == null)
199       throw new NotImplementedException("Non-independent form XObjects [PDF:1.1-] are NOT supported due to their stupid messy dependency coupling (see [PDF:1.6:4.9.1]).");
200
201     return (PdfDictionary)File.resolve(resources);
202   }
203   // </IContentStreamContext>
204
// </internal>
205
// </interface>
206
// </dynamic>
207
// </class>
208
}
Popular Tags