1 50 51 package com.lowagie.text; 52 53 import java.io.*; 54 import java.net.*; 55 import com.lowagie.text.pdf.*; 56 import com.lowagie.text.pdf.codec.postscript.*; 57 import java.util.StringTokenizer; 58 59 66 67 public class ImgPostscript 68 extends Image 69 implements Element { 70 71 73 ImgPostscript(Image image) { 74 super(image); 75 } 76 77 84 85 public ImgPostscript(URL url) throws BadElementException, IOException { 86 super(url); 87 processParameters(); 88 } 89 90 98 99 public ImgPostscript(String filename) throws BadElementException, 100 MalformedURLException, IOException { 101 this(Image.toURL(filename)); 102 } 103 104 111 112 public ImgPostscript(byte[] img) throws BadElementException, IOException { 113 super( (URL)null); 114 rawData = img; 115 originalData = img; 116 processParameters(); 117 } 118 119 124 125 private void processParameters() throws BadElementException, IOException { 126 type = IMGTEMPLATE; 127 originalType = ORIGINAL_PS; 128 InputStream is = null; 129 try { 130 String errorID; 131 if (rawData == null) { 132 is = url.openStream(); 133 errorID = url.toString(); 134 } 135 else { 136 is = new java.io.ByteArrayInputStream(rawData); 137 errorID = "Byte array"; 138 } 139 String boundingbox=null; 140 Reader r = new BufferedReader(new InputStreamReader(is)); 141 while (r.ready()) { 143 char c; 144 StringBuffer sb = new StringBuffer(); 145 while ( (c = ( (char) r.read())) != '\n') { 146 sb.append(c); 147 } 148 if (sb.toString().startsWith("%%BoundingBox:")) { 150 boundingbox = sb.toString(); 151 152 } 153 if (sb.toString().startsWith("%%TemplateBox:")) { 154 boundingbox = sb.toString(); 155 } 156 if (sb.toString().startsWith("%%EndComments")) { 157 break; 158 } 159 160 } 161 if(boundingbox==null)return; 162 StringTokenizer st=new StringTokenizer(boundingbox,": \r\n"); 163 st.nextElement(); 164 String xx1=st.nextToken(); 165 String yy1=st.nextToken(); 166 String xx2=st.nextToken(); 167 String yy2=st.nextToken(); 168 169 int left = Integer.parseInt(xx1); 170 int top = Integer.parseInt(yy1); 171 int right = Integer.parseInt(xx2); 172 int bottom = Integer.parseInt(yy2); 173 int inch = 1; 174 dpiX = 72; 175 dpiY = 72; 176 scaledHeight = (float) (bottom - top) / inch *1f; 177 scaledHeight=800; 178 setTop(scaledHeight); 179 scaledWidth = (float) (right - left) / inch * 1f; 180 scaledWidth=800; 181 setRight(scaledWidth); 182 } 183 finally { 184 if (is != null) { 185 is.close(); 186 } 187 plainWidth = width(); 188 plainHeight = height(); 189 } 190 } 191 192 197 public void readPostscript(PdfTemplate template) throws IOException, 198 DocumentException { 199 setTemplateData(template); 200 template.setWidth(width()); 201 template.setHeight(height()); 202 InputStream is = null; 203 try { 204 if (rawData == null) { 205 is = url.openStream(); 206 } 207 else { 208 is = new java.io.ByteArrayInputStream(rawData); 209 } 210 MetaDoPS meta = new MetaDoPS(is, template); 211 meta.readAll(); 212 } 213 finally { 214 if (is != null) { 215 is.close(); 216 } 217 } 218 } 219 } 220 | Popular Tags |