Package org.apache.tika.parser.pdf
Class PDFParser
java.lang.Object
org.apache.tika.parser.pdf.PDFParser
- All Implemented Interfaces:
Serializable
,Initializable
,Parser
,RenderingParser
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:
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
float
float
int
long
int
float
protected org.apache.pdfbox.pdmodel.PDDocument
getPDDocument
(InputStream inputStream, String password, org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction streamCacheCreateFunction, Metadata metadata, ParseContext parseContext) protected org.apache.pdfbox.pdmodel.PDDocument
getPDDocument
(InputStream stream, TikaInputStream tstream, String password, org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction streamCacheCreateFunction, Metadata metadata, ParseContext context) protected org.apache.pdfbox.pdmodel.PDDocument
getPDDocument
(Path path, String password, org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction streamCacheCreateFunction, Metadata metadata, ParseContext parseContext) float
getSupportedTypes
(ParseContext context) Returns the set of media types supported by this parser when used with the given parse context.void
initialize
(Map<String, Param> params) This is a no-op.boolean
boolean
boolean
boolean
boolean
boolean
boolean
If true, text in annotations will be extracted.boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
void
parse
(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) Parses a document stream into a sequence of XHTML SAX events.void
setAllowExtractionForAccessibility
(boolean allowExtractionForAccessibility) void
setAverageCharTolerance
(float averageCharTolerance) void
setCatchIntermediateExceptions
(boolean catchIntermediateExceptions) void
setDetectAngles
(boolean detectAngles) void
setDropThreshold
(float dropThreshold) void
setEnableAutoSpace
(boolean v) If true (the default), the parser should estimate where spaces should be inserted between words.void
setExtractAcroFormContent
(boolean extractAcroFormContent) void
setExtractActions
(boolean extractActions) void
setExtractAnnotationText
(boolean v) If true (the default), text in annotations will be extracted.void
setExtractBookmarksText
(boolean extractBookmarksText) void
setExtractFontNames
(boolean extractFontNames) void
setExtractIncrementalUpdateInfo
(boolean setExtractIncrementalUpdateInfo) Whether or not to scan a PDF for incremental updates.void
setExtractInlineImageMetadataOnly
(boolean extractInlineImageMetadataOnly) void
setExtractInlineImages
(boolean extractInlineImages) void
setExtractMarkedContent
(boolean extractMarkedContent) void
setExtractUniqueInlineImagesOnly
(boolean extractUniqueInlineImagesOnly) void
setIfXFAExtractOnlyXFA
(boolean ifXFAExtractOnlyXFA) void
setImageGraphicsEngineFactory
(ImageGraphicsEngineFactory imageGraphicsEngineFactory) void
setImageStrategy
(String imageStrategy) void
setMaxIncrementalUpdates
(int maxIncrementalUpdates) Set the maximum number of incremental updates to parsevoid
setMaxMainMemoryBytes
(long maxMainMemoryBytes) void
setOcrDPI
(int dpi) void
setOcrImageFormatName
(String formatName) void
setOcrImageQuality
(float imageQuality) void
setOcrImageType
(String imageType) void
setOcrRenderingStrategy
(String ocrRenderingStrategy) void
setOcrStrategy
(String ocrStrategyString) void
setOcrStrategyAuto
(String ocrStrategyAuto) void
setParseIncrementalUpdates
(boolean parseIncrementalUpdates) If set to true, this will parse incremental updates if they exist within a PDF.void
setPDFParserConfig
(PDFParserConfig config) void
setRenderer
(Renderer renderer) void
setSetKCMS
(boolean setKCMS) void
setSortByPosition
(boolean v) If true, sort text tokens by their x/y position before extracting text.void
setSpacingTolerance
(float spacingTolerance) void
setSuppressDuplicateOverlappingText
(boolean v) If true, the parser should try to remove duplicated text over the same region.void
setThrowOnEncryptedPayload
(boolean throwOnEncryptedPayload) If the file is a 'Collection' and contains an embedded file with a defined 'AssociatedFile' value of 'EncryptedPayload', then throw anEncryptedDocumentException
.
-
Field Details
-
MEDIA_TYPE
-
-
Constructor Details
-
PDFParser
public PDFParser()
-
-
Method Details
-
getSupportedTypes
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 interfaceParser
- Parameters:
context
- parse context- Returns:
- immutable set of media types
-
parse
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException 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 interfaceParser
- Parameters:
stream
- the document stream (input)handler
- handler for the XHTML SAX events (output)metadata
- document metadata (input and output)context
- parse context- Throws:
IOException
- if the document stream could not be readSAXException
- if the SAX events could not be processedTikaException
- if the document could not be parsed
-
getPDDocument
protected org.apache.pdfbox.pdmodel.PDDocument getPDDocument(InputStream stream, TikaInputStream tstream, String password, org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction streamCacheCreateFunction, Metadata metadata, ParseContext context) throws IOException, EncryptedDocumentException -
getPDDocument
protected org.apache.pdfbox.pdmodel.PDDocument getPDDocument(InputStream inputStream, String password, org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction streamCacheCreateFunction, Metadata metadata, ParseContext parseContext) throws IOException - Throws:
IOException
-
getPDDocument
protected org.apache.pdfbox.pdmodel.PDDocument getPDDocument(Path path, String password, org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction streamCacheCreateFunction, Metadata metadata, ParseContext parseContext) throws IOException - Throws:
IOException
-
getPDFParserConfig
-
setPDFParserConfig
-
isEnableAutoSpace
public boolean isEnableAutoSpace()- See Also:
-
setEnableAutoSpace
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
If true (the default), text in annotations will be extracted. -
isSuppressDuplicateOverlappingText
public boolean isSuppressDuplicateOverlappingText() -
setSuppressDuplicateOverlappingText
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. -
isSortByPosition
public boolean isSortByPosition()- See Also:
-
setSortByPosition
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
-
getOcrStrategy
-
setOcrStrategyAuto
-
getOcrStrategyAuto
-
setOcrRenderingStrategy
-
getOcrRenderingStrategy
-
setOcrImageType
-
getOcrImageType
-
setOcrDPI
-
getOcrDPI
public int getOcrDPI() -
setOcrImageQuality
-
getOcrImageQuality
public float getOcrImageQuality() -
setOcrImageFormatName
-
getOcrImageFormatName
-
setExtractBookmarksText
-
isExtractBookmarksText
public boolean isExtractBookmarksText() -
setExtractInlineImages
-
isExtractInlineImages
public boolean isExtractInlineImages() -
setExtractInlineImageMetadataOnly
-
isExtractInlineImageMetadataOnly
public boolean isExtractInlineImageMetadataOnly() -
setAverageCharTolerance
-
getAverageCharTolerance
public float getAverageCharTolerance() -
setSpacingTolerance
-
getSpacingTolerance
public float getSpacingTolerance() -
setCatchIntermediateExceptions
-
isCatchIntermediateExceptions
public boolean isCatchIntermediateExceptions() -
setExtractAcroFormContent
-
isExtractAcroFormContent
public boolean isExtractAcroFormContent() -
setIfXFAExtractOnlyXFA
-
isIfXFAExtractOnlyXFA
public boolean isIfXFAExtractOnlyXFA() -
setAllowExtractionForAccessibility
-
isAllowExtractionForAccessibility
public boolean isAllowExtractionForAccessibility() -
setExtractUniqueInlineImagesOnly
-
isExtractUniqueInlineImagesOnly
public boolean isExtractUniqueInlineImagesOnly() -
setExtractActions
-
isExtractActions
public boolean isExtractActions() -
setExtractFontNames
-
isExtractFontNames
public boolean isExtractFontNames() -
setSetKCMS
-
isSetKCMS
public boolean isSetKCMS() -
setDetectAngles
-
isDetectAngles
public boolean isDetectAngles() -
setExtractMarkedContent
-
isExtractMarkedContent
public boolean isExtractMarkedContent() -
setDropThreshold
-
getDropThreshold
public float getDropThreshold() -
setMaxMainMemoryBytes
-
setExtractIncrementalUpdateInfo
Whether or not to scan a PDF for incremental updates.- Parameters:
setExtractIncrementalUpdateInfo
-
-
getMaxMainMemoryBytes
public long getMaxMainMemoryBytes() -
isExtractIncrementalUpdateInfo
public boolean isExtractIncrementalUpdateInfo() -
setParseIncrementalUpdates
If set to true, this will parse incremental updates if they exist within a PDF. If set totrue
, this will overridesetExtractIncrementalUpdateInfo(boolean)
.- Parameters:
parseIncrementalUpdates
-
-
isParseIncrementalUpdates
public boolean isParseIncrementalUpdates() -
setMaxIncrementalUpdates
Set the maximum number of incremental updates to parse- Parameters:
maxIncrementalUpdates
-
-
getMaxIncrementalUpdates
public int getMaxIncrementalUpdates() -
setThrowOnEncryptedPayload
If the file is a 'Collection' and contains an embedded file with a defined 'AssociatedFile' value of 'EncryptedPayload', then throw anEncryptedDocumentException
.Microsoft IRM v2 wraps the encrypted document inside a container PDF. See TIKA-4082.
The goal of this is to make the user experience the same for traditionally encrypted files and PDFs that are containers for `EncryptedPayload`s.
The default value is
false
.- Parameters:
throwOnEncryptedPayload
-
-
isThrowOnEncryptedPayload
public boolean isThrowOnEncryptedPayload() -
initialize
This is a no-op. There is no need to initialize multiple fields. The regular field loading should happen without this.- Specified by:
initialize
in interfaceInitializable
- Parameters:
params
- params to use for initialization- Throws:
TikaConfigException
-
checkInitialization
- Specified by:
checkInitialization
in interfaceInitializable
- Parameters:
handler
- if there is a problem and no custom initializableProblemHandler has been configured via Initializable parameters, this is called to respond.- Throws:
TikaConfigException
-
setRenderer
- Specified by:
setRenderer
in interfaceRenderingParser
-
getRenderer
-
setImageGraphicsEngineFactory
@Field public void setImageGraphicsEngineFactory(ImageGraphicsEngineFactory imageGraphicsEngineFactory) -
getImageGraphicsEngineFactory
-
setImageStrategy
-
getImageStrategy
-