Package com.itextpdf.text.pdf.parser
Class LocationTextExtractionStrategy
- java.lang.Object
-
- com.itextpdf.text.pdf.parser.LocationTextExtractionStrategy
-
- All Implemented Interfaces:
RenderListener
,TextExtractionStrategy
public class LocationTextExtractionStrategy extends java.lang.Object implements TextExtractionStrategy
Development preview - this class (and all of the parser classes) are still experiencing heavy development, and are subject to change both behavior and interface.
A text extraction renderer that keeps track of relative position of text on page The resultant text will be relatively consistent with the physical layout that most PDF files have on screen.
This renderer keeps track of the orientation and distance (both perpendicular and parallel) to the unit vector of the orientation. Text is ordered by orientation, then perpendicular, then parallel distance. Text with the same perpendicular distance, but different parallel distance is treated as being on the same line.
This renderer also uses a simple strategy based on the font metrics to determine if a blank space should be inserted into the output.- Since:
- 5.0.2
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
LocationTextExtractionStrategy.TextChunk
Represents a chunk of text, it's orientation, and location relative to the orientation vectorstatic interface
LocationTextExtractionStrategy.TextChunkFilter
Specifies a filter for filteringLocationTextExtractionStrategy.TextChunk
objects during text extractionstatic interface
LocationTextExtractionStrategy.TextChunkLocation
static class
LocationTextExtractionStrategy.TextChunkLocationDefaultImp
static interface
LocationTextExtractionStrategy.TextChunkLocationStrategy
-
Field Summary
Fields Modifier and Type Field Description (package private) static boolean
DUMP_STATE
set to true for debuggingprivate java.util.List<LocationTextExtractionStrategy.TextChunk>
locationalResult
a summary of all found textprivate LocationTextExtractionStrategy.TextChunkLocationStrategy
tclStrat
-
Constructor Summary
Constructors Constructor Description LocationTextExtractionStrategy()
Creates a new text extraction renderer.LocationTextExtractionStrategy(LocationTextExtractionStrategy.TextChunkLocationStrategy strat)
Creates a new text extraction renderer, with a custom strategy for creating new TextChunkLocation objects based on the input of the TextRenderInfo.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
beginTextBlock()
Called when a new text block is beginning (i.e.private static int
compareInts(int int1, int int2)
private void
dumpState()
Used for debugging onlyprivate boolean
endsWithSpace(java.lang.String str)
void
endTextBlock()
Called when a text block has ended (i.e.private java.util.List<LocationTextExtractionStrategy.TextChunk>
filterTextChunks(java.util.List<LocationTextExtractionStrategy.TextChunk> textChunks, LocationTextExtractionStrategy.TextChunkFilter filter)
Filters the provided list with the provided filterjava.lang.String
getResultantText()
Returns the result so far.java.lang.String
getResultantText(LocationTextExtractionStrategy.TextChunkFilter chunkFilter)
Gets text that meets the specified filter If multiple text extractions will be performed for the same page (i.e.protected boolean
isChunkAtWordBoundary(LocationTextExtractionStrategy.TextChunk chunk, LocationTextExtractionStrategy.TextChunk previousChunk)
Determines if a space character should be inserted between a previous chunk and the current chunk.void
renderImage(ImageRenderInfo renderInfo)
no-op method - this renderer isn't interested in image eventsvoid
renderText(TextRenderInfo renderInfo)
Called when text should be renderedprivate boolean
startsWithSpace(java.lang.String str)
-
-
-
Field Detail
-
DUMP_STATE
static boolean DUMP_STATE
set to true for debugging
-
locationalResult
private final java.util.List<LocationTextExtractionStrategy.TextChunk> locationalResult
a summary of all found text
-
tclStrat
private final LocationTextExtractionStrategy.TextChunkLocationStrategy tclStrat
-
-
Constructor Detail
-
LocationTextExtractionStrategy
public LocationTextExtractionStrategy()
Creates a new text extraction renderer.
-
LocationTextExtractionStrategy
public LocationTextExtractionStrategy(LocationTextExtractionStrategy.TextChunkLocationStrategy strat)
Creates a new text extraction renderer, with a custom strategy for creating new TextChunkLocation objects based on the input of the TextRenderInfo.- Parameters:
strat
- the custom strategy
-
-
Method Detail
-
beginTextBlock
public void beginTextBlock()
Description copied from interface:RenderListener
Called when a new text block is beginning (i.e. BT)- Specified by:
beginTextBlock
in interfaceRenderListener
- See Also:
RenderListener.beginTextBlock()
-
endTextBlock
public void endTextBlock()
Description copied from interface:RenderListener
Called when a text block has ended (i.e. ET)- Specified by:
endTextBlock
in interfaceRenderListener
- See Also:
RenderListener.endTextBlock()
-
startsWithSpace
private boolean startsWithSpace(java.lang.String str)
- Parameters:
str
-- Returns:
- true if the string starts with a space character, false if the string is empty or starts with a non-space character
-
endsWithSpace
private boolean endsWithSpace(java.lang.String str)
- Parameters:
str
-- Returns:
- true if the string ends with a space character, false if the string is empty or ends with a non-space character
-
filterTextChunks
private java.util.List<LocationTextExtractionStrategy.TextChunk> filterTextChunks(java.util.List<LocationTextExtractionStrategy.TextChunk> textChunks, LocationTextExtractionStrategy.TextChunkFilter filter)
Filters the provided list with the provided filter- Parameters:
textChunks
- a list of all TextChunks that this strategy found during processingfilter
- the filter to apply. If null, filtering will be skipped.- Returns:
- the filtered list
- Since:
- 5.3.3
-
isChunkAtWordBoundary
protected boolean isChunkAtWordBoundary(LocationTextExtractionStrategy.TextChunk chunk, LocationTextExtractionStrategy.TextChunk previousChunk)
Determines if a space character should be inserted between a previous chunk and the current chunk. This method is exposed as a callback so subclasses can fine time the algorithm for determining whether a space should be inserted or not. By default, this method will insert a space if the there is a gap of more than half the font space character width between the end of the previous chunk and the beginning of the current chunk. It will also indicate that a space is needed if the starting point of the new chunk appears *before* the end of the previous chunk (i.e. overlapping text).- Parameters:
chunk
- the new chunk being evaluatedpreviousChunk
- the chunk that appeared immediately before the current chunk- Returns:
- true if the two chunks represent different words (i.e. should have a space between them). False otherwise.
-
getResultantText
public java.lang.String getResultantText(LocationTextExtractionStrategy.TextChunkFilter chunkFilter)
Gets text that meets the specified filter If multiple text extractions will be performed for the same page (i.e. for different physical regions of the page), filtering at this level is more efficient than filtering usingFilteredRenderListener
- but not nearly as powerful because most of the RenderInfo state is not captured inLocationTextExtractionStrategy.TextChunk
- Parameters:
chunkFilter
- the filter to to apply- Returns:
- the text results so far, filtered using the specified filter
-
getResultantText
public java.lang.String getResultantText()
Returns the result so far.- Specified by:
getResultantText
in interfaceTextExtractionStrategy
- Returns:
- a String with the resulting text.
-
dumpState
private void dumpState()
Used for debugging only
-
renderText
public void renderText(TextRenderInfo renderInfo)
Description copied from interface:RenderListener
Called when text should be rendered- Specified by:
renderText
in interfaceRenderListener
- Parameters:
renderInfo
- information specifying what to render- See Also:
RenderListener.renderText(com.itextpdf.text.pdf.parser.TextRenderInfo)
-
compareInts
private static int compareInts(int int1, int int2)
- Parameters:
int1
-int2
-- Returns:
- comparison of the two integers
-
renderImage
public void renderImage(ImageRenderInfo renderInfo)
no-op method - this renderer isn't interested in image events- Specified by:
renderImage
in interfaceRenderListener
- Parameters:
renderInfo
- information specifying what to render- Since:
- 5.0.1
- See Also:
RenderListener.renderImage(com.itextpdf.text.pdf.parser.ImageRenderInfo)
-
-