Class ApachePdfBoxDocumentParser
- All Implemented Interfaces:
DocumentParser
Document using the Apache PDFBox library.
Create a parser with the default settings using new ApachePdfBoxDocumentParser(),
or configure it with builder():
DocumentParser parser = ApachePdfBoxDocumentParser.builder()
.includeMetadata(true)
.maxCharacters(20_000_000)
.maxCharactersPerPage(200_000)
.build();
Maximum number of extracted characters
By default, the parser extracts at most 10,000,000 characters of text from a document, and reads at most 100,000 characters from a single page:- The limit per document counts the characters of the returned text, including the spaces and line breaks that PDFBox adds between words, lines and pages. A single page from which PDFBox reads more characters than this limit also exceeds it.
- The limit per page counts the characters of the page's text as PDFBox reads it, before it adds spaces and line breaks. Every character drawn on the page counts, also when it is drawn more than once at the same position.
parse(InputStream) throws a RuntimeException
instead of returning a Document. The exception message names the limit that was passed.
Use ApachePdfBoxDocumentParser.Builder.maxCharacters(Integer) and ApachePdfBoxDocumentParser.Builder.maxCharactersPerPage(Integer) to set different
limits, or -1 for no limit.
The limit per page exists because PDFBox keeps every character of a page in memory, with its position and font, until the whole page is processed.
The limits bound the amount of extracted text. They do not bound all the memory that PDFBox needs to read a
document: some small, highly compressed PDF files can need a lot of memory before these limits apply.
An OutOfMemoryError in the JVM of your application can affect the whole application, even with a heap limit
(-Xmx). So if you parse documents from sources you do not trust, parse them in a separate process with its
own heap limit.
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsModifierConstructorDescriptionCreates a parser with the default settings: no metadata is added to the parsed document, and the default limits on the number of extracted characters apply.ApachePdfBoxDocumentParser(boolean includeMetadata) Creates a parser with the default limits on the number of extracted characters.protectedCreates a parser with the options of the given builder. -
Method Summary
Modifier and TypeMethodDescriptionbuilder()Creates a builder for anApachePdfBoxDocumentParser.parse(InputStream inputStream) Parses a givenInputStreaminto aDocument.
-
Constructor Details
-
ApachePdfBoxDocumentParser
public ApachePdfBoxDocumentParser()Creates a parser with the default settings: no metadata is added to the parsed document, and the default limits on the number of extracted characters apply. -
ApachePdfBoxDocumentParser
public ApachePdfBoxDocumentParser(boolean includeMetadata) Creates a parser with the default limits on the number of extracted characters.- Parameters:
includeMetadata- whether to add the metadata of the PDF file (for example its author) to the parsed document
-
ApachePdfBoxDocumentParser
Creates a parser with the options of the given builder. To create a parser, usebuilder()andApachePdfBoxDocumentParser.Builder.build(). This constructor is for subclasses, which can use it to set the options of the parser, for example:class MyDocumentParser extends ApachePdfBoxDocumentParser { MyDocumentParser() { super(ApachePdfBoxDocumentParser.builder().maxCharacters(-1)); } }- Parameters:
builder- the builder with the options of the parser- Throws:
IllegalArgumentException- if the options are not valid, seeApachePdfBoxDocumentParser.Builder.build()- Since:
- 1.25.0
-
-
Method Details
-
builder
Creates a builder for anApachePdfBoxDocumentParser. Every option is optional: options that are not set (or set tonull) use their default values.- Returns:
- a new builder
- Since:
- 1.25.0
-
parse
Description copied from interface:DocumentParserParses a givenInputStreaminto aDocument. The specific implementation of this method will depend on the type of the document being parsed.Note: This method does not close the provided
InputStream- it is the caller's responsibility to manage the lifecycle of the stream.- Specified by:
parsein interfaceDocumentParser- Parameters:
inputStream- TheInputStreamthat contains the content of theDocument.- Returns:
- The parsed
Document.
-