KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > etymon > pjx > util > AddImageSimple


1 package com.etymon.pjx.util;
2
3 import java.io.*;
4 import java.nio.*;
5 import java.nio.channels.*;
6 import java.util.*;
7 import com.etymon.pjx.*;
8
9 /**
10    Provides a very rudimentary function for adding JPEG images to a
11    PDF document. This works with most JPEG images.
12    @author Nassib Nassar
13    @deprecated This class will be removed when a more comprehensive
14    image class is completed.
15 */

16 public class AddImageSimple {
17
18     protected static final PdfName PDFNAME_CONTENTS = new PdfName("Contents");
19     protected static final PdfName PDFNAME_RESOURCES = new PdfName("Resources");
20     protected static final PdfName PDFNAME_PROCSET = new PdfName("ProcSet");
21     protected static final PdfName PDFNAME_IMAGEC = new PdfName("ImageC");
22     protected static final PdfName PDFNAME_PARENT = new PdfName("Parent");
23     protected static final PdfName PDFNAME_NAME = new PdfName("Name");
24     protected static final PdfName PDFNAME_TYPE = new PdfName("Type");
25     protected static final PdfName PDFNAME_XOBJECT = new PdfName("XObject");
26     protected static final PdfName PDFNAME_SUBTYPE = new PdfName("Subtype");
27     protected static final PdfName PDFNAME_IMAGE = new PdfName("Image");
28     protected static final PdfName PDFNAME_FILTER = new PdfName("Filter");
29     protected static final PdfName PDFNAME_DCTDECODE = new PdfName("DCTDecode");
30     protected static final PdfName PDFNAME_WIDTH = new PdfName("Width");
31     protected static final PdfName PDFNAME_HEIGHT = new PdfName("Height");
32     protected static final PdfName PDFNAME_BITSPERCOMPONENT = new PdfName("BitsPerComponent");
33     protected static final PdfName PDFNAME_COLORSPACE = new PdfName("ColorSpace");
34     protected static final PdfName PDFNAME_DEVICERGB = new PdfName("DeviceRGB");
35     protected static final PdfInteger PDFINTEGER_8 = new PdfInteger(8);
36
37     /**
38        The manager associated with this document.
39     */

40     protected PdfManager _m;
41
42     /**
43        Constructs an <code>AddImageSimple</code> instance based on
44        a specified <code>PdfManager</code>.
45      */

46     public AddImageSimple(PdfManager manager) {
47
48         _m = manager;
49         
50     }
51
52     /**
53        Adds a specified JPEG image (contained in a file) to a page
54        in the PDF document. This method requires the calling
55        method to specify the original size of the image.
56        @param imageFile the file containing the JPEG image.
57        @param imageWidth the original width of the image.
58        @param imageHeight the original height of the image.
59        @param imageName a name object to associate with the image,
60        for identification purposes.
61        @param page an indirect reference to the page dictionary
62        object that the image will be added to.
63        @param positionX the X location to position the image at.
64        @param positionY the Y location to position the image at.
65        @param scaleX a scaling factor in the X dimension.
66        @param scaleY a scaling factor in the Y dimension.
67        @param background if <code>true</code>, the image will be
68        layered behind the existing page contents rather than on
69        top of it. This can be used to create simple watermarks.
70        @throws PdfFormatException
71      */

72     public void addImage(File imageFile, int imageWidth, int imageHeight,
73                  PdfName imageName,
74                  PdfReference page,
75                  float positionX, float positionY,
76                  float scaleX, float scaleY,
77                  boolean background) throws IOException, PdfFormatException {
78         synchronized (this) {
79             synchronized (_m) {
80
81                 FileInputStream fis = new FileInputStream(imageFile);
82                 FileChannel fc = fis.getChannel();
83                 ByteBuffer bb = ByteBuffer.allocateDirect((int)fc.size());
84                 fc.read(bb);
85                 fc.close();
86                 fis.close();
87                 bb.position(0);
88
89                 addImage(bb, imageWidth, imageHeight,
90                      imageName,
91                      page,
92                      positionX, positionY,
93                      scaleX, scaleY,
94                      background);
95                 
96             }
97         }
98     }
99     
100     /**
101        Adds a specified JPEG image (contained in a buffer) to a
102        page in the PDF document. This method requires the calling
103        method to specify the original size of the image.
104        @param image the buffer containing the JPEG image. This
105        method reads from the current position until there are no
106        more bytes remaining.
107        @param imageWidth the original width of the image.
108        @param imageHeight the original height of the image.
109        @param imageName a name object to associate with the image,
110        for identification purposes.
111        @param page an indirect reference to the page dictionary
112        object that the image will be added to.
113        @param positionX the X location to position the image at.
114        @param positionY the Y location to position the image at.
115        @param scaleX a scaling factor in the X dimension.
116        @param scaleY a scaling factor in the Y dimension.
117        @param background if <code>true</code>, the image will be
118        layered behind the existing page contents rather than on
119        top of it. This can be used to create simple watermarks.
120        @throws PdfFormatException
121      */

122     public void addImage(ByteBuffer image, int imageWidth, int imageHeight,
123                  PdfName imageName,
124                  PdfReference page,
125                  float positionX, float positionY,
126                  float scaleX, float scaleY,
127                  boolean background) throws IOException, PdfFormatException {
128         synchronized (this) {
129             synchronized (_m) {
130
131                 PdfPageTree pageTree = new PdfPageTree(_m);
132                 
133                 // get the page dictionary
134
Object JavaDoc obj = _m.getObjectIndirect(page);
135                 if ( !(obj instanceof PdfDictionary) ) {
136                     throw new PdfFormatException(
137                         "Page object is not a dictionary.");
138                 }
139                 PdfDictionary pageExplicit = pageTree.inheritAttributes( (PdfDictionary)obj );
140                 Map pageMap = new HashMap(pageExplicit.getMap());
141                 
142                 // create the image XObject
143
Map imageMap = new HashMap(9);
144                 imageMap.put(PDFNAME_NAME, imageName);
145                 imageMap.put(PDFNAME_TYPE, PDFNAME_XOBJECT);
146                 imageMap.put(PDFNAME_SUBTYPE, PDFNAME_IMAGE);
147                 imageMap.put(PDFNAME_FILTER, PDFNAME_DCTDECODE);
148                 imageMap.put(PDFNAME_WIDTH, new PdfInteger(imageWidth));
149                 imageMap.put(PDFNAME_HEIGHT, new PdfInteger(imageHeight));
150                 imageMap.put(PDFNAME_BITSPERCOMPONENT, PDFINTEGER_8);
151                 imageMap.put(PDFNAME_COLORSPACE, PDFNAME_DEVICERGB);
152                 PdfStream imageStream = new PdfStream(new PdfDictionary(imageMap), image);
153
154                 // add the image XObject to the
155
// document
156
int imageId = _m.addObject(imageStream);
157
158                 // create a tiny contents stream with
159
// only "q" to save the state. this
160
// will go before all streams on the
161
// page, to ensure that we can get a
162
// clean coordinate matrix
163
String JavaDoc tinyString = "q\n";
164                 byte[] tinyBytes = tinyString.getBytes();
165                 ByteBuffer tinyBuffer = ByteBuffer.wrap(tinyBytes);
166                 PdfStream tinyStream = new PdfStream(new PdfDictionary(new HashMap()),
167                                      tinyBuffer);
168                 
169                 // create the image contents stream
170
float sX = scaleX * imageWidth;
171                 float sY = scaleY * imageHeight;
172                 String JavaDoc contentsString = "Q q " +
173                     sX + " 0 0 " + sY + ' ' + positionX + ' ' + positionY + " cm " +
174                     imageName + " Do Q\n";
175                 byte[] contentsBytes = contentsString.getBytes();
176                 ByteBuffer contentsBuffer = ByteBuffer.wrap(contentsBytes);
177                 PdfStream contentsStream = new PdfStream(new PdfDictionary(new HashMap()),
178                                      contentsBuffer);
179
180                 // add the contents streams to the
181
// document
182
int tinyId = _m.addObject(tinyStream);
183                 int contentsId = _m.addObject(contentsStream);
184
185                 // add references to the contents
186
// streams to the page dictionary
187
obj = pageMap.get(PDFNAME_CONTENTS);
188                 List contentList;
189                 if (obj instanceof PdfArray) {
190                     contentList = new ArrayList( ((PdfArray)obj).getList() );
191                 }
192                 else if (obj instanceof PdfReference) {
193                     contentList = new ArrayList();
194                     contentList.add(obj);
195                 }
196                 else {
197                     throw new PdfFormatException(
198                         "Page content stream is not an array or indirect reference.");
199                 }
200                 contentList.add( 0, new PdfReference(tinyId, 0) );
201                 if (background) {
202                     contentList.add( 1, new PdfReference(contentsId, 0) );
203                 } else {
204                     contentList.add( new PdfReference(contentsId, 0) );
205                 }
206                 pageMap.put(PDFNAME_CONTENTS, new PdfArray(contentList) );
207
208                 // get the resources dictionary
209
obj = pageMap.get(PDFNAME_RESOURCES);
210                 if (obj == null) {
211                     throw new PdfFormatException(
212                         "Resources dictionary is missing from page.");
213                 }
214                 if ( !(obj instanceof PdfObject) ) {
215                     throw new PdfFormatException(
216                         "Resources dictionary is not a PDF object.");
217                 }
218                 obj = _m.getObjectIndirect((PdfObject)obj);
219                 if ( !(obj instanceof PdfDictionary) ) {
220                     throw new PdfFormatException(
221                         "Resources object is not a dictionary.");
222                 }
223                 Map resourcesMap = new HashMap( ((PdfDictionary)obj).getMap() );
224
225                 // get the procedure set and add ImageC
226
obj = resourcesMap.get(PDFNAME_PROCSET);
227                 Set procsetSet;
228                 if (obj == null) {
229                     procsetSet = new HashSet(1);
230                 } else {
231                     if ( !(obj instanceof PdfObject) ) {
232                         throw new PdfFormatException(
233                             "Procedure set is not a PDF object.");
234                     }
235                     obj = _m.getObjectIndirect((PdfObject)obj);
236                     if ( !(obj instanceof PdfArray) ) {
237                         throw new PdfFormatException(
238                             "Procedure set is not an array.");
239                     }
240                     procsetSet = new HashSet( ((PdfArray)obj).getList() );
241                 }
242                 procsetSet.add(PDFNAME_IMAGEC);
243                 resourcesMap.put(PDFNAME_PROCSET,
244                          new PdfArray( new ArrayList(procsetSet) ) );
245
246                 // get the XObject dictionary, or
247
// create one
248
Map xobjectMap;
249                 obj = resourcesMap.get(PDFNAME_XOBJECT);
250                 if (obj == null) {
251                     xobjectMap = new HashMap();
252                 } else {
253                     if ( !(obj instanceof PdfObject) ) {
254                         throw new PdfFormatException(
255                             "XObject is not a PDF object.");
256                     }
257                     obj = _m.getObjectIndirect((PdfObject)obj);
258                     if ( !(obj instanceof PdfDictionary) ) {
259                         throw new PdfFormatException(
260                             "XObject is not a dictionary.");
261                     }
262                     xobjectMap = new HashMap( ((PdfDictionary)obj).getMap() );
263                 }
264                 xobjectMap.put(imageName, new PdfReference(imageId, 0));
265                 resourcesMap.put(PDFNAME_XOBJECT, new PdfDictionary(xobjectMap));
266
267                 // set the new resources dictionary
268
pageMap.put(PDFNAME_RESOURCES, new PdfDictionary(resourcesMap) );
269                 
270                 // update page dictionary
271
_m.setObject( new PdfDictionary(pageMap),
272                           page.getObjectNumber() );
273
274             }
275         }
276     }
277     
278 }
279
Popular Tags