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 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 40 protected PdfManager _m; 41 42 46 public AddImageSimple(PdfManager manager) { 47 48 _m = manager; 49 50 } 51 52 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 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 Object 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 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 int imageId = _m.addObject(imageStream); 157 158 String 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 float sX = scaleX * imageWidth; 171 float sY = scaleY * imageHeight; 172 String 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 int tinyId = _m.addObject(tinyStream); 183 int contentsId = _m.addObject(contentsStream); 184 185 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 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 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 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 pageMap.put(PDFNAME_RESOURCES, new PdfDictionary(resourcesMap) ); 269 270 _m.setObject( new PdfDictionary(pageMap), 272 page.getObjectNumber() ); 273 274 } 275 } 276 } 277 278 } 279 | Popular Tags |