Class PdfCanvasProcessor
java.lang.Object
com.itextpdf.kernel.pdf.canvas.parser.PdfCanvasProcessor
Processor for a PDF content stream.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
A handler that implements operator (BDC).private static class
A handler that implements operator (BMC).private static class
A handler that implements operator (BT).private static class
A handler that implements operator (W, W*).private static class
A handler that implements operator (h).private static class
A handler that implements operator (v).private static class
A handler that implements operator (y).private static class
A handler that implements operator (c).private static class
A handler that implements operator (Do).private static class
A handler that implements operator (EI).private static class
A handler that implements operator (EMC).private static class
A handler that implements operator (ET).private static class
An XObject subtype handler for FORMprivate static class
A handler that implements operator (unregistered).private static class
An XObject subtype handler that does nothingprivate static class
An XObject subtype handler for IMAGEprivate static class
A handler that implements operator (l).private static class
A handler that implements operator (cm).private static class
A handler that implements operator (').private static class
A handler that implements operator (").private static class
A handler that implements operator (m).private static class
A handler that implements operator (S, s, f, F, f*, B, B*, b, b*).protected static class
A handler that implements operator (Q).private static class
A handler that implements operator (gs).private static class
A handler that implements operator (q).private static class
A handler that implements operator (re).private static class
A handler that implements operator (k).private static class
A handler that implements operator (K).private static class
A handler that implements operator (sc / scn).private static class
A handler that implements operator (CS).private static class
A handler that implements operator (cs).private static class
A handler that implements operator (SC / SCN).private static class
A handler that implements operator (g).private static class
A handler that implements operator (G).private static class
A handler that implements operator (J).private static class
A handler that implements operator (d).private static class
A handler that implements operator (j).private static class
A handler that implements operator (w).private static class
A handler that implements operator (M).private static class
A handler that implements operator (rg).private static class
A handler that implements operator (RG).private static class
A handler that implements operator (Tc).private static class
A handler that implements operator (Tf).private static class
A handler that implements operator (Tz).private static class
A handler that implements operator (TL).private static class
A handler that implements operator (Tr).private static class
A handler that implements operator (Ts).private static class
A handler that implements operator (Tw).private static class
A handler that implements operator (TJ).private static class
A handler that implements operator (Tj).private static class
A handler that implements operator (T*).private static class
A handler that implements operator (Td).private static class
A handler that implements operator (TD).private static class
A handler that implements operator (Tm). -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Map
<Integer, WeakReference<PdfFont>> The font cacheprotected int
Specifies the filling rule which should be applied while calculating new clipping path.protected Path
static final String
protected final IEventListener
Listener that will be notified of render eventsprivate final Stack
<ParserGraphicsState> Stack keeping track of the graphics state.protected boolean
Indicates whether the current clipping path should be modified by intersecting it with the current path.A stack containing marked content info.private MemoryLimitsAwareHandler
A memory limits handler.private Map
<String, IContentOperator> A map with all supported operators (PDF syntax).private long
Page size in bytes.private List
<PdfResources> Resources for the content stream.Cache supported events in case the user'sIEventListener.getSupportedEvents()
method is not very efficientprivate Matrix
private Matrix
private Map
<PdfName, IXObjectDoHandler> A map with all supported XObject handlers -
Constructor Summary
ConstructorsConstructorDescriptionPdfCanvasProcessor
(IEventListener eventListener) Creates a new PDF Content Stream Processor that will send its output to the designated render listener.PdfCanvasProcessor
(IEventListener eventListener, Map<String, IContentOperator> additionalContentOperators) Creates a new PDF Content Stream Processor that will send its output to the designated render listener. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
applyTextAdjust
(float tj) Adjusts the text matrix for the specified adjustment value (see TJ operator in the PDF spec for information)protected void
beginMarkedContent
(PdfName tag, PdfDictionary dict) Add to the marked content stackprivate void
Used to trigger beginTextBlock on the renderListenerprivate void
displayImage
(Stack<CanvasTag> canvasTagHierarchy, PdfStream imageStream, PdfName resourceName, boolean isInline) private void
displayPdfString
(PdfString string) Displays text.private void
displayXObject
(PdfName resourceName) Displays an XObject using the registered handler for this XObject's subtypeprotected void
Remove the latest marked content from the stack.private void
endText()
Used to trigger endTextBlock on the renderListenerprotected void
eventOccurred
(IEventData data, EventType type) This is a proxy to pass only those events to the event listener which are supported by it.private static Color
Gets a color based on a list of operands.private static Color
getColor
(PdfColorSpace pdfColorSpace, List<PdfObject> operands, PdfResources resources) Gets a color based on a list of operands and Color space.private static float[]
getColorants
(List<PdfObject> operands) Accessor method for theIEventListener
object maintained in this class.protected PdfFont
getFont
(PdfDictionary fontDict) Creates aPdfFont
object by a font dictionary.Gets the currentParserGraphicsState
Gets theCollection
containing all the registered operators strings.protected PdfResources
protected PdfStream
getXObjectStream
(PdfName xobjectName) private void
initClippingPath
(PdfPage page) protected void
invokeOperator
(PdfLiteral operator, List<PdfObject> operands) Invokes an operator.protected void
paintPath
(int operation, int rule) Displays the current path.protected void
Loads all the supported graphics and text state operators in a map.protected void
void
processContent
(byte[] contentBytes, PdfResources resources) Processes PDF syntax.void
processPageContent
(PdfPage page) Processes PDF syntax.registerContentOperator
(String operatorString, IContentOperator operator) Registers a content operator that will be called when the specified operator string is encountered during content processing.registerXObjectDoHandler
(PdfName xobjectSubType, IXObjectDoHandler handler) Registers a Do handler that will be called when Do for the provided XObject subtype is encountered during content processing.void
reset()
Resets the graphics state stack, matrices and resources.
-
Field Details
-
DEFAULT_OPERATOR
- See Also:
-
eventListener
Listener that will be notified of render events -
supportedEvents
Cache supported events in case the user'sIEventListener.getSupportedEvents()
method is not very efficient -
currentPath
-
isClip
protected boolean isClipIndicates whether the current clipping path should be modified by intersecting it with the current path. -
clippingRule
protected int clippingRuleSpecifies the filling rule which should be applied while calculating new clipping path. -
operators
A map with all supported operators (PDF syntax). -
resourcesStack
Resources for the content stream. Current resources are always at the top of the stack. Stack is needed in case if some "inner" content stream with it's own resources is encountered (like Form XObject). -
gsStack
Stack keeping track of the graphics state. -
textMatrix
-
textLineMatrix
-
xobjectDoHandlers
A map with all supported XObject handlers -
cachedFonts
The font cache -
markedContentStack
A stack containing marked content info. -
memoryLimitsHandler
A memory limits handler. -
pageSize
private long pageSizePage size in bytes.
-
-
Constructor Details
-
PdfCanvasProcessor
Creates a new PDF Content Stream Processor that will send its output to the designated render listener.- Parameters:
eventListener
- theIEventListener
that will receive rendering notifications
-
PdfCanvasProcessor
public PdfCanvasProcessor(IEventListener eventListener, Map<String, IContentOperator> additionalContentOperators) Creates a new PDF Content Stream Processor that will send its output to the designated render listener. Also allows registration of custom IContentOperators that can influence how (and whether or not) the PDF instructions will be parsed.- Parameters:
eventListener
- theIEventListener
that will receive rendering notificationsadditionalContentOperators
- an optional map of customIContentOperator
s for rendering instructions
-
-
Method Details
-
registerXObjectDoHandler
public IXObjectDoHandler registerXObjectDoHandler(PdfName xobjectSubType, IXObjectDoHandler handler) Registers a Do handler that will be called when Do for the provided XObject subtype is encountered during content processing.
If you register a handler, it is a very good idea to pass the call on to the existing registered handler (returned by this call), otherwise you may inadvertently change the internal behavior of the processor.- Parameters:
xobjectSubType
- the XObject subtype this handler will process, or PdfName.DEFAULT for a catch-all handlerhandler
- the handler that will receive notification when the Do operator for the specified subtype is encountered- Returns:
- the existing registered handler, if any
-
registerContentOperator
Registers a content operator that will be called when the specified operator string is encountered during content processing.
If you register an operator, it is a very good idea to pass the call on to the existing registered operator (returned by this call), otherwise you may inadvertently change the internal behavior of the processor.- Parameters:
operatorString
- the operator id, or DEFAULT_OPERATOR for a catch-all operatoroperator
- the operator that will receive notification when the operator is encountered- Returns:
- the existing registered operator, if any
-
getRegisteredOperatorStrings
Gets theCollection
containing all the registered operators strings.- Returns:
Collection
containing all the registered operators strings.
-
reset
public void reset()Resets the graphics state stack, matrices and resources. -
getGraphicsState
Gets the currentParserGraphicsState
- Returns:
- the current
ParserGraphicsState
-
processContent
Processes PDF syntax. Note: If you re-use a givenPdfCanvasProcessor
, you must callreset()
- Parameters:
contentBytes
- the bytes of a content streamresources
- the resources of the content stream. Must not be null.
-
processPageContent
Processes PDF syntax. Note: If you re-use a givenPdfCanvasProcessor
, you must callreset()
- Parameters:
page
- the page to process
-
getEventListener
Accessor method for theIEventListener
object maintained in this class. Necessary for implementing custom ContentOperator implementations.- Returns:
- the renderListener
-
populateOperators
protected void populateOperators()Loads all the supported graphics and text state operators in a map. -
paintPath
protected void paintPath(int operation, int rule) Displays the current path.- Parameters:
operation
- One of the possible combinations ofPathRenderInfo.STROKE
andPathRenderInfo.FILL
values orPathRenderInfo.NO_OP
rule
- EitherPdfCanvasConstants.FillingRule.NONZERO_WINDING
orPdfCanvasConstants.FillingRule.EVEN_ODD
In case it isn't applicable pass anybyte
value.
-
invokeOperator
Invokes an operator.- Parameters:
operator
- the PDF Syntax of the operatoroperands
- a list with operands
-
getXObjectStream
-
getResources
-
populateXObjectDoHandlers
protected void populateXObjectDoHandlers() -
getFont
Creates aPdfFont
object by a font dictionary. The font may have been cached in case it is an indirect object.- Parameters:
fontDict
- thefont dictionary
to create the font from- Returns:
- the created font
-
beginMarkedContent
Add to the marked content stack- Parameters:
tag
- the tag of the marked contentdict
- the PdfDictionary associated with the marked content
-
endMarkedContent
protected void endMarkedContent()Remove the latest marked content from the stack. Keeps track of the BMC, BDC and EMC operators. -
beginText
private void beginText()Used to trigger beginTextBlock on the renderListener -
endText
private void endText()Used to trigger endTextBlock on the renderListener -
eventOccurred
This is a proxy to pass only those events to the event listener which are supported by it.- Parameters:
data
- event datatype
- event type
-
displayPdfString
Displays text.- Parameters:
string
- the text to display
-
displayXObject
Displays an XObject using the registered handler for this XObject's subtype- Parameters:
resourceName
- the name of the XObject to retrieve from the resource dictionary
-
displayImage
-
applyTextAdjust
private void applyTextAdjust(float tj) Adjusts the text matrix for the specified adjustment value (see TJ operator in the PDF spec for information)- Parameters:
tj
- the text adjustment
-
initClippingPath
-
getColor
private static Color getColor(PdfColorSpace pdfColorSpace, List<PdfObject> operands, PdfResources resources) Gets a color based on a list of operands and Color space. -
getColor
Gets a color based on a list of operands. -
getColorants
-