Top Description Inners Fields Constructors Methods
org.apache.pdfbox.text

public Class PDFTextStripper

extends LegacyPDFStreamEngine
Class Inheritance
Known Direct Subclasses
org.apache.pdfbox.text.PDFTextStripperByArea
Imports
java.io.BufferedInputStream, .IOException, .InputStream, .InputStreamReader, .LineNumberReader, .StringWriter, .Writer, java.text.Bidi, .Normalizer, java.util.ArrayDeque, .ArrayList, .Collections, .Deque, .HashMap, .Iterator, .LinkedList, .List, .Map, .SortedMap, .SortedSet, .StringTokenizer, .TreeMap, .TreeSet, java.util.regex.Pattern, org.apache.commons.logging.Log, .LogFactory, org.apache.pdfbox.contentstream.operator.markedcontent.BeginMarkedContentSequence, .BeginMarkedContentSequenceWithProperties, .EndMarkedContentSequence, org.apache.pdfbox.cos.COSDictionary, .COSName, org.apache.pdfbox.pdmodel.PDDocument, .PDPage, .PDPageTree, org.apache.pdfbox.pdmodel.common.PDRectangle, org.apache.pdfbox.pdmodel.documentinterchange.markedcontent.PDMarkedContent, org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem, org.apache.pdfbox.pdmodel.interactive.pagenavigation.PDThreadBead, org.apache.pdfbox.util.IterativeMergeSort

This class will take a pdf document and strip out all of the text and ignore the formatting and such. Please note; it is up to clients of this class to verify that a specific user has the correct permissions to extract text from the PDF document. The basic flow of this process is that we get a document and use a series of processXXX() functions that work on smaller and smaller chunks of the page. Eventually, we fully process each page and then print it.
Author
Ben Litchfield

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
PDFTextStripper.LineItem

internal marker class.

private static class
PDFTextStripper.PositionWrapper

wrapper of TextPosition that adds flags to track status as linestart and paragraph start positions.

private static class
PDFTextStripper.WordWithTextPositions

Internal class that maps strings to lists of TextPosition arrays.

Field Summary

Modifier and TypeField and Description
private String
private boolean
private String
private String
private float
private List<PDRectangle>
private final Map<String, TreeMap<Float, TreeSet<Float>>>
protected ArrayList<List<TextPosition>>
charactersByArticle

The charactersByArticle is used to extract text by article divisions.

private final Deque<PDMarkedContent>
private int
private static float
private static float
protected PDDocument
private float
private static final float
private PDOutlineItem
private int
private int
private static final float
private boolean
private boolean
private float
private boolean
inParagraph

True if we started a paragraph but haven't ended it yet.

private static final float
protected static final String
LINE_SEPARATOR

The platform's line separator.

private String
private static final String[]
LIST_ITEM_EXPRESSIONS

a list of regular expressions that match commonly used list item formats, i.e. bullets, numbers, letters, Roman numerals, etc. Not meant to be comprehensive.

private List<Pattern>
private static final Log
private static final float
private static final float
private static final float
private static final Map<Character, Character>
protected Writer
private String
private String
private String
private String
private boolean
private boolean
private float
private PDOutlineItem
private int
private int
private boolean
private String

Constructor Summary

AccessConstructor and Description
public
PDFTextStripper()

Instantiate a new PDFTextStripper object.

Method Summary

Modifier and TypeMethod and Description
public void
beginMarkedContentSequence(COSName
indicates the role or significance of the sequence
tag
,
COSDictionary
optional properties
properties
)

Overrides org.apache.pdfbox.contentstream.PDFStreamEngine.beginMarkedContentSequence.

Called when a marked content group begins

private PDFTextStripper.WordWithTextPositions
createWord(String word, List<TextPosition> wordPositions)

Used within normalize(List) to create a single WordWithTextPositions entry.

protected void
endArticle()

End an article.

protected void
endDocument(PDDocument
The PDF document that is being processed.
document
)

This method is available for subclasses of this class.

public void
protected void
endPage(PDPage
The page we are about to process.
page
)

End a page.

private void
public boolean

Returns:

