1 47 package com.lowagie.text.pdf.events; 48 49 import java.io.IOException ; 50 import java.util.HashMap ; 51 52 import com.lowagie.text.Document; 53 import com.lowagie.text.DocumentException; 54 import com.lowagie.text.ExceptionConverter; 55 import com.lowagie.text.Rectangle; 56 import com.lowagie.text.pdf.PdfContentByte; 57 import com.lowagie.text.pdf.PdfFormField; 58 import com.lowagie.text.pdf.PdfName; 59 import com.lowagie.text.pdf.PdfPCell; 60 import com.lowagie.text.pdf.PdfPCellEvent; 61 import com.lowagie.text.pdf.PdfPageEventHelper; 62 import com.lowagie.text.pdf.PdfRectangle; 63 import com.lowagie.text.pdf.PdfWriter; 64 import com.lowagie.text.pdf.TextField; 65 66 71 public class FieldPositioningEvents extends PdfPageEventHelper implements PdfPCellEvent { 72 73 76 protected HashMap genericChunkFields = new HashMap (); 77 78 81 protected PdfFormField cellField = null; 82 83 86 protected PdfWriter fieldWriter = null; 87 90 protected PdfFormField parent = null; 91 92 93 public FieldPositioningEvents() {} 94 95 96 public float padding; 97 98 101 public void addField(String text, PdfFormField field) { 102 genericChunkFields.put(text, field); 103 } 104 105 106 public FieldPositioningEvents(PdfWriter writer, PdfFormField field) { 107 this.cellField = field; 108 this.fieldWriter = writer; 109 } 110 111 112 public FieldPositioningEvents(PdfFormField parent, PdfFormField field) { 113 this.cellField = field; 114 this.parent = parent; 115 } 116 117 120 public FieldPositioningEvents(PdfWriter writer, String text) throws IOException , DocumentException { 121 this.fieldWriter = writer; 122 TextField tf = new TextField(writer, new Rectangle(0, 0), text); 123 tf.setFontSize(14); 124 cellField = tf.getTextField(); 125 } 126 127 130 public FieldPositioningEvents(PdfWriter writer, PdfFormField parent, String text) throws IOException , DocumentException { 131 this.parent = parent; 132 TextField tf = new TextField(writer, new Rectangle(0, 0), text); 133 tf.setFontSize(14); 134 cellField = tf.getTextField(); 135 } 136 137 140 public void setPadding(float padding) { 141 this.padding = padding; 142 } 143 144 147 public void setParent(PdfFormField parent) { 148 this.parent = parent; 149 } 150 153 public void onGenericTag(PdfWriter writer, Document document, 154 Rectangle rect, String text) { 155 rect.setBottom(rect.getBottom() - 3); 156 PdfFormField field = (PdfFormField) genericChunkFields.get(text); 157 if (field == null) { 158 TextField tf = new TextField(writer, new Rectangle(rect.getLeft(padding), rect.getBottom(padding), rect.getRight(padding), rect.getTop(padding)), text); 159 tf.setFontSize(14); 160 try { 161 field = tf.getTextField(); 162 } catch (Exception e) { 163 throw new ExceptionConverter(e); 164 } 165 } 166 else { 167 field.put(PdfName.RECT, new PdfRectangle(rect.getLeft(padding), rect.getBottom(padding), rect.getRight(padding), rect.getTop(padding))); 168 } 169 if (parent == null) 170 writer.addAnnotation(field); 171 else 172 parent.addKid(field); 173 } 174 175 178 public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] canvases) { 179 if (cellField == null || (fieldWriter == null && parent == null)) throw new ExceptionConverter(new IllegalArgumentException ("You have used the wrong constructor for this FieldPositioningEvents class.")); 180 cellField.put(PdfName.RECT, new PdfRectangle(rect.getLeft(padding), rect.getBottom(padding), rect.getRight(padding), rect.getTop(padding))); 181 if (parent == null) 182 fieldWriter.addAnnotation(cellField); 183 else 184 parent.addKid(cellField); 185 } 186 } | Popular Tags |