| 1 4 5 package com.etymon.pj; 6 7 import java.util.*; 8 import java.io.*; 9 import java.nio.*; 10 import com.etymon.pj.exception.*; 11 import com.etymon.pj.object.*; 12 import com.etymon.pj.object.pagemark.*; 13 import com.etymon.pjx.*; 14 import com.etymon.pjx.util.*; 15 16 20 public class PjxConvert { 21 22 protected static final PdfName PDFNAME_LENGTH = new PdfName("Length"); 23 24 public static PjObject toPjObject(PdfObject obj) throws com.etymon.pj.exception.PdfFormatException { 25 26 if (obj instanceof PdfNull) { 27 return new PjNull(); 28 } 29 30 if (obj instanceof PdfBoolean) { 31 return new PjBoolean( ((PdfBoolean)obj).getBoolean() ); 32 } 33 34 if (obj instanceof PdfInteger) { 35 return new PjNumber( (float)((PdfInteger)obj).getInt() ); 36 } 37 38 if (obj instanceof PdfFloat) { 39 return new PjNumber( ((PdfFloat)obj).getFloat() ); 40 } 41 42 if (obj instanceof PdfString) { 43 return new PjString( ((PdfString)obj).getString() ); 44 } 45 46 if (obj instanceof PdfName) { 47 return new PjName( ((PdfName)obj).getString() ); 48 } 49 50 if (obj instanceof PdfArray) { 51 List list = ((PdfArray)obj).getList(); 52 Vector v = new Vector(); 53 for (Iterator it = list.iterator(); it.hasNext(); ) { 54 v.addElement( toPjObject((PdfObject)it.next()) ); 55 } 56 return new PjArray(v); 57 } 58 59 if (obj instanceof PdfDictionary) { 60 Map map = ((PdfDictionary)obj).getMap(); 61 Hashtable h = new Hashtable(); 62 for (Iterator it = map.keySet().iterator(); it.hasNext(); ) { 63 Object key = it.next(); 64 h.put(toPjObject((PdfObject)key), toPjObject((PdfObject)map.get(key)) ); 65 } 66 67 PjDictionary dictionary = new PjDictionary(h); 69 if (PjPage.isLike(dictionary)) { 70 return new PjPage(h); 71 } 72 else if (PjPages.isLike(dictionary)) { 73 return new PjPages(h); 74 } 75 else if (PjFontType1.isLike(dictionary)) { 76 return new PjFontType1(h); 77 } 78 else if (PjFontDescriptor.isLike(dictionary)) { 79 return new PjFontDescriptor(h); 80 } 81 else if (PjResources.isLike(dictionary)) { 82 return new PjResources(h); 83 } 84 else if (PjCatalog.isLike(dictionary)) { 85 return new PjCatalog(h); 86 } 87 else if (PjInfo.isLike(dictionary)) { 88 return new PjInfo(h); 89 } 90 else if (PjEncoding.isLike(dictionary)) { 91 return new PjEncoding(h); 92 } 93 else { 94 return dictionary; 95 } 96 } 97 98 if (obj instanceof PdfStream) { 99 PdfStream stream = (PdfStream)obj; 100 PjDictionary d = (PjDictionary)(toPjObject(stream.getDictionary())); 101 ByteBuffer streambuffer = stream.getBuffer(); 102 byte[] ba = new byte[streambuffer.capacity()]; 103 streambuffer.position(0); 104 streambuffer.limit(streambuffer.capacity()); 105 streambuffer.get(ba); 106 return new PjStream(new PjStreamDictionary(d.getHashtable()), ba); 107 } 108 109 if (obj instanceof PdfReference) { 110 PdfReference ref = (PdfReference)obj; 111 return new PjReference(new PjNumber(ref.getObjectNumber()), 112 new PjNumber(ref.getGenerationNumber()) ); 113 } 114 115 throw new com.etymon.pj.exception.PdfFormatException("Error converting object from PJX format."); 116 117 } 118 119 public static PdfObject toPjxObject(PjObject obj) throws com.etymon.pj.exception.PdfFormatException { 120 121 if (obj instanceof PjNull) { 122 return PdfNull.valueOf(); 123 } 124 125 if (obj instanceof PjBoolean) { 126 return PdfBoolean.valueOf( ((PjBoolean)obj).getBoolean() ); 127 } 128 129 if (obj instanceof PjNumber) { 130 PjNumber n = (PjNumber)obj; 131 float f = n.getFloat(); 132 int x = n.getInt(); 133 if (f == x) { 134 return new PdfInteger(x); 135 } else { 136 return new PdfFloat(f); 137 } 138 } 139 140 if (obj instanceof PjString) { 141 return new PdfString( ((PjString)obj).getString() ); 142 } 143 144 if (obj instanceof PjName) { 145 return new PdfName( ((PjName)obj).getString() ); 146 } 147 148 if (obj instanceof PjArray) { 149 Vector v = ((PjArray)obj).getVector(); 150 int vsize = v.size(); 151 ArrayList list = new ArrayList(vsize); 152 for (int x = 0; x < vsize; x++) { 153 list.add( toPjxObject((PjObject)v.elementAt(x)) ); 154 } 155 return new PdfArray(list); 156 } 157 158 if (obj instanceof PjDictionary) { 159 Hashtable h = ((PjDictionary)obj).getHashtable(); 160 HashMap map = new HashMap(h.size()); 161 for (Enumeration m = h.keys(); m.hasMoreElements();) { 162 PjObject key = (PjObject)m.nextElement(); 163 PjObject value = (PjObject)h.get(key); 164 map.put( toPjxObject(key), toPjxObject(value) ); 165 } 166 return new PdfDictionary(map); 167 } 168 169 if (obj instanceof PjStream) { 170 PjStream os = (PjStream)obj; 171 byte[] ob = os.getBuffer(); 172 ByteBuffer buffer = ByteBuffer.allocateDirect(ob.length); 173 buffer.put(ob); 174 Map map = ((PdfDictionary)toPjxObject(os.getStreamDictionary())).getMap(); 175 HashMap dictionary = new HashMap(map.size()); 176 dictionary.putAll(map); 177 dictionary.put(PDFNAME_LENGTH, new PdfInteger(ob.length)); 178 buffer.position(0); 179 return new PdfStream(new PdfDictionary(dictionary), buffer); 180 } 181 182 if (obj instanceof PjReference) { 183 PjReference r = (PjReference)obj; 184 return new PdfReference( r.getObjNumber().getInt(), 185 r.getGenNumber().getInt() ); 186 } 187 188 throw new com.etymon.pj.exception.PdfFormatException("Error converting object from PJ format."); 189 190 } 191 192 198 public static List pjxGetFields(PdfManager manager) throws IOException, com.etymon.pjx.PdfFormatException { 199 200 synchronized (manager) { 201 202 PdfModifier mod = new PdfModifier(manager); 203 204 ArrayList fieldList = new ArrayList(); 205 206 PdfDictionary catalog = mod.getCatalog(); 208 209 PdfDictionary acroForm; 211 try { 212 acroForm = (PdfDictionary)(manager.getObjectIndirect( 213 (PdfObject)catalog.getMap().get(new PdfName("AcroForm")) )); 214 } 215 catch (ClassCastException e) { 216 throw new com.etymon.pjx.PdfFormatException("AcroForm object is not a dictionary."); 217 } 218 219 if (acroForm == null) { 220 return fieldList; 221 } 222 223 226 PdfArray fields = (PdfArray)(acroForm.getMap().get(new PdfName("Fields"))); 228 if (fields == null) { 229 return fieldList; 230 } 231 List fieldsV = fields.getList(); 232 233 int fieldsV_n = fieldsV.size(); 235 for (int x = 0; x < fieldsV_n; x++) { 236 237 PdfReference fieldRef; 239 try { 240 fieldRef = (PdfReference)(fieldsV.get(x)); 241 } 242 catch (ClassCastException e) { 243 throw new com.etymon.pjx.PdfFormatException("Fields array element is not a reference."); 244 } 245 246 pjxGetFieldsAddField(manager, fieldList, fieldRef); 247 248 } 249 250 return fieldList; 251 } 252 } 253 254 257 private static void pjxGetFieldsAddField(PdfManager manager, ArrayList fieldList, PdfReference fieldRef) 258 throws IOException, com.etymon.pjx.PdfFormatException { 259 260 PdfDictionary field; 262 try { 263 field = (PdfDictionary)(manager.getObjectIndirect(fieldRef)); 264 } 265 catch (ClassCastException e) { 266 throw new com.etymon.pjx.PdfFormatException("Field object is not a dictionary."); 267 } 268 269 Map fieldHt = field.getMap(); 270 271 fieldList.add(field); 273 274 PdfArray kids; 276 try { 277 kids = (PdfArray)(manager.getObjectIndirect((PdfObject)(fieldHt.get(new PdfName("Kids"))))); 278 } 279 catch (ClassCastException e) { 280 throw new com.etymon.pjx.PdfFormatException("Kids object is not an array."); 281 } 282 283 if (kids != null) { 285 List kidsV = kids.getList(); 286 int kidsV_n = kidsV.size(); 287 for (int x = 0; x < kidsV_n; x++) { 288 289 PdfReference fieldRef2; 291 try { 292 fieldRef2 = (PdfReference)(kidsV.get(x)); 293 } 294 catch (ClassCastException e) { 295 throw new com.etymon.pjx.PdfFormatException("Kids array element is not a reference."); 296 } 297 298 pjxGetFieldsAddField(manager, fieldList, fieldRef2); 299 300 } 301 } 302 303 } 304 305 310 public static void pjxUpdateFieldValue(PdfManager manager, PdfDictionary origField, 311 PdfDictionary field, String value) 312 throws IOException, com.etymon.pjx.PdfFormatException { 313 314 synchronized (manager) { 315 316 try { 317 318 Map origFieldHtRO = origField.getMap(); 319 HashMap origFieldHt = new HashMap(origFieldHtRO.size()); 320 origFieldHt.putAll(origFieldHtRO); 321 322 Map fieldHt = field.getMap(); 323 324 PdfString oldValue = (PdfString)(fieldHt.get(new PdfName("V"))); 326 327 PdfString valueString = new PdfString(value); 328 origFieldHt.put(new PdfName("V"), valueString); 329 origFieldHt.put(new PdfName("DV"), valueString); 330 331 PdfInteger q = (PdfInteger)(manager.getObjectIndirect((PdfObject)(fieldHt.get(new PdfName("Q"))))); 333 boolean leftJustified = false; 334 boolean centered = false; 335 boolean rightJustified = false; 336 if (q == null) { 337 leftJustified = true; 338 } else { 339 switch (q.getInt()) { 340 case 1: 341 centered = true; 342 break; 343 case 2: 344 rightJustified = true; 345 break; 346 default: 347 leftJustified = true; 348 } 349 } 350 351 PdfDictionary ap = (PdfDictionary)(manager.getObjectIndirect((PdfObject)(fieldHt.get(new PdfName("AP"))))); 352 if (ap != null) { 353 Map apHtReadOnly = ap.getMap(); 354 HashMap apHt = new HashMap(apHtReadOnly.size()); 355 apHt.putAll(apHtReadOnly); 356 PdfObject apnObj = (PdfObject)(apHt.get(new PdfName("N"))); 357 int apnId; 358 PdfReference apnRef; 359 PdfObject apn; 360 PdfDictionary apnDict; 361 byte[] apnBuffer; 362 if (apnObj instanceof PdfReference) { 363 apnRef = (PdfReference)apnObj; 365 apnId = apnRef.getObjectNumber(); 366 apn = manager.getObjectIndirect(apnRef); 367 } else { 368 apnId = manager.addObject(apnObj); 370 apnRef = new PdfReference(apnId, 0); 371 apHt.put(new PdfName("N"), apnRef); 372 apn = apnObj; 373 } 374 375 381 float rectX1 = 0; 382 float rectX2 = 0; 383 float rectWidth = 0; 384 if (centered) { 385 PdfArray rect = 387 (PdfArray)(fieldHt.get(new PdfName("Rect"))); 388 List rectList = rect.getList(); 389 rectX1 = ((PdfNumber)rectList.get(0)).getFloat(); 390 rectX2 = ((PdfNumber)rectList.get(2)).getFloat(); 391 rectWidth = rectX2 - rectX1; 392 } 393 394 if ( (apn != null) && (apn instanceof PdfStream) ) { 395 PjStream apnPj = (PjStream)PjxConvert.toPjObject(apn); 398 Vector pmVector = new StreamParser().parse( 399 apnPj.flateDecompress()); 400 if (oldValue != null) { 401 pjxReplaceTextData(pmVector, oldValue, valueString); 402 } 403 if (centered) { 404 pjxAdjustTextMatrixX(pmVector, rectWidth); 405 } 406 ByteArrayOutputStream baos = 408 new ByteArrayOutputStream(); 409 for (int pmX = 0; pmX < pmVector.size(); pmX++) { 410 PageMark pm = (PageMark)(pmVector.elementAt(pmX)); 411 try { 412 pm.writePdf(baos); 413 } 414 catch (IOException e) { 415 e.printStackTrace(); 416 } 417 } 418 byte[] ba = baos.toByteArray(); 419 PjStream temp = new PjStream( apnPj.getStreamDictionary(), ba ); 420 manager.setObject(PjxConvert.toPjxObject(temp), apnId); 421 } 422 } 423 } 424 catch (com.etymon.pj.exception.PdfFormatException e) { 425 throw new com.etymon.pjx.PdfFormatException(e.getMessage()); 426 } 427 catch (com.etymon.pj.exception.InvalidPdfObjectException f) { 428 throw new com.etymon.pjx.PdfFormatException(f.getMessage()); 429 } 430 } 431 } 432 433 437 private static void pjxReplaceTextData(Vector pmVector, PdfString oldText, PdfString newText) 438 throws com.etymon.pj.exception.PdfFormatException { 439 440 442 int pmX = pmVector.size(); 443 444 PjString oldTextPj = (PjString)PjxConvert.toPjObject(oldText); 445 PjString newTextPj = (PjString)PjxConvert.toPjObject(newText); 446 447 while (pmX > 0) { 450 451 pmX--; 452 PageMark pm = (PageMark)(pmVector.elementAt(pmX)); 453 454 if (pm instanceof XTj) { 455 XTj tj = (XTj)pm; 456 if (tj.getText().equals(oldTextPj)) { 457 XTj newTj = new XTj(newTextPj); 458 pmVector.setElementAt(newTj, pmX); 459 } 460 } 461 462 } 463 } 464 465 469 private static void pjxAdjustTextMatrixX(Vector pmVector, float rectWidth) { 470 475 int pmX = pmVector.size(); 476 float textWidth = 0; 477 float rectCenter = rectWidth / 2; 478 479 while (pmX > 0) { 480 481 pmX--; 482 PageMark pm = (PageMark)(pmVector.elementAt(pmX)); 483 484 if (pm instanceof XTj) { 485 XTj tj = (XTj)pm; 486 textWidth = tj.getText().getString().length() * 6; 487 } 488 489 if (pm instanceof XTm) { 490 float newX = rectCenter - (textWidth / 2); 491 if (newX < 0) { 492 newX = 0; 493 } 494 XTm tm = (XTm)pm; 495 XTm newTm = new XTm( 496 tm.getA(), 497 tm.getB(), 498 tm.getC(), 499 tm.getD(), 500 new PjNumber(newX), 501 tm.getY()); 502 pmVector.setElementAt(newTm, pmX); 503 pmX = 0; } 505 506 } 507 } 508 509 513 private static void pjxClearTextMatrixX(Vector pmVector) { 514 517 int pmX = pmVector.size(); 518 519 while (pmX > 0) { 520 521 pmX--; 522 PageMark pm = (PageMark)(pmVector.elementAt(pmX)); 523 524 if (pm instanceof XTm) { 525 XTm tm = (XTm)pm; 526 XTm newTm = new XTm( 527 tm.getA(), 528 tm.getB(), 529 tm.getC(), 530 tm.getD(), 531 PjNumber.ZERO, 532 tm.getY()); 533 pmVector.setElementAt(newTm, pmX); 534 pmX = 0; } 536 537 } 538 } 539 540 559 public static PdfDictionary pjxInheritFieldAttributes(PdfManager manager, PdfDictionary d) 560 throws IOException, com.etymon.pjx.PdfFormatException { 561 synchronized (manager) { 562 try { 563 PjDictionary node = (PjDictionary)PjxConvert.toPjObject(d); 564 PjDictionary newNode; 565 try { 566 newNode = (PjDictionary)(node.clone()); 567 } 568 catch (CloneNotSupportedException e) { 569 throw new com.etymon.pj.exception.InvalidPdfObjectException(e.getMessage()); 570 } 571 Hashtable ht = newNode.getHashtable(); 572 PjObject parentRef = (PjObject)(newNode.getHashtable().get(PjName.PARENT)); 573 while (parentRef != null) { 574 PjObject parentObj = PjxConvert.toPjObject( 575 manager.getObjectIndirect(PjxConvert.toPjxObject(parentRef))); 576 if ( ! (parentObj instanceof PjDictionary) ) { 577 throw new com.etymon.pj.exception.InvalidPdfObjectException("Ancestor of field node is not a dictionary."); 578 } 579 PjDictionary parent = (PjDictionary)parentObj; 580 pjxInheritFieldAttributesCollapse(PjName.FT, ht, newNode, parent); 581 pjxInheritFieldAttributesCollapse(PjName.V, ht, newNode, parent); 582 pjxInheritFieldAttributesCollapse(PjName.DV, ht, newNode, parent); 583 pjxInheritFieldAttributesCollapse(PjName.FF, ht, newNode, parent); 584 pjxInheritFieldAttributesCollapse(PjName.DR, ht, newNode, parent); 585 pjxInheritFieldAttributesCollapse(PjName.DA, ht, newNode, parent); 586 pjxInheritFieldAttributesCollapse(PjName.Q, ht, newNode, parent); 587 pjxInheritFieldAttributesCollapse(PjName.OPT, ht, newNode, parent); 588 pjxInheritFieldAttributesCollapse(PjName.TOPINDEX, ht, newNode, parent); 589 pjxInheritFieldAttributesCollapse(PjName.MAXLEN, ht, newNode, parent); 590 parentRef = (PjObject)(parent.getHashtable().get(PjName.PARENT)); 591 } 592 return (PdfDictionary)PjxConvert.toPjxObject(newNode); 593 } 594 catch (com.etymon.pj.exception.PdfFormatException e) { 595 throw new com.etymon.pjx.PdfFormatException(e.getMessage()); 596 } 597 catch (com.etymon.pj.exception.InvalidPdfObjectException f) { 598 throw new com.etymon.pjx.PdfFormatException(f.getMessage()); 599 } 600 } 601 } 602 603 607 private static void pjxInheritFieldAttributesCollapse( 608 PjName name, Hashtable ht, PjDictionary newNode, PjDictionary parent) { 609 610 if (ht.get(name) == null) { 611 Object obj = parent.getHashtable().get(name); 612 if (obj != null) { 613 ht.put(name, obj); 614 } 615 } 616 } 617 618 } 619 | Popular Tags |