1 26 27 package it.stefanochizzolini.clown.documents; 28 29 import it.stefanochizzolini.clown.documents.contents.ContentStreams; 30 import it.stefanochizzolini.clown.documents.contents.IContentStreamContext; 31 import it.stefanochizzolini.clown.documents.contents.Resources; 32 import it.stefanochizzolini.clown.files.File; 33 import it.stefanochizzolini.clown.objects.IPdfNumber; 34 import it.stefanochizzolini.clown.objects.PdfArray; 35 import it.stefanochizzolini.clown.objects.PdfDictionary; 36 import it.stefanochizzolini.clown.objects.PdfDirectObject; 37 import it.stefanochizzolini.clown.objects.PdfInteger; 38 import it.stefanochizzolini.clown.objects.PdfName; 39 import it.stefanochizzolini.clown.objects.PdfObjectWrapper; 40 import it.stefanochizzolini.clown.objects.PdfReference; 41 import it.stefanochizzolini.clown.util.NotImplementedException; 42 43 import java.util.Map ; 44 45 48 public class Page 49 extends PdfObjectWrapper<PdfDictionary> 50 implements IContentStreamContext 51 { 52 57 public static Page wrap( 62 PdfReference reference 63 ) 64 {return new Page(reference);} 65 69 public Page( 72 Document context 73 ) 74 { 75 super( 76 context.getFile(), 77 new PdfDictionary( 78 new PdfName[] 79 { 80 PdfName.Type, 81 PdfName.MediaBox, 82 PdfName.Contents, 83 PdfName.Resources 84 }, 85 new PdfDirectObject[] 86 { 87 PdfName.Page, 88 context.ensureMediaBox(), 89 new PdfArray(), 90 context.getResources().getBaseObject() 91 } 92 ) 93 ); 94 } 95 96 100 public Page( 101 PdfDirectObject baseObject 102 ) 103 { 104 super( 105 baseObject, 106 null ); 108 } 109 111 public Object clone( 114 Document context 115 ) 116 { 117 124 126 File contextFile = context.getFile(); 127 PdfDictionary clone = new PdfDictionary(getBaseDataObject().size()); 128 for( 129 Map.Entry <PdfName,PdfDirectObject> entry : getBaseDataObject().entrySet() 130 ) 131 { 132 PdfName key = entry.getKey(); 133 if(key.equals(PdfName.Parent) 135 || key.equals(PdfName.Annots)) 136 continue; 137 138 clone.put( 140 key, 141 (PdfDirectObject)entry.getValue().clone(contextFile) 142 ); 143 } 144 145 return new Page( 146 contextFile.getIndirectObjects().add(clone).getReference() 147 ); 148 } 149 150 153 public ContentStreams getContents( 154 ) 155 { 156 return new ContentStreams( 157 getBaseDataObject().get(PdfName.Contents), 158 this 159 ); 160 } 161 162 public double getHeight( 163 ) 164 { 165 return ((IPdfNumber) 166 ((PdfArray) 167 File.resolve( 168 ensureEntry(PdfName.MediaBox) 169 ) 170 ).get(3) 171 ).getNumberValue(); 172 } 173 174 public void setHeight( 175 double value 176 ) 177 { 178 throw new NotImplementedException("When page height is about to be modified, you need to verify that the change will affect just the mediaBox of this page; so, if such a mediaBox is implicit (inherited), it MUST be cloned and explicitly assigned to this page in order to apply customizations."); 179 } 180 181 188 public int getIndex( 189 ) 190 { 191 196 PdfReference ancestorKidReference = (PdfReference)getBaseObject(); 197 PdfReference parentReference = (PdfReference)getBaseDataObject().get(PdfName.Parent); 198 PdfDictionary parent = (PdfDictionary)File.resolve(parentReference); 199 PdfArray kids = (PdfArray)File.resolve(parent.get(PdfName.Kids)); 200 int index = 0; 201 for( 202 int i = 0; 203 true; 204 i++ 205 ) 206 { 207 PdfReference kidReference = (PdfReference)kids.get(i); 208 if(kidReference.equals(ancestorKidReference)) { 212 if(!parent.containsKey(PdfName.Parent)) 214 { 215 return index; 217 } 218 ancestorKidReference = parentReference; 220 parentReference = (PdfReference)parent.get(PdfName.Parent); 222 parent = (PdfDictionary)File.resolve(parentReference); 223 kids = (PdfArray)File.resolve(parent.get(PdfName.Kids)); 224 i = -1; 225 } 226 else { 228 PdfDictionary kid = (PdfDictionary)File.resolve(kidReference); 229 if(kid.get(PdfName.Type).equals(PdfName.Page)) 230 index++; 231 else 232 index += ((PdfInteger)kid.get(PdfName.Count)).getValue(); 233 } 234 } 235 } 236 237 243 public Page getNext( 244 ) 245 {return getPage(+1);} 246 247 253 public Page getPrevious( 254 ) 255 {return getPage(-1);} 256 257 263 public Page getPage( 264 int offset 265 ) 266 { 267 if(offset == 0) 268 return this; 269 273 return getDocument().getPages().get(getIndex() + offset); 274 } 275 276 279 public Resources getResources( 280 ) 281 { 282 return new Resources( 283 ensureEntry(PdfName.Resources), 284 ((PdfReference)getBaseObject()).getIndirectObject() 285 ); 286 } 287 288 public double getWidth( 289 ) 290 { 291 return ((IPdfNumber) 292 ((PdfArray) 293 File.resolve( 294 ensureEntry(PdfName.MediaBox) 295 ) 296 ).get(2) 297 ).getNumberValue(); 298 } 299 300 public void setWidth( 301 double value 302 ) 303 { 304 throw new NotImplementedException("When page width is about to be modified, you need to verify that the change will affect just the mediaBox of this page; so, if such a mediaBox is implicit (inherited), it MUST be cloned and explicitly assigned to this page in order to apply customizations."); 305 } 306 308 protected PdfDirectObject ensureEntry( 310 PdfName key 311 ) 312 { 313 PdfDirectObject entry = getBaseDataObject().get(key); 314 if(entry == null) 315 throw new NotImplementedException("When an entry (" + key + ") is implicit, it MUST be retrieve as inherited attribute from upstream nodes (move upwards through the Parent attribute nodes!)."); 316 317 return entry; 318 } 319 321 327 public PdfArray getContextBox( 328 ) 329 { 330 return (PdfArray)File.resolve(ensureEntry(PdfName.MediaBox)); 331 } 332 333 337 public PdfDictionary getContextResources( 338 ) 339 { 340 return (PdfDictionary)File.resolve(ensureEntry(PdfName.Resources)); 341 } 342 } | Popular Tags |