true if some more text formatting will be added
getAddMoreFormatting
()

This will tell if the text stripper should add some more text formatting.

public String

Returns:

the article end string
getArticleEnd
()

Returns the string which will be used at the end of an article.

public String

Returns:

the article start string
getArticleStart
()

Returns the string which will be used at the beginning of an article.

public float

Returns:

The current tolerance / scaling factor
getAverageCharTolerance
()

Get the current character width-based tolerance value that is being used to estimate where spaces in text should be added.

protected List<List<TextPosition>>

Returns:

A double List of TextPositions for all text strings on the page.
getCharactersByArticle
()

Character strings are grouped by articles.

protected int

Returns:

A 1 based number representing the current page.
getCurrentPageNo
()

Get the current page number that is being processed.

public float

Returns:

the character height multiple for max allowed whitespace between lines in the same paragraph.
getDropThreshold
()

the minimum whitespace, as a multiple of the max height of the current characters beyond which the current line start is considered to be a paragraph start.

public PDOutlineItem

Returns:

The ending bookmark.
getEndBookmark
()

Get the bookmark where text extraction should end, inclusive.

public int

Returns:

Value of property endPage.
getEndPage
()

This will get the last page that will be extracted.

public boolean

Returns:

true is space glyphs in the content stream text rendering instructions will be ignored - default is false
getIgnoreContentStreamSpaceGlyphs
()

Determines whether spaces in the content stream text rendering instructions will be ignored during text extraction.

public float

Returns:

the number of whitespace character widths to use when detecting paragraph indents.
getIndentThreshold
()

returns the multiple of whitespace character widths for the current text which the current line start can be indented from the previous line start beyond which the current line start is considered to be a paragraph start.

public String

Returns:

The desired line separator string.
getLineSeparator
()

This will get the line separator.

protected List<Pattern>

Returns:

a list of Pattern objects.
getListItemPatterns
()

returns a list of regular expression Patterns representing different common list item formats.

protected Writer

Returns:

The stream that output is being written to.
getOutput
()

The output stream that is being written to.

public String

Returns:

the page end string
getPageEnd
()

Returns the string which will be used at the end of a page.

public String

Returns:

the page start string
getPageStart
()

Returns the string which will be used at the beginning of a page.

public String

Returns:

the paragraph end string
getParagraphEnd
()

Returns the string which will be used at the end of a paragraph.

public String

Returns:

the paragraph start string
getParagraphStart
()

Returns the string which will be used at the beginning of a paragraph.

public boolean

Returns:

If the text will be grouped by beads.
getSeparateByBeads
()

This will tell if the text stripper should separate by beads.

public boolean

Returns:

true If the text tokens will be sorted before being written.
getSortByPosition
()

This will tell if the text stripper should sort the text tokens before writing to the stream.

public float

Returns:

The current tolerance / scaling factor
getSpacingTolerance
()

Get the current space width-based tolerance value that is being used to estimate where spaces in text should be added.

public PDOutlineItem

Returns:

The starting bookmark.
getStartBookmark
()

Get the bookmark where text extraction should start, inclusive.

public int

Returns:

Value of property startPage.
getStartPage
()

This is the page that the text extraction will start on.

public boolean

Returns:

Returns the suppressDuplicateOverlappingText.
getSuppressDuplicateOverlappingText
()

public String

Returns:

The text of the PDF document.
getText
(PDDocument
The document to get the text from.
doc
)

This will return the text of a document.

public String

Returns:

The desired word separator string.
getWordSeparator
()

This will get the word separator.

private String

Returns:

new word with the correct direction of the containing characters
handleDirection
(String
The word that shall be processed
word
)

Handles the LTR and RTL direction of the given words.

private PDFTextStripper.PositionWrapper

Returns:

start position of the last line
handleLineSeparation
(PDFTextStripper.PositionWrapper
the current text position
current
,
PDFTextStripper.PositionWrapper
the previous text position
lastPosition
,
PDFTextStripper.PositionWrapper
the last text position that followed a line separator.
lastLineStartPosition
,
float
max height for positions since lastLineStartPosition
maxHeightForLine
)

