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

public Class PDFText2HTML

extends PDFTextStripper
Class Inheritance
Imports
java.io.IOException, java.util.ArrayList, .HashSet, .Iterator, .List, .Set, org.apache.pdfbox.pdmodel.PDDocument, org.apache.pdfbox.pdmodel.font.PDFontDescriptor, org.apache.pdfbox.text.PDFTextStripper, .TextPosition

Wrap stripped text in simple HTML, trying to form HTML paragraphs. Paragraphs broken by pages, columns, or figures are not mended.
Author
John J Barton

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
PDFText2HTML.FontState

A helper class to maintain the current font state.

Field Summary

Modifier and TypeField and Description
private final PDFText2HTML.FontState
private static final int
Inherited from org.apache.pdfbox.text.PDFTextStripper:
charactersByArticledocumentLINE_SEPARATORoutput

Constructor Summary

AccessConstructor and Description
public
PDFText2HTML()

Constructor.

Method Summary

Modifier and TypeMethod and Description
private static void
appendEscaped(StringBuilder builder, char character)

protected void
endArticle()

Overrides org.apache.pdfbox.text.PDFTextStripper.endArticle.

Write out the article separator.

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

Overrides org.apache.pdfbox.text.PDFTextStripper.endDocument.

This method is available for subclasses of this class.

private static String

Returns:

returns escaped String.
escape
(String
String to be escaped
chars
)

Escape some HTML characters.

protected String

Returns:

returns the title.
getTitle
()

This method will attempt to guess the title of the document using either the document properties or the first lines of text.

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

Overrides org.apache.pdfbox.text.PDFTextStripper.startArticle.

Write out the article separator (div tag) with proper text direction information.

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

Overrides org.apache.pdfbox.text.PDFTextStripper.startDocument.

This method is available for subclasses of this class.

protected void
writeParagraphEnd()

Overrides org.apache.pdfbox.text.PDFTextStripper.writeParagraphEnd.

Writes the paragraph end "</p>" to the output.

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

Overrides org.apache.pdfbox.text.PDFTextStripper.writeString.

Write a string to the output stream, maintain font state, and escape some HTML characters.

protected void
writeString(String
String to be written to the stream
chars
)

Overrides org.apache.pdfbox.text.PDFTextStripper.writeString.

Write a string to the output stream and escape some HTML characters.

Inherited from org.apache.pdfbox.text.PDFTextStripper:
beginMarkedContentSequenceendMarkedContentSequenceendPagegetAddMoreFormattinggetArticleEndgetArticleStartgetAverageCharTolerancegetCharactersByArticlegetCurrentPageNogetDropThresholdgetEndBookmarkgetEndPagegetIgnoreContentStreamSpaceGlyphsgetIndentThresholdgetLineSeparatorgetListItemPatternsgetOutputgetPageEndgetPageStartgetParagraphEndgetParagraphStartgetSeparateByBeadsgetSortByPositiongetSpacingTolerancegetStartBookmarkgetStartPagegetSuppressDuplicateOverlappingTextgetTextgetWordSeparatormatchPatternprocessPageprocessPagesprocessTextPositionsetAddMoreFormattingsetArticleEndsetArticleStartsetAverageCharTolerancesetDropThresholdsetEndBookmarksetEndPagesetIgnoreContentStreamSpaceGlyphssetIndentThresholdsetLineSeparatorsetListItemPatternssetPageEndsetPageStartsetParagraphEndsetParagraphStartsetShouldSeparateByBeadssetSortByPositionsetSpacingTolerancesetStartBookmarksetStartPagesetSuppressDuplicateOverlappingTextsetWordSeparatorstartArticlestartPagewriteCharacterswriteLineSeparatorwritePagewritePageEndwritePageStartwriteParagraphSeparatorwriteParagraphStartwriteTextwriteWordSeparator

Field Detail

fontStateback to summary
private final PDFText2HTML.FontState fontState
INITIAL_PDF_TO_HTML_BYTESback to summary
private static final int INITIAL_PDF_TO_HTML_BYTES

Constructor Detail

PDFText2HTMLback to summary
public PDFText2HTML() throws IOException

Constructor.

Exceptions
IOException:
If there is an error during initialization.

Method Detail

appendEscapedback to summary
private static void appendEscaped(StringBuilder builder, char character)
endArticleback to summary
protected void endArticle() throws IOException

Overrides org.apache.pdfbox.text.PDFTextStripper.endArticle.

Write out the article separator.

Annotations
@Override
Exceptions
IOException:
If there is an error writing to the stream.
endDocumentback to summary
public void endDocument(PDDocument document) throws IOException

