KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdfviewer > PageDrawer


1 /**
2  * Copyright (c) 2003-2005, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.pdfviewer;
32
33 import java.awt.Color JavaDoc;
34 import java.awt.Dimension JavaDoc;
35 import java.awt.Graphics JavaDoc;
36 import java.awt.Graphics2D JavaDoc;
37 import java.awt.RenderingHints JavaDoc;
38
39 import java.awt.geom.GeneralPath JavaDoc;
40
41 import java.io.IOException JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.List JavaDoc;
44 import java.util.Map JavaDoc;
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 /**
60  * This will paint a page in a PDF document to a graphics context.
61  *
62  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
63  * @version $Revision: 1.19 $
64  */

65 public class PageDrawer extends PDFStreamEngine
66 {
67
68     private Graphics2D JavaDoc graphics;
69     private Dimension JavaDoc pageSize;
70     private PDPage page;
71
72     private List JavaDoc lineSubPaths = new ArrayList JavaDoc();
73     private GeneralPath JavaDoc linePath = new GeneralPath JavaDoc();
74     private Color JavaDoc strokingColor = Color.BLACK;
75     private Color JavaDoc nonStrokingColor = Color.BLACK;
76     
77     /**
78      * Default constructor, loads properties from file.
79      *
80      * @throws IOException If there is an error loading properties from the file.
81      */

82     public PageDrawer() throws IOException JavaDoc
83     {
84         super( ResourceLoader.loadProperties( "Resources/PageDrawer.properties" ) );
85     }
86
87     /**
88      * This will draw the page to the requested context.
89      *
90      * @param g The graphics context to draw onto.
91      * @param p The page to draw.
92      * @param pageDimension The size of the page to draw.
93      *
94      * @throws IOException If there is an IO error while drawing the page.
95      */

96     public void drawPage( Graphics JavaDoc g, PDPage p, Dimension JavaDoc pageDimension ) throws IOException JavaDoc
97     {
98         graphics = (Graphics2D JavaDoc)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 JavaDoc 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 JavaDoc appearanceName = annot.getAppearanceStream();
111             PDAppearanceDictionary appearDictionary = annot.getAppearance();
112             if( appearDictionary != null )
113             {
114                 if( appearanceName == null )
115                 {
116                     appearanceName = "default";
117                 }
118                 Map JavaDoc 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                     //g.translate( 20, -20 );
125
processSubStream( page, appearance.getResources(), appearance.getStream() );
126                     g.translate( (int)-rect.getLowerLeftX(), (int)+rect.getLowerLeftY() );
127                 }
128             }
129         }
130         // Transformations should be done in order
131
// 1 - Translate
132
// 2 - Rotate
133
// 3 - Scale
134
// Refer to PDFReference p176 (or 188 in xpdf)
135
//AffineTransform transform = graphics.getTransform();
136
//transform.setToTranslate( 0, page.findMediaBox().getHeight()/2 );
137
//transform.setToRotation((double)p.getRotation());
138
//transform.setTransform( 1, 0, 0, 1, 0, 0 );
139
//transform.setToScale( 1, 1 );
140

141         //AffineTransform rotation = graphics.getTransform();
142
//rotation.rotate( (page.findRotation() * Math.PI) / 180d );
143
//graphics.setTransform( rotation );
144
}
145
146     /**
147      * You should override this method if you want to perform an action when a
148      * string is being shown.
149      *
150      * @param text The string to display.
151      */

152     protected void showCharacter( TextPosition text )
153     {
154         //should use colorspaces for the font color but for now assume that
155
//the font color is black
156
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 JavaDoc io )
164         {
165             io.printStackTrace();
166         }
167     }
168     
169     /**
170      * Get the graphics that we are currently drawing on.
171      *
172      * @return The graphics we are drawing on.
173      */

174     public Graphics2D JavaDoc getGraphics()
175     {
176         return graphics;
177     }
178     
179     /**
180      * Get the page that is currently being drawn.
181      *
182      * @return The page that is being drawn.
183      */

184     public PDPage getPage()
185     {
186         return page;
187     }
188     
189     /**
190      * Get the size of the page that is currently being drawn.
191      *
192      * @return The size of the page that is being drawn.
193      */

194     public Dimension JavaDoc getPageSize()
195     {
196         return pageSize;
197     }
198     
199     /**
200      * Fix the y coordinate based on page rotation.
201      *
202      * @param x The x coordinate.
203      * @param y The y coordinate.
204      * @return The updated y coordinate.
205      */

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     /**
222      * Get the current line path to be drawn.
223      *
224      * @return The current line path to be drawn.
225      */

226     public GeneralPath JavaDoc getLinePath()
227     {
228         return linePath;
229     }
230
231     /**
232      * Set the line path to draw.
233      *
234      * @param newLinePath Set the line path to draw.
235      */

236     public void setLinePath(GeneralPath JavaDoc newLinePath)
237     {
238         linePath = newLinePath;
239     }
240     
241     /**
242      * Get the current list of line paths to be drawn.
243      *
244      * @return The current list of line paths to be drawn.
245      */

246     public List JavaDoc getLineSubPaths()
247     {
248         return lineSubPaths;
249     }
250
251     /**
252      * Set the list of line paths to draw.
253      *
254      * @param newLineSubPaths Set the list of line paths to draw.
255      */

256     public void setLineSubPaths(List JavaDoc newLineSubPaths)
257     {
258         lineSubPaths = newLineSubPaths;
259     }
260
261     /**
262      * Get the non stroking color.
263      *
264      * @return The non stroking color.
265      */

266     public Color JavaDoc getNonStrokingColor()
267     {
268         return nonStrokingColor;
269     }
270
271     /**
272      * Set the non stroking color.
273      *
274      * @param newNonStrokingColor The non stroking color.
275      */

276     public void setNonStrokingColor(Color JavaDoc newNonStrokingColor)
277     {
278         nonStrokingColor = newNonStrokingColor;
279     }
280
281     /**
282      * Get the stroking color.
283      *
284      * @return The stroking color.
285      */

286     public Color JavaDoc getStrokingColor()
287     {
288         return strokingColor;
289     }
290
291     /**
292      * Set the stroking color.
293      *
294      * @param newStrokingColor The stroking color.
295      */

296     public void setStrokingColor(Color JavaDoc newStrokingColor)
297     {
298         strokingColor = newStrokingColor;
299     }
300 }
Popular Tags