handles the line separator for a new line given the specified current and previous TextPositions.

private void
isParagraphSeparation(PDFTextStripper.PositionWrapper
the current text position. This may have its isParagraphStart or isHangingIndent flags set upon return.
position
,
PDFTextStripper.PositionWrapper
the previous text position (should not be null).
lastPosition
,
PDFTextStripper.PositionWrapper
the last text position that followed a line separator, or null.
lastLineStartPosition
,
float
max height for text positions since lasLineStartPosition.
maxHeightForLine
)

tests the relationship between the last text position, the current text position and the last text position that followed a line separator to decide if the gap represents a paragraph separation.

private Pattern

Returns:

the matching pattern
matchListItemPattern
(PDFTextStripper.PositionWrapper
position
pw
)

returns the list item Pattern object that matches the text at the specified PositionWrapper or null if the text does not match such a pattern.

protected static Pattern

Returns:

matching pattern
matchPattern
(String
the string to be searched
string
,
List<Pattern>
list of patterns
patterns
)

iterates over the specified list of Patterns until it finds one that matches the specified string.

private float
multiplyFloat(float value1, float value2)

private List<PDFTextStripper.WordWithTextPositions>

Returns:

a list of strings, one string for every word
normalize
(List<PDFTextStripper.LineItem>
list of TextPositions
line
)

Normalize the given list of TextPositions.

private StringBuilder

Returns:

The StringBuilder that must be used when calling this method.
normalizeAdd
(List<PDFTextStripper.WordWithTextPositions> normalized, StringBuilder lineBuilder, List<TextPosition> wordPositions, PDFTextStripper.LineItem item)

Used within normalize(List) to handle a TextPosition.

private String

Returns:

Normalized word
normalizeWord
(String
Word to normalize
word
)

Normalize certain Unicode characters.

private boolean
overlap(float y1, float height1, float y2, float height2)

private static void
parseBidiFile(InputStream
- The bidi file as inputstream
inputStream
)

This method parses the bidi file provided as inputstream.

public void
processPage(PDPage
The page to process.
page
)

Overrides org.apache.pdfbox.text.LegacyPDFStreamEngine.processPage.

This will process the contents of a page.

protected void
processPages(PDPageTree
The pages object in the document.
pages
)

This will process all of the pages and the text that is in them.

protected void
processTextPosition(TextPosition
The text to process.
text
)

Overrides org.apache.pdfbox.text.LegacyPDFStreamEngine.processTextPosition.

This will process a TextPosition object and add the text to the list of characters on a page.

private void
removeContainedSpaces(List<TextPosition> textList)

Remove all space characters if contained within the adjacent letters

private void
public void
setAddMoreFormatting(boolean
Tell PDFBox to add some more text formatting
newAddMoreFormatting
)

There will some additional text formatting be added if addMoreFormatting is set to true.

public void
setArticleEnd(String
the article end string
articleEndValue
)

Sets the string which will be used at the end of an article.

public void
setArticleStart(String
the article start string
articleStartValue
)

Sets the string which will be used at the beginning of an article.

public void
setAverageCharTolerance(float
average tolerance / scaling factor to use
averageCharToleranceValue
)

Set the character width-based tolerance value that is used to estimate where spaces in text should be added.

public void
setDropThreshold(float
the character height multiple for max allowed whitespace between lines in the same paragraph.
dropThresholdValue
)

sets the minimum whitespace, as a multiple of the max height of the current characters beyond which the current line start is considered to be a paragraph start.

public void
setEndBookmark(PDOutlineItem
The ending bookmark.
aEndBookmark
)

Set the bookmark where the text extraction should stop.

public void
setEndPage(int
New value of 1-based endPage property.
endPageValue
)

This will set the last page to be extracted by this class.

public void
setIgnoreContentStreamSpaceGlyphs(boolean
whether PDF Box should ignore context stream spaces
newIgnoreContentStreamSpaceGlyphs
)

Instruct the algorithm to ignore any spaces in the text rendering instructions in the content stream, and instead rely purely on the algorithm to determine where word breaks are.

