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

public Class PDFText2Markdown

extends PDFTextStripper
Class Inheritance
Known Direct Subclasses
org.apache.pdfbox.tools.FilteredText2Markdown
Imports
java.io.IOException, java.util.ArrayList, .HashSet, .List, .Set, org.apache.pdfbox.pdmodel.font.PDFontDescriptor, org.apache.pdfbox.text.PDFTextStripper, .TextPosition

Convert PDF text to Markdown format. Each line in the PDF is converted to a corresponding Markdown paragraph. Bold and italic formatting is also applied based on font properties.
Author
Saurav Rawat

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
PDFText2Markdown.FontState

A helper class to maintain the current font state.

Field Summary

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

Constructor Summary

AccessConstructor and Description
public
PDFText2Markdown()

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.

private static String

Returns:

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

Escape some Markdown characters.

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 with proper text direction information.

protected void
writeParagraphEnd()

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

Writes the Markdown paragraph end 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 Markdown 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 Markdown characters.

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

Field Detail

fontStateback to summary
private final PDFText2Markdown.FontState fontState

Constructor Detail

PDFText2Markdownback to summary
public PDFText2Markdown()

Constructor.

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.
escapeback to summary
private static String escape(String chars)

Escape some Markdown characters.

Parameters
chars:String

String to be escaped

Returns:String

returns escaped String.

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

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

Write out the article separator 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.
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 Markdown paragraph end 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 Markdown 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 Markdown 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 PDFText2Markdown.FontState

extends Object
Class Inheritance

A helper class to maintain the current font state. Its public methods will emit opening and closing tags as needed and in the correct order.

Responsible for applying Markdown formatting based on font properties. Supports bold and italic text based on font descriptors.

Authors
Axel Dörfler, Saurav Rawat

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 Markdown formatting.
clear
()

Closes all open Markdown formatting.

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 Markdown formatting.

Returns:String

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

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)