1 31 package org.pdfbox.pdfviewer; 32 33 import java.awt.Color ; 34 import java.awt.Dimension ; 35 import java.awt.Graphics ; 36 import java.awt.Graphics2D ; 37 import java.awt.RenderingHints ; 38 39 import java.awt.geom.GeneralPath ; 40 41 import java.io.IOException ; 42 import java.util.ArrayList ; 43 import java.util.List ; 44 import java.util.Map ; 45 46 import org.pdfbox.pdmodel.PDPage; 47 import org.pdfbox.pdmodel.PDResources; 48 49 import org.pdfbox.pdmodel.common.PDRectangle; 50 import org.pdfbox.pdmodel.font.PDFont; 51 import org.pdfbox.pdmodel.interactive.annotation.PDAnnotation; 52 import org.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary; 53 import org.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream; 54 55 import org.pdfbox.util.PDFStreamEngine; 56 import org.pdfbox.util.ResourceLoader; 57 import org.pdfbox.util.TextPosition; 58 59 65 public class PageDrawer extends PDFStreamEngine 66 { 67 68 private Graphics2D graphics; 69 private Dimension pageSize; 70 private PDPage page; 71 72 private List lineSubPaths = new ArrayList (); 73 private GeneralPath linePath = new GeneralPath (); 74 private Color strokingColor = Color.BLACK; 75 private Color nonStrokingColor = Color.BLACK; 76 77 82 public PageDrawer() throws IOException 83 { 84 super( ResourceLoader.loadProperties( "Resources/PageDrawer.properties" ) ); 85 } 86 87 96 public void drawPage( Graphics g, PDPage p, Dimension pageDimension ) throws IOException 97 { 98 graphics = (Graphics2D )g; 99 page = p; 100 pageSize = pageDimension; 101 102 graphics.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); 103 PDResources resources = page.findResources(); 104 processStream( page, resources, page.getContents().getStream() ); 105 List annotations = page.getAnnotations(); 106 for( int i=0; i<annotations.size(); i++ ) 107 { 108 PDAnnotation annot = (PDAnnotation)annotations.get( i ); 109 PDRectangle rect = annot.getRectangle(); 110 String appearanceName = annot.getAppearanceStream(); 111 PDAppearanceDictionary appearDictionary = annot.getAppearance(); 112 if( appearDictionary != null ) 113 { 114 if( appearanceName == null ) 115 { 116 appearanceName = "default"; 117 } 118 Map appearanceMap = appearDictionary.getNormalAppearance(); 119 PDAppearanceStream appearance = 120 (PDAppearanceStream)appearanceMap.get( appearanceName ); 121 if( appearance != null ) 122 { 123 g.translate( (int)rect.getLowerLeftX(), (int)-rect.getLowerLeftY() ); 124 processSubStream( page, appearance.getResources(), appearance.getStream() ); 126 g.translate( (int)-rect.getLowerLeftX(), (int)+rect.getLowerLeftY() ); 127 } 128 } 129 } 130 141 } 145 146 152 protected void showCharacter( TextPosition text ) 153 { 154 try 157 { 158 graphics.setColor( Color.black ); 159 PDFont font = text.getFont(); 160 font.drawString( text.getCharacter(), graphics, text.getFontSize(), text.getXScale(), text.getYScale(), 161 text.getX(), text.getY() ); 162 } 163 catch( IOException io ) 164 { 165 io.printStackTrace(); 166 } 167 } 168 169 174 public Graphics2D getGraphics() 175 { 176 return graphics; 177 } 178 179 184 public PDPage getPage() 185 { 186 return page; 187 } 188 189 194 public Dimension getPageSize() 195 { 196 return pageSize; 197 } 198 199 206 public double fixY( double x, double y ) 207 { 208 double retval = y; 209 int rotation = page.findRotation(); 210 if( rotation == 0 ) 211 { 212 retval = pageSize.getHeight() - y; 213 } 214 else if( rotation == 90 ) 215 { 216 retval = y; 217 } 218 return retval; 219 } 220 221 226 public GeneralPath getLinePath() 227 { 228 return linePath; 229 } 230 231 236 public void setLinePath(GeneralPath newLinePath) 237 { 238 linePath = newLinePath; 239 } 240 241 246 public List getLineSubPaths() 247 { 248 return lineSubPaths; 249 } 250 251 256 public void setLineSubPaths(List newLineSubPaths) 257 { 258 lineSubPaths = newLineSubPaths; 259 } 260 261 266 public Color getNonStrokingColor() 267 { 268 return nonStrokingColor; 269 } 270 271 276 public void setNonStrokingColor(Color newNonStrokingColor) 277 { 278 nonStrokingColor = newNonStrokingColor; 279 } 280 281 286 public Color getStrokingColor() 287 { 288 return strokingColor; 289 } 290 291 296 public void setStrokingColor(Color newStrokingColor) 297 { 298 strokingColor = newStrokingColor; 299 } 300 } | Popular Tags |