Class ApachePdfBoxDocumentParser

java.lang.Object
dev.langchain4j.data.document.parser.apache.pdfbox.ApachePdfBoxDocumentParser
All Implemented Interfaces:
DocumentParser

public class ApachePdfBoxDocumentParser extends Object implements DocumentParser
Parses a PDF file into a 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.
If a document or a page contains more characters, 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.

  • 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

      protected ApachePdfBoxDocumentParser(ApachePdfBoxDocumentParser.Builder builder)
      Creates a parser with the options of the given builder. To create a parser, use builder() and ApachePdfBoxDocumentParser.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, see ApachePdfBoxDocumentParser.Builder.build()
      Since:
      1.25.0
  • Method Details