public void
setIndentThreshold(float
the number of whitespace character widths to use when detecting paragraph indents.
indentThresholdValue
)

sets the multiple of whitespace character widths for the current text which the current line start can be indented from the previous line start beyond which the current line start is considered to be a paragraph start.

public void
setLineSeparator(String
The desired line separator string.
separator
)

Set the desired line separator for output text.

protected void
setListItemPatterns(List<Pattern>
list of patterns
patterns
)

use to supply a different set of regular expression patterns for matching list item starts.

public void
setPageEnd(String
the page end string
pageEndValue
)

Sets the string which will be used at the end of a page.

public void
setPageStart(String
the page start string
pageStartValue
)

Sets the string which will be used at the beginning of a page.

public void
setParagraphEnd(String
the paragraph end string
s
)

Sets the string which will be used at the end of a paragraph.

public void
setParagraphStart(String
the paragraph start string
s
)

Sets the string which will be used at the beginning of a paragraph.

public void
setShouldSeparateByBeads(boolean
The new grouping of beads.
aShouldSeparateByBeads
)

Set if the text stripper should group the text output by a list of beads.

public void
setSortByPosition(boolean
Tell PDFBox to sort the text positions.
newSortByPosition
)

The order of the text tokens in a PDF file may not be in the same as they appear visually on the screen.

public void
setSpacingTolerance(float
tolerance / scaling factor to use
spacingToleranceValue
)

Set the space width-based tolerance value that is used to estimate where spaces in text should be added.

public void
setStartBookmark(PDOutlineItem
The starting bookmark.
aStartBookmark
)

Set the bookmark where text extraction should start, inclusive.

public void
setStartPage(int
New value of 1-based startPage property.
startPageValue
)

This will set the first page to be extracted by this class.

public void
setSuppressDuplicateOverlappingText(boolean
The suppressDuplicateOverlappingText to set.
suppressDuplicateOverlappingTextValue
)

By default the text stripper will attempt to remove text that overlapps each other.

public void
setWordSeparator(String
The desired page separator string.
separator
)

Set the desired word separator for output text.

protected void
startArticle()

Start a new article, which is typically defined as a column on a single page (also referred to as a bead).

protected void
startArticle(boolean
true if primary direction of text is left to right.
isLTR
)

Start a new article, which is typically defined as a column on a single page (also referred to as a bead).

protected void
startDocument(PDDocument
The PDF document that is being processed.
document
)

This method is available for subclasses of this class.

protected void
startPage(PDPage
The page we are about to process.
page
)

Start a new page.

private boolean
within(float
The first number to compare to.
first
,
float
The second number to compare to.
second
,
float
The allowed variance.
variance
)

This will determine of two floating point numbers are within a specified variance.

protected void
writeCharacters(TextPosition
The text to write to the stream.
text
)

Write the string in TextPosition to the output stream.

private void
writeLine(List<PDFTextStripper.WordWithTextPositions>
a list with the words of the given line
line
)

Write a list of string containing a whole line of a document.

protected void
writeLineSeparator()

Write the line separator value to the output stream.

protected void
writePage()

This will print the text of the processed page to "output".

protected void
writePageEnd()

Write something (if defined) at the end of a page.

protected void
writePageStart()

Write something (if defined) at the start of a page.

protected void
writeParagraphEnd()

Write something (if defined) at the end of a paragraph.

protected void
writeParagraphSeparator()

writes the paragraph separator string to the output.

protected void
writeParagraphStart()

Write something (if defined) at the start of a paragraph.

protected void
writeString(String
The text to write to the stream.
text
,
List<TextPosition>
The TextPositions belonging to the text.
textPositions
)

Write a Java string to the output stream.

protected void
writeString(String
The text to write to the stream.
text
)

Write a Java string to the output stream.

public void
writeText(PDDocument
The document to get the data from.
doc
,
Writer
The location to put the text.
outputStream
)

This will take a PDDocument and write the text of that document to the print writer.

protected void
writeWordSeparator()

Write the word separator value to the output stream.

Inherited from org.apache.pdfbox.text.LegacyPDFStreamEngine:
computeFontHeightshowGlyph