Overrides org.apache.pdfbox.text.PDFTextStripper.endDocument.

Doc from org.apache.pdfbox.text.PDFTextStripper.endDocument.

This method is available for subclasses of this class. It will be called after processing of the document finishes.

Parameters
document:PDDocument

The PDF document that is being processed.

Annotations
@Override
Exceptions
IOException:
If an IO error occurs.
escapeback to summary
private static String escape(String chars)

Escape some HTML characters.

Parameters
chars:String

String to be escaped

Returns:String

returns escaped String.

getTitleback to summary
protected String getTitle()

This method will attempt to guess the title of the document using either the document properties or the first lines of text.

Returns:String

returns the title.

startArticleback to summary
protected void startArticle(boolean isLTR) throws IOException

Overrides org.apache.pdfbox.text.PDFTextStripper.startArticle.

Write out the article separator (div tag) with proper text direction information.

Parameters
isLTR:boolean

true if direction of text is left to right

Annotations
@Override
Exceptions
IOException:
If there is an error writing to the stream.
startDocumentback to summary
protected void startDocument(PDDocument document) throws IOException

Overrides org.apache.pdfbox.text.PDFTextStripper.startDocument.

Doc from org.apache.pdfbox.text.PDFTextStripper.startDocument.

This method is available for subclasses of this class. It will be called before processing of the document start.

Parameters
document:PDDocument

The PDF document that is being processed.

Annotations
@Override
Exceptions
IOException:
If an IO error occurs.
writeParagraphEndback to summary
protected void writeParagraphEnd() throws IOException

Overrides org.apache.pdfbox.text.PDFTextStripper.writeParagraphEnd.

Doc from org.apache.pdfbox.text.PDFTextStripper.writeParagraphEnd.

Writes the paragraph end "</p>" to the output. Furthermore, it will also clear the font state. Write something (if defined) at the end of a paragraph.

Annotations
@Override
Exceptions
IOException:
if something went wrong
writeStringback to summary
protected void writeString(String text, List<TextPosition> textPositions) throws IOException

Overrides org.apache.pdfbox.text.PDFTextStripper.writeString.

Write a string to the output stream, maintain font state, and escape some HTML characters. The font state is only preserved per word.

Parameters
text:String

The text to write to the stream.

textPositions:List<TextPosition>

the corresponding text positions

Annotations
@Override
Exceptions
IOException:
If there is an error writing to the stream.
writeStringback to summary
protected void writeString(String chars) throws IOException

Overrides org.apache.pdfbox.text.PDFTextStripper.writeString.

Write a string to the output stream and escape some HTML characters.

Parameters
chars:String

String to be written to the stream

Annotations
@Override
Exceptions
IOException:
If there is an error writing to the stream.
org.apache.pdfbox.tools back to summary

private Class PDFText2HTML.FontState

extends Object
Class Inheritance

A helper class to maintain the current font state. It's public methods will emit opening and closing tags as needed, and in the correct order.
Author
Axel Dörfler

Field Summary

Modifier and TypeField and Description
private final List<String>
private final Set<String>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public String

Returns:

A string that contains the closing tags of all currently open states.
clear
()

Closes all open states.

private String
private String
private int
closeUntil(StringBuilder tagsBuilder, String endTag)

private boolean
private boolean
private String
open(String tag)

private String
public String

Returns:

A string that contains the text including tag changes caused by its font state.
push
(String text, List<TextPosition> textPositions)

Pushes new TextPositions into the font state.

protected String
push(StringBuilder buffer, char character, TextPosition textPosition)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

stateListback to summary
private final List<String> stateList
stateSetback to summary
private final Set<String> stateSet

Constructor Detail

FontStateback to summary
private FontState()

Method Detail

clearback to summary
public String clear()

Closes all open states.

Returns:String

A string that contains the closing tags of all currently open states.

closeback to summary
private String close(String tag)
closeTagback to summary
private String closeTag(String tag)
closeUntilback to summary
private int closeUntil(StringBuilder tagsBuilder, String endTag)
isBoldback to summary
private boolean isBold(PDFontDescriptor descriptor)
isItalicback to summary
private boolean isItalic(PDFontDescriptor descriptor)
openback to summary
private String open(String tag)
openTagback to summary
private String openTag(String tag)
pushback to summary
public String push(String text, List<TextPosition> textPositions)

Pushes new TextPositions into the font state. The state is only preserved correctly for each letter if the number of letters in text matches the number of TextPosition objects. Otherwise, it's done once for the complete array (just by looking at its first entry).

Returns:String

A string that contains the text including tag changes caused by its font state.

pushback to summary
protected String push(StringBuilder buffer, char character, TextPosition textPosition)