Class PDFParser

  • All Implemented Interfaces:
    Serializable, Initializable, Parser, RenderingParser

    public class PDFParser
    extends AbstractParser
    implements RenderingParser, Initializable
    PDF parser.

    This parser can process also encrypted PDF documents if the required password is given as a part of the input metadata associated with a document. If no password is given, then this parser will try decrypting the document using the empty password that's often used with PDFs. If the PDF contains any embedded documents (for example as part of a PDF package) then this parser will use the EmbeddedDocumentExtractor to handle them.

    As of Tika 1.6, it is possible to extract inline images with the EmbeddedDocumentExtractor as if they were regular attachments. By default, this feature is turned off because of the potentially enormous number and size of inline images. To turn this feature on, see PDFParserConfig.setExtractInlineImages(boolean).

    Please note that many pdfs do not store table structures. So you should not expect table markup for what looks like a table. It takes significant computation to identify and then correctly extract tables from PDFs. As of this writing, the PDFParser extracts text within tables, but it does not compute table cell boundaries or table row boundaries. Please see tabula for one project that tries to maintain the structure of tables represented in PDFs. If your PDFs contain marked content or tags, consider PDFParserConfig.setExtractMarkedContent(boolean)

    See Also:
    Serialized Form
    • Constructor Detail

      • PDFParser

        public PDFParser()
    • Method Detail

      • getSupportedTypes

        public Set<MediaType> getSupportedTypes​(ParseContext context)
        Description copied from interface: Parser
        Returns the set of media types supported by this parser when used with the given parse context.
        Specified by:
        getSupportedTypes in interface Parser
        context - parse context
        immutable set of media types
      • parse

        public void parse​(InputStream stream,
                          ContentHandler handler,
                          Metadata metadata,
                          ParseContext context)
                   throws IOException,
        Description copied from interface: Parser
        Parses a document stream into a sequence of XHTML SAX events. Fills in related document metadata in the given metadata object.

        The given document stream is consumed but not closed by this method. The responsibility to close the stream remains on the caller.

        Information about the parsing context can be passed in the context parameter. See the parser implementations for the kinds of context information they expect.

        Specified by:
        parse in interface Parser
        stream - the document stream (input)
        handler - handler for the XHTML SAX events (output)
        metadata - document metadata (input and output)
        context - parse context
        IOException - if the document stream could not be read
        SAXException - if the SAX events could not be processed
        TikaException - if the document could not be parsed
      • setPDFParserConfig

        public void setPDFParserConfig​(PDFParserConfig config)
      • setEnableAutoSpace

        public void setEnableAutoSpace​(boolean v)
        If true (the default), the parser should estimate where spaces should be inserted between words. For many PDFs this is necessary as they do not include explicit whitespace characters.
      • isExtractAnnotationText

        public boolean isExtractAnnotationText()
        If true, text in annotations will be extracted.
      • setExtractAnnotationText

        public void setExtractAnnotationText​(boolean v)
        If true (the default), text in annotations will be extracted.
      • setSuppressDuplicateOverlappingText

        public void setSuppressDuplicateOverlappingText​(boolean v)
        If true, the parser should try to remove duplicated text over the same region. This is needed for some PDFs that achieve bolding by re-writing the same text in the same area. Note that this can slow down extraction substantially (PDFBOX-956) and sometimes remove characters that were not in fact duplicated (PDFBOX-1155). By default this is disabled.
      • setSortByPosition

        public void setSortByPosition​(boolean v)
        If true, sort text tokens by their x/y position before extracting text. This may be necessary for some PDFs (if the text tokens are not rendered "in order"), while for other PDFs it can produce the wrong result (for example if there are 2 columns, the text will be interleaved). Default is false.
      • setOcrStrategy

        public void setOcrStrategy​(String ocrStrategyString)
      • getOcrStrategy

        public String getOcrStrategy()
      • setOcrStrategyAuto

        public void setOcrStrategyAuto​(String ocrStrategyAuto)
      • getOcrStrategyAuto

        public String getOcrStrategyAuto()
      • setOcrRenderingStrategy

        public void setOcrRenderingStrategy​(String ocrRenderingStrategy)
      • getOcrRenderingStrategy

        public String getOcrRenderingStrategy()
      • setOcrImageType

        public void setOcrImageType​(String imageType)
      • getOcrImageType

        public String getOcrImageType()
      • setOcrDPI

        public void setOcrDPI​(int dpi)
      • getOcrDPI

        public int getOcrDPI()
      • setOcrImageQuality

        public void setOcrImageQuality​(float imageQuality)
      • getOcrImageQuality

        public float getOcrImageQuality()
      • setOcrImageFormatName

        public void setOcrImageFormatName​(String formatName)
      • getOcrImageFormatName

        public String getOcrImageFormatName()
      • setExtractBookmarksText

        public void setExtractBookmarksText​(boolean extractBookmarksText)
      • isExtractBookmarksText

        public boolean isExtractBookmarksText()
      • setExtractInlineImages

        public void setExtractInlineImages​(boolean extractInlineImages)
      • isExtractInlineImages

        public boolean isExtractInlineImages()
      • setExtractInlineImageMetadataOnly

        public void setExtractInlineImageMetadataOnly​(boolean extractInlineImageMetadataOnly)
      • isExtractInlineImageMetadataOnly

        public boolean isExtractInlineImageMetadataOnly()
      • setAverageCharTolerance

        public void setAverageCharTolerance​(float averageCharTolerance)
      • getAverageCharTolerance

        public float getAverageCharTolerance()
      • setSpacingTolerance

        public void setSpacingTolerance​(float spacingTolerance)
      • getSpacingTolerance

        public float getSpacingTolerance()
      • setCatchIntermediateExceptions

        public void setCatchIntermediateExceptions​(boolean catchIntermediateExceptions)
      • isCatchIntermediateExceptions

        public boolean isCatchIntermediateExceptions()
      • setExtractAcroFormContent

        public void setExtractAcroFormContent​(boolean extractAcroFormContent)
      • isExtractAcroFormContent

        public boolean isExtractAcroFormContent()
      • setIfXFAExtractOnlyXFA

        public void setIfXFAExtractOnlyXFA​(boolean ifXFAExtractOnlyXFA)
      • isIfXFAExtractOnlyXFA

        public boolean isIfXFAExtractOnlyXFA()
      • setAllowExtractionForAccessibility

        public void setAllowExtractionForAccessibility​(boolean allowExtractionForAccessibility)
      • isAllowExtractionForAccessibility

        public boolean isAllowExtractionForAccessibility()
      • setExtractUniqueInlineImagesOnly

        public void setExtractUniqueInlineImagesOnly​(boolean extractUniqueInlineImagesOnly)
      • isExtractUniqueInlineImagesOnly

        public boolean isExtractUniqueInlineImagesOnly()
      • setExtractActions

        public void setExtractActions​(boolean extractActions)
      • isExtractActions

        public boolean isExtractActions()
      • setExtractFontNames

        public void setExtractFontNames​(boolean extractFontNames)
      • isExtractFontNames

        public boolean isExtractFontNames()
      • setSetKCMS

        public void setSetKCMS​(boolean setKCMS)
      • isSetKCMS

        public boolean isSetKCMS()
      • setDetectAngles

        public void setDetectAngles​(boolean detectAngles)
      • isDetectAngles

        public boolean isDetectAngles()
      • setExtractMarkedContent

        public void setExtractMarkedContent​(boolean extractMarkedContent)
      • isExtractMarkedContent

        public boolean isExtractMarkedContent()
      • setDropThreshold

        public void setDropThreshold​(float dropThreshold)
      • getDropThreshold

        public float getDropThreshold()
      • setMaxMainMemoryBytes

        public void setMaxMainMemoryBytes​(long maxMainMemoryBytes)
      • setExtractIncrementalUpdateInfo

        public void setExtractIncrementalUpdateInfo​(boolean setExtractIncrementalUpdateInfo)
        Whether or not to scan a PDF for incremental updates.
        setExtractIncrementalUpdateInfo -
      • getMaxMainMemoryBytes

        public long getMaxMainMemoryBytes()
      • isExtractIncrementalUpdateInfo

        public boolean isExtractIncrementalUpdateInfo()
      • setParseIncrementalUpdates

        public void setParseIncrementalUpdates​(boolean parseIncrementalUpdates)
        If set to true, this will parse incremental updates if they exist within a PDF. If set to true, this will override setExtractIncrementalUpdateInfo(boolean).
        parseIncrementalUpdates -
      • isParseIncrementalUpdates

        public boolean isParseIncrementalUpdates()
      • setMaxIncrementalUpdates

        public void setMaxIncrementalUpdates​(int maxIncrementalUpdates)
        Set the maximum number of incremental updates to parse
        maxIncrementalUpdates -
      • getMaxIncrementalUpdates

        public int getMaxIncrementalUpdates()
      • getRenderer

        public Renderer getRenderer()
      • setImageStrategy

        public void setImageStrategy​(String imageStrategy)
      • getImageStrategy

        public String getImageStrategy()