KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > graphics > xobject > PDXObject


1 /**
2  * Copyright (c) 2005, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.pdmodel.graphics.xobject;
32
33 import java.io.IOException JavaDoc;
34 import java.util.List JavaDoc;
35
36 import org.pdfbox.cos.COSBase;
37 import org.pdfbox.cos.COSName;
38 import org.pdfbox.cos.COSStream;
39
40 import org.pdfbox.pdmodel.PDDocument;
41 import org.pdfbox.pdmodel.common.COSObjectable;
42 import org.pdfbox.pdmodel.common.PDMetadata;
43 import org.pdfbox.pdmodel.common.PDStream;
44
45 /**
46  * The base class for all XObjects in the PDF document.
47  *
48  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
49  * @author mathiak
50  * @author Marcel Kammer
51  * @version $Revision: 1.13 $
52  */

53 public abstract class PDXObject implements COSObjectable
54 {
55     private PDStream xobject;
56     
57     /**
58      * Standard constuctor.
59      *
60      * @param xobj The XObject dictionary.
61      */

62     public PDXObject(COSStream xobj)
63     {
64         xobject = new PDStream( xobj );
65     }
66     
67     /**
68      * Standard constuctor.
69      *
70      * @param xobj The XObject dictionary.
71      */

72     public PDXObject(PDStream xobj)
73     {
74         xobject = xobj;
75     }
76     
77     /**
78      * Standard constuctor.
79      *
80      * @param doc The doc to store the object contents.
81      */

82     public PDXObject(PDDocument doc)
83     {
84         xobject = new PDStream(doc);
85         xobject.getStream().setName( COSName.TYPE, "XObject" );
86     }
87     
88     /**
89      * Returns the stream.
90      *
91      * {@inheritDoc}
92      */

93     public COSBase getCOSObject()
94     {
95         return xobject.getCOSObject();
96     }
97     
98     /**
99      * Returns the stream.
100      * @return The stream for this object.
101      */

102     public COSStream getCOSStream()
103     {
104         return xobject.getStream();
105     }
106     
107     /**
108      * Returns the stream.
109      * @return The stream for this object.
110      */

111     public PDStream getPDStream()
112     {
113         return xobject;
114     }
115     
116     /**
117      * Create the correct xobject from the cos base.
118      *
119      * @param xobject The cos level xobject to create.
120      *
121      * @return a pdmodel xobject
122      * @throws IOException If there is an error creating the xobject.
123      */

124     public static PDXObject createXObject( COSBase xobject ) throws IOException JavaDoc
125     {
126         PDXObject retval = null;
127         if( xobject == null )
128         {
129             retval = null;
130         }
131         else if( xobject instanceof COSStream )
132         {
133             COSStream xstream = (COSStream)xobject;
134             String JavaDoc subtype = xstream.getNameAsString( "Subtype" );
135             if( subtype.equals( PDXObjectImage.SUB_TYPE ) )
136             {
137                 PDStream image = new PDStream( xstream );
138                 // See if filters are DCT or JPX otherwise treat as Bitmap-like
139
// There might be a problem with several filters, but that's ToDo until
140
// I find an example
141
List JavaDoc filters = image.getFilters();
142                 if( filters != null && filters.contains( COSName.DCT_DECODE.getName() ) )
143                 {
144                     return new PDJpeg(image);
145                 }
146                 else if ( filters != null && filters.contains( COSName.CCITTFAX_DECODE.getName() ) )
147                 {
148                     return new PDCcitt(image);
149                 }
150                 else if( filters != null && filters.contains(COSName.JPX_DECODE.getName()))
151                 {
152                     //throw new IOException( "JPXDecode has not been implemented for images" );
153
//JPX Decode is not really supported right now, but if we are just doing
154
//text extraction then we don't want to throw an exception, so for now
155
//just return a PDPixelMap, which will break later on if it is
156
//actually used, but for text extraction it is not used.
157
return new PDPixelMap( image );
158                     
159                 }
160                 else
161                 {
162                     retval = new PDPixelMap(image);
163                 }
164             }
165             else if( subtype.equals( PDXObjectForm.SUB_TYPE ) )
166             {
167                 retval = new PDXObjectForm( xstream );
168             }
169             else
170             {
171                 throw new IOException JavaDoc( "Unknown xobject subtype '" + subtype + "'" );
172             }
173         }
174         else
175         {
176             throw new IOException JavaDoc( "Unknown xobject type:" + xobject.getClass().getName() );
177         }
178         
179         return retval;
180     }
181     
182     /**
183      * Get the metadata that is part of the document catalog. This will
184      * return null if there is no meta data for this object.
185      *
186      * @return The metadata for this object.
187      */

188     public PDMetadata getMetadata()
189     {
190         PDMetadata retval = null;
191         COSStream mdStream = (COSStream)xobject.getStream().getDictionaryObject( "Metadata" );
192         if( mdStream != null )
193         {
194             retval = new PDMetadata( mdStream );
195         }
196         return retval;
197     }
198     
199     /**
200      * Set the metadata for this object. This can be null.
201      *
202      * @param meta The meta data for this object.
203      */

204     public void setMetadata( PDMetadata meta )
205     {
206         xobject.getStream().setItem( "Metadata", meta );
207     }
208 }
209
Popular Tags