1 31 package org.pdfbox.pdmodel.graphics.xobject; 32 33 import java.io.IOException ; 34 import java.util.List ; 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 53 public abstract class PDXObject implements COSObjectable 54 { 55 private PDStream xobject; 56 57 62 public PDXObject(COSStream xobj) 63 { 64 xobject = new PDStream( xobj ); 65 } 66 67 72 public PDXObject(PDStream xobj) 73 { 74 xobject = xobj; 75 } 76 77 82 public PDXObject(PDDocument doc) 83 { 84 xobject = new PDStream(doc); 85 xobject.getStream().setName( COSName.TYPE, "XObject" ); 86 } 87 88 93 public COSBase getCOSObject() 94 { 95 return xobject.getCOSObject(); 96 } 97 98 102 public COSStream getCOSStream() 103 { 104 return xobject.getStream(); 105 } 106 107 111 public PDStream getPDStream() 112 { 113 return xobject; 114 } 115 116 124 public static PDXObject createXObject( COSBase xobject ) throws IOException 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 subtype = xstream.getNameAsString( "Subtype" ); 135 if( subtype.equals( PDXObjectImage.SUB_TYPE ) ) 136 { 137 PDStream image = new PDStream( xstream ); 138 List 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 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 ( "Unknown xobject subtype '" + subtype + "'" ); 172 } 173 } 174 else 175 { 176 throw new IOException ( "Unknown xobject type:" + xobject.getClass().getName() ); 177 } 178 179 return retval; 180 } 181 182 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 204 public void setMetadata( PDMetadata meta ) 205 { 206 xobject.getStream().setItem( "Metadata", meta ); 207 } 208 } 209 | Popular Tags |