1 49 50 package com.lowagie.text.pdf.internal; 51 52 import java.io.IOException ; 53 import java.net.URL ; 54 import java.util.ArrayList ; 55 import java.util.HashMap ; 56 57 import com.lowagie.text.Annotation; 58 import com.lowagie.text.ExceptionConverter; 59 import com.lowagie.text.Rectangle; 60 import com.lowagie.text.pdf.PdfAcroForm; 61 import com.lowagie.text.pdf.PdfAction; 62 import com.lowagie.text.pdf.PdfAnnotation; 63 import com.lowagie.text.pdf.PdfArray; 64 import com.lowagie.text.pdf.PdfFileSpecification; 65 import com.lowagie.text.pdf.PdfFormField; 66 import com.lowagie.text.pdf.PdfName; 67 import com.lowagie.text.pdf.PdfObject; 68 import com.lowagie.text.pdf.PdfRectangle; 69 import com.lowagie.text.pdf.PdfString; 70 import com.lowagie.text.pdf.PdfWriter; 71 72 public class PdfAnnotationsImp { 73 74 77 protected PdfAcroForm acroForm; 78 79 83 protected ArrayList annotations; 84 85 89 protected ArrayList delayedAnnotations = new ArrayList (); 90 91 92 public PdfAnnotationsImp(PdfWriter writer) { 93 acroForm = new PdfAcroForm(writer); 94 } 95 96 99 public boolean hasValidAcroForm() { 100 return acroForm.isValid(); 101 } 102 103 107 public PdfAcroForm getAcroForm() { 108 return acroForm; 109 } 110 111 public void setSigFlags(int f) { 112 acroForm.setSigFlags(f); 113 } 114 115 public void addCalculationOrder(PdfFormField formField) { 116 acroForm.addCalculationOrder(formField); 117 } 118 119 public void addAnnotation(PdfAnnotation annot) { 120 if (annot.isForm()) { 121 PdfFormField field = (PdfFormField)annot; 122 if (field.getParent() == null) 123 addFormFieldRaw(field); 124 } 125 else 126 annotations.add(annot); 127 } 128 129 public void addPlainAnnotation(PdfAnnotation annot) { 130 annotations.add(annot); 131 } 132 133 void addFormFieldRaw(PdfFormField field) { 134 annotations.add(field); 135 ArrayList kids = field.getKids(); 136 if (kids != null) { 137 for (int k = 0; k < kids.size(); ++k) 138 addFormFieldRaw((PdfFormField)kids.get(k)); 139 } 140 } 141 142 public boolean hasUnusedAnnotations() { 143 return !annotations.isEmpty(); 144 } 145 146 public void resetAnnotations() { 147 annotations = delayedAnnotations; 148 delayedAnnotations = new ArrayList (); 149 } 150 151 public PdfArray rotateAnnotations(PdfWriter writer, Rectangle pageSize) { 152 PdfArray array = new PdfArray(); 153 int rotation = pageSize.getRotation() % 360; 154 int currentPage = writer.getCurrentPageNumber(); 155 for (int k = 0; k < annotations.size(); ++k) { 156 PdfAnnotation dic = (PdfAnnotation)annotations.get(k); 157 int page = dic.getPlaceInPage(); 158 if (page > currentPage) { 159 delayedAnnotations.add(dic); 160 continue; 161 } 162 if (dic.isForm()) { 163 if (!dic.isUsed()) { 164 HashMap templates = dic.getTemplates(); 165 if (templates != null) 166 acroForm.addFieldTemplates(templates); 167 } 168 PdfFormField field = (PdfFormField)dic; 169 if (field.getParent() == null) 170 acroForm.addDocumentField(field.getIndirectReference()); 171 } 172 if (dic.isAnnotation()) { 173 array.add(dic.getIndirectReference()); 174 if (!dic.isUsed()) { 175 PdfRectangle rect = (PdfRectangle)dic.get(PdfName.RECT); 176 if (rect != null) { 177 switch (rotation) { 178 case 90: 179 dic.put(PdfName.RECT, new PdfRectangle( 180 pageSize.getTop() - rect.bottom(), 181 rect.left(), 182 pageSize.getTop() - rect.top(), 183 rect.right())); 184 break; 185 case 180: 186 dic.put(PdfName.RECT, new PdfRectangle( 187 pageSize.getRight() - rect.left(), 188 pageSize.getTop() - rect.bottom(), 189 pageSize.getRight() - rect.right(), 190 pageSize.getTop() - rect.top())); 191 break; 192 case 270: 193 dic.put(PdfName.RECT, new PdfRectangle( 194 rect.bottom(), 195 pageSize.getRight() - rect.left(), 196 rect.top(), 197 pageSize.getRight() - rect.right())); 198 break; 199 } 200 } 201 } 202 } 203 if (!dic.isUsed()) { 204 dic.setUsed(); 205 try { 206 writer.addToBody(dic, dic.getIndirectReference()); 207 } 208 catch (IOException e) { 209 throw new ExceptionConverter(e); 210 } 211 } 212 } 213 return array; 214 } 215 216 public static PdfAnnotation convertAnnotation(PdfWriter writer, Annotation annot, Rectangle defaultRect) throws IOException { 217 switch(annot.annotationType()) { 218 case Annotation.URL_NET: 219 return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((URL ) annot.attributes().get(Annotation.URL))); 220 case Annotation.URL_AS_STRING: 221 return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String ) annot.attributes().get(Annotation.FILE))); 222 case Annotation.FILE_DEST: 223 return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String ) annot.attributes().get(Annotation.FILE), (String ) annot.attributes().get(Annotation.DESTINATION))); 224 case Annotation.SCREEN: 225 boolean sparams[] = (boolean[])annot.attributes().get(Annotation.PARAMETERS); 226 String fname = (String ) annot.attributes().get(Annotation.FILE); 227 String mimetype = (String ) annot.attributes().get(Annotation.MIMETYPE); 228 PdfFileSpecification fs; 229 if (sparams[0]) 230 fs = PdfFileSpecification.fileEmbedded(writer, fname, fname, null); 231 else 232 fs = PdfFileSpecification.fileExtern(writer, fname); 233 PdfAnnotation ann = PdfAnnotation.createScreen(writer, new Rectangle(annot.llx(), annot.lly(), annot.urx(), annot.ury()), 234 fname, fs, mimetype, sparams[1]); 235 return ann; 236 case Annotation.FILE_PAGE: 237 return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String ) annot.attributes().get(Annotation.FILE), ((Integer ) annot.attributes().get(Annotation.PAGE)).intValue())); 238 case Annotation.NAMED_DEST: 239 return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction(((Integer ) annot.attributes().get(Annotation.NAMED)).intValue())); 240 case Annotation.LAUNCH: 241 return new PdfAnnotation(writer, annot.llx(), annot.lly(), annot.urx(), annot.ury(), new PdfAction((String ) annot.attributes().get(Annotation.APPLICATION),(String ) annot.attributes().get(Annotation.PARAMETERS),(String ) annot.attributes().get(Annotation.OPERATION),(String ) annot.attributes().get(Annotation.DEFAULTDIR))); 242 default: 243 return new PdfAnnotation(writer, defaultRect.getLeft(), defaultRect.getBottom(), defaultRect.getRight(), defaultRect.getTop(), new PdfString(annot.title(), PdfObject.TEXT_UNICODE), new PdfString(annot.content(), PdfObject.TEXT_UNICODE)); 244 } 245 } 246 } 247 | Popular Tags |