List of usage examples for com.itextpdf.text.pdf PdfArray getAsArray
public PdfArray getAsArray(final int idx)
PdfObject
as a PdfArray
, resolving indirect references. From source file:de.gbv.marginalia.Annotation.java
License:Open Source License
/** * Serialize the annotation in XML format. * The annotation is emitted as stream of SAX events to a ContentHandler. * The XML is XFDF with additional Marginalia elements in its own namespace. *//* w w w . j av a 2s .c om*/ public void serializeXML(ContentHandler handler) throws SAXException { SimpleXMLCreator xml = new SimpleXMLCreator(handler, namespaces); Set<PdfName> allkeys = this.dict.getKeys(); allkeys.remove(PdfName.TYPE); allkeys.remove(PdfName.SUBTYPE); allkeys.remove(PdfName.PARENT); allkeys.remove(PdfName.CONTENTS); allkeys.remove(PdfName.POPUP); Map<String, String> attrs = new HashMap<String, String>(); for (String aName : this.FIELDS.keySet()) { Field f = this.FIELDS.get(aName); String value = f.getFrom(this.dict); if (value != null) { // TODO: encoding & exception attrs.put(aName, value); // allkeys.remove( f.name ); } } PdfDictionary pg = getAsDictionary(this.dict, PdfName.P); allkeys.remove(PdfName.P); //CropBox=[0, 0, 595, 842] //Rotate //MediaBox=[0, 0, 595, 842] // TODO: find out where page number is stored if (attrs.get("page") == null) attrs.put("page", "" + this.pageNum); String element = subtypes.get(this.subtype); if (element == null) { // TODO element = this.subtype.toString(); } xml.startElement(element, attrs); if (element.equals("ink")) { PdfArray inklist = this.dict.getAsArray(new PdfName("InkList")); if (inklist != null) { xml.startElement("inklist"); for (int i = 0; i < inklist.size(); i++) { PdfArray pathArray = inklist.getAsArray(i); String s = ""; for (int j = 0; j < pathArray.size(); j += 2) { if (j > 0) s += ";"; s += "" + pathArray.getAsNumber(j).floatValue() + ","; s += "" + pathArray.getAsNumber(j + 1).floatValue(); } xml.contentElement("gesture", s); } xml.endElement(); } } if (attrs.get("rect") != null) { Map<String, String> a = new HashMap<String, String>(); RectField rf = (RectField) this.FIELDS.get("rect"); PdfRectangle r = null; if (rf != null) r = (PdfRectangle) rf.getObjectFrom(this.dict); if (r != null) { a.put("left", "" + r.left()); a.put("bottom", "" + r.bottom()); a.put("right", "" + r.right()); a.put("top", "" + r.top()); xml.emptyElement("m", "rect", a); } } if (this.content != null && !this.content.equals("")) { // TODO: encode content if not UTF-8 ? xml.contentElement("content", content.toString()); } // TODO: contents-richtext // TODO: popup /* if ( this.popup != null ) { out.println("<!--popup>"); for ( PdfName n : this.popup.getKeys() ) { out.println( n + "=" + this.popup.getDirectObject(n) ); } out.println("</popup-->"); } */ // remaining dictionary elements /* for ( PdfName name : allkeys ) { Map<String,String> a = new HashMap<String,String>(); a.put("name",name.toString()); a.put("value",this.dict.getDirectObject(name).toString()); xml.emptyElement( "m","unknown", a ); } */ xml.endElement(); }