KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > util > PDFTextStripperByArea


1 /**
2  * Copyright (c) 2005-2006, 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.util;
32
33 import java.awt.geom.Rectangle2D JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.StringWriter JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.Map JavaDoc;
41 import java.util.Vector JavaDoc;
42
43 import org.pdfbox.cos.COSStream;
44 import org.pdfbox.pdmodel.PDPage;
45 import org.pdfbox.pdmodel.common.PDStream;
46
47 /**
48  * This will extract text from a specified region in the PDF.
49  *
50  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
51  * @version $Revision: 1.5 $
52  */

53 public class PDFTextStripperByArea extends PDFTextStripper
54 {
55     private List JavaDoc regions = new ArrayList JavaDoc();
56     private Map JavaDoc regionArea = new HashMap JavaDoc();
57     private Map JavaDoc regionCharacterList = new HashMap JavaDoc();
58     private Map JavaDoc regionText = new HashMap JavaDoc();
59     
60     /**
61      * Constructor.
62      * @throws IOException If there is an error loading properties.
63      */

64     public PDFTextStripperByArea() throws IOException JavaDoc
65     {
66         super();
67         setPageSeparator( "" );
68     }
69     
70     /**
71      * Add a new region to group text by.
72      *
73      * @param regionName The name of the region.
74      * @param rect The rectangle area to retrieve the text from.
75      */

76     public void addRegion( String JavaDoc regionName, Rectangle2D JavaDoc rect )
77     {
78         regions.add( regionName );
79         regionArea.put( regionName, rect );
80     }
81     
82     /**
83      * Get the list of regions that have been setup.
84      *
85      * @return A list of java.lang.String objects to identify the region names.
86      */

87     public List JavaDoc getRegions()
88     {
89         return regions;
90     }
91     
92     /**
93      * Get the text for the region, this should be called after extractRegions().
94      *
95      * @param regionName The name of the region to get the text from.
96      * @return The text that was identified in that region.
97      */

98     public String JavaDoc getTextForRegion( String JavaDoc regionName )
99     {
100         StringWriter JavaDoc text = (StringWriter JavaDoc)regionText.get( regionName );
101         return text.toString();
102     }
103     
104     /**
105      * Process the page to extract the region text.
106      *
107      * @param page The page to extract the regions from.
108      * @throws IOException If there is an error while extracting text.
109      */

110     public void extractRegions( PDPage page ) throws IOException JavaDoc
111     {
112         Iterator JavaDoc regionIter = regions.iterator();
113         while( regionIter.hasNext() )
114         {
115             //reset the stored text for the region so this class
116
//can be reused.
117
String JavaDoc regionName = (String JavaDoc)regionIter.next();
118             Vector JavaDoc regionCharactersByArticle = new Vector JavaDoc();
119             regionCharactersByArticle.add( new ArrayList JavaDoc() );
120             regionCharacterList.put( regionName, regionCharactersByArticle );
121             regionText.put( regionName, new StringWriter JavaDoc() );
122         }
123         
124         PDStream contentStream = page.getContents();
125         if( contentStream != null )
126         {
127             COSStream contents = contentStream.getStream();
128             processPage( page, contents );
129         }
130     }
131     
132     /**
133      * {@inheritDoc}
134      */

135     protected void showCharacter( TextPosition text )
136     {
137         Iterator JavaDoc regionIter = regionArea.keySet().iterator();
138         while( regionIter.hasNext() )
139         {
140             String JavaDoc region = (String JavaDoc)regionIter.next();
141             Rectangle2D JavaDoc rect = (Rectangle2D JavaDoc)regionArea.get( region );
142             if( rect.contains( text.getX(), text.getY() ) )
143             {
144                 charactersByArticle = (Vector JavaDoc)regionCharacterList.get( region );
145                 super.showCharacter( text );
146             }
147         }
148     }
149     
150     /**
151      * This will print the text to the output stream.
152      *
153      * @throws IOException If there is an error writing the text.
154      */

155     protected void flushText() throws IOException JavaDoc
156     {
157         Iterator JavaDoc regionIter = regionArea.keySet().iterator();
158         while( regionIter.hasNext() )
159         {
160             String JavaDoc region = (String JavaDoc)regionIter.next();
161             charactersByArticle = (Vector JavaDoc)regionCharacterList.get( region );
162             output = (StringWriter JavaDoc)regionText.get( region );
163             super.flushText();
164         }
165     }
166 }
167
Popular Tags