Class PdfCanvasProcessor
- java.lang.Object
-
- com.itextpdf.kernel.pdf.canvas.parser.PdfCanvasProcessor
-
public class PdfCanvasProcessor extends java.lang.ObjectProcessor for a PDF content stream.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classPdfCanvasProcessor.BeginMarkedContentDictionaryOperatorA handler that implements operator (BDC).private static classPdfCanvasProcessor.BeginMarkedContentOperatorA handler that implements operator (BMC).private static classPdfCanvasProcessor.BeginTextOperatorA handler that implements operator (BT).private static classPdfCanvasProcessor.ClipPathOperatorA handler that implements operator (W, W*).private static classPdfCanvasProcessor.CloseSubpathOperatorA handler that implements operator (h).private static classPdfCanvasProcessor.CurveFirstPointDuplicatedOperatorA handler that implements operator (v).private static classPdfCanvasProcessor.CurveFourhPointDuplicatedOperatorA handler that implements operator (y).private static classPdfCanvasProcessor.CurveOperatorA handler that implements operator (c).private static classPdfCanvasProcessor.DoOperatorA handler that implements operator (Do).private static classPdfCanvasProcessor.EndImageOperatorA handler that implements operator (EI).private static classPdfCanvasProcessor.EndMarkedContentOperatorA handler that implements operator (EMC).private static classPdfCanvasProcessor.EndTextOperatorA handler that implements operator (ET).private static classPdfCanvasProcessor.FormXObjectDoHandlerAn XObject subtype handler for FORMprivate static classPdfCanvasProcessor.IgnoreOperatorA handler that implements operator (unregistered).private static classPdfCanvasProcessor.IgnoreXObjectDoHandlerAn XObject subtype handler that does nothingprivate static classPdfCanvasProcessor.ImageXObjectDoHandlerAn XObject subtype handler for IMAGEprivate static classPdfCanvasProcessor.LineToOperatorA handler that implements operator (l).private static classPdfCanvasProcessor.ModifyCurrentTransformationMatrixOperatorA handler that implements operator (cm).private static classPdfCanvasProcessor.MoveNextLineAndShowTextOperatorA handler that implements operator (').private static classPdfCanvasProcessor.MoveNextLineAndShowTextWithSpacingOperatorA handler that implements operator (").private static classPdfCanvasProcessor.MoveToOperatorA handler that implements operator (m).private static classPdfCanvasProcessor.PaintPathOperatorA handler that implements operator (S, s, f, F, f*, B, B*, b, b*).protected static classPdfCanvasProcessor.PopGraphicsStateOperatorA handler that implements operator (Q).private static classPdfCanvasProcessor.ProcessGraphicsStateResourceOperatorA handler that implements operator (gs).private static classPdfCanvasProcessor.PushGraphicsStateOperatorA handler that implements operator (q).private static classPdfCanvasProcessor.RectangleOperatorA handler that implements operator (re).private static classPdfCanvasProcessor.SetCMYKFillOperatorA handler that implements operator (k).private static classPdfCanvasProcessor.SetCMYKStrokeOperatorA handler that implements operator (K).private static classPdfCanvasProcessor.SetColorFillOperatorA handler that implements operator (sc / scn).private static classPdfCanvasProcessor.SetColorSpaceFillOperatorA handler that implements operator (CS).private static classPdfCanvasProcessor.SetColorSpaceStrokeOperatorA handler that implements operator (cs).private static classPdfCanvasProcessor.SetColorStrokeOperatorA handler that implements operator (SC / SCN).private static classPdfCanvasProcessor.SetGrayFillOperatorA handler that implements operator (g).private static classPdfCanvasProcessor.SetGrayStrokeOperatorA handler that implements operator (G).private static classPdfCanvasProcessor.SetLineCapOperatorA handler that implements operator (J).private static classPdfCanvasProcessor.SetLineDashPatternOperatorA handler that implements operator (d).private static classPdfCanvasProcessor.SetLineJoinOperatorA handler that implements operator (j).private static classPdfCanvasProcessor.SetLineWidthOperatorA handler that implements operator (w).private static classPdfCanvasProcessor.SetMiterLimitOperatorA handler that implements operator (M).private static classPdfCanvasProcessor.SetRGBFillOperatorA handler that implements operator (rg).private static classPdfCanvasProcessor.SetRGBStrokeOperatorA handler that implements operator (RG).private static classPdfCanvasProcessor.SetTextCharacterSpacingOperatorA handler that implements operator (Tc).private static classPdfCanvasProcessor.SetTextFontOperatorA handler that implements operator (Tf).private static classPdfCanvasProcessor.SetTextHorizontalScalingOperatorA handler that implements operator (Tz).private static classPdfCanvasProcessor.SetTextLeadingOperatorA handler that implements operator (TL).private static classPdfCanvasProcessor.SetTextRenderModeOperatorA handler that implements operator (Tr).private static classPdfCanvasProcessor.SetTextRiseOperatorA handler that implements operator (Ts).private static classPdfCanvasProcessor.SetTextWordSpacingOperatorA handler that implements operator (Tw).private static classPdfCanvasProcessor.ShowTextArrayOperatorA handler that implements operator (TJ).private static classPdfCanvasProcessor.ShowTextOperatorA handler that implements operator (Tj).private static classPdfCanvasProcessor.TextMoveNextLineOperatorA handler that implements operator (T*).private static classPdfCanvasProcessor.TextMoveStartNextLineOperatorA handler that implements operator (Td).private static classPdfCanvasProcessor.TextMoveStartNextLineWithLeadingOperatorA handler that implements operator (TD).private static classPdfCanvasProcessor.TextSetTextMatrixOperatorA handler that implements operator (Tm).
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<java.lang.Integer,java.lang.ref.WeakReference<PdfFont>>cachedFontsThe font cacheprotected intclippingRuleSpecifies the filling rule which should be applied while calculating new clipping path.protected PathcurrentPathstatic java.lang.StringDEFAULT_OPERATORprotected IEventListenereventListenerListener that will be notified of render eventsprivate java.util.Stack<ParserGraphicsState>gsStackStack keeping track of the graphics state.protected booleanisClipIndicates whether the current clipping path should be modified by intersecting it with the current path.private java.util.Stack<CanvasTag>markedContentStackA stack containing marked content info.private MemoryLimitsAwareHandlermemoryLimitsHandlerA memory limits handler.private java.util.Map<java.lang.String,IContentOperator>operatorsA map with all supported operators (PDF syntax).private longpageSizePage size in bytes.private java.util.List<PdfResources>resourcesStackResources for the content stream.protected java.util.Set<EventType>supportedEventsCache supported events in case the user'sIEventListener.getSupportedEvents()method is not very efficientprivate MatrixtextLineMatrixprivate MatrixtextMatrixprivate java.util.Map<PdfName,IXObjectDoHandler>xobjectDoHandlersA map with all supported XObject handlers
-
Constructor Summary
Constructors Constructor Description PdfCanvasProcessor(IEventListener eventListener)Creates a new PDF Content Stream Processor that will send its output to the designated render listener.PdfCanvasProcessor(IEventListener eventListener, java.util.Map<java.lang.String,IContentOperator> additionalContentOperators)Creates a new PDF Content Stream Processor that will send its output to the designated render listener.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidapplyTextAdjust(float tj)Adjusts the text matrix for the specified adjustment value (see TJ operator in the PDF spec for information)protected voidbeginMarkedContent(PdfName tag, PdfDictionary dict)Add to the marked content stackprivate voidbeginText()Used to trigger beginTextBlock on the renderListenerprivate voiddisplayImage(java.util.Stack<CanvasTag> canvasTagHierarchy, PdfStream imageStream, PdfName resourceName, boolean isInline)private voiddisplayPdfString(PdfString string)Displays text.private voiddisplayXObject(PdfName resourceName)Displays an XObject using the registered handler for this XObject's subtypeprotected voidendMarkedContent()Remove the latest marked content from the stack.private voidendText()Used to trigger endTextBlock on the renderListenerprotected voideventOccurred(IEventData data, EventType type)This is a proxy to pass only those events to the event listener which are supported by it.private static ColorgetColor(int nOperands, java.util.List<PdfObject> operands)Gets a color based on a list of operands.private static ColorgetColor(PdfColorSpace pdfColorSpace, java.util.List<PdfObject> operands, PdfResources resources)Gets a color based on a list of operands and Color space.private static float[]getColorants(java.util.List<PdfObject> operands)IEventListenergetEventListener()Accessor method for theIEventListenerobject maintained in this class.protected PdfFontgetFont(PdfDictionary fontDict)Creates aPdfFontobject by a font dictionary.ParserGraphicsStategetGraphicsState()Gets the currentParserGraphicsStatejava.util.Collection<java.lang.String>getRegisteredOperatorStrings()Gets theCollectioncontaining all the registered operators strings.protected PdfResourcesgetResources()protected PdfStreamgetXObjectStream(PdfName xobjectName)private voidinitClippingPath(PdfPage page)protected voidinvokeOperator(PdfLiteral operator, java.util.List<PdfObject> operands)Invokes an operator.protected voidpaintPath(int operation, int rule)Displays the current path.protected voidpopulateOperators()Loads all the supported graphics and text state operators in a map.protected voidpopulateXObjectDoHandlers()voidprocessContent(byte[] contentBytes, PdfResources resources)Processes PDF syntax.voidprocessPageContent(PdfPage page)Processes PDF syntax.IContentOperatorregisterContentOperator(java.lang.String operatorString, IContentOperator operator)Registers a content operator that will be called when the specified operator string is encountered during content processing.IXObjectDoHandlerregisterXObjectDoHandler(PdfName xobjectSubType, IXObjectDoHandler handler)Registers a Do handler that will be called when Do for the provided XObject subtype is encountered during content processing.voidreset()Resets the graphics state stack, matrices and resources.
-
-
-
Field Detail
-
DEFAULT_OPERATOR
public static final java.lang.String DEFAULT_OPERATOR
- See Also:
- Constant Field Values
-
eventListener
protected final IEventListener eventListener
Listener that will be notified of render events
-
supportedEvents
protected final java.util.Set<EventType> supportedEvents
Cache supported events in case the user'sIEventListener.getSupportedEvents()method is not very efficient
-
currentPath
protected Path currentPath
-
isClip
protected boolean isClip
Indicates whether the current clipping path should be modified by intersecting it with the current path.
-
clippingRule
protected int clippingRule
Specifies the filling rule which should be applied while calculating new clipping path.
-
operators
private java.util.Map<java.lang.String,IContentOperator> operators
A map with all supported operators (PDF syntax).
-
resourcesStack
private java.util.List<PdfResources> 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
private final java.util.Stack<ParserGraphicsState> gsStack
Stack keeping track of the graphics state.
-
textMatrix
private Matrix textMatrix
-
textLineMatrix
private Matrix textLineMatrix
-
xobjectDoHandlers
private java.util.Map<PdfName,IXObjectDoHandler> xobjectDoHandlers
A map with all supported XObject handlers
-
cachedFonts
private java.util.Map<java.lang.Integer,java.lang.ref.WeakReference<PdfFont>> cachedFonts
The font cache
-
markedContentStack
private java.util.Stack<CanvasTag> markedContentStack
A stack containing marked content info.
-
memoryLimitsHandler
private MemoryLimitsAwareHandler memoryLimitsHandler
A memory limits handler.
-
pageSize
private long pageSize
Page size in bytes.
-
-
Constructor Detail
-
PdfCanvasProcessor
public PdfCanvasProcessor(IEventListener eventListener)
Creates a new PDF Content Stream Processor that will send its output to the designated render listener.- Parameters:
eventListener- theIEventListenerthat will receive rendering notifications
-
PdfCanvasProcessor
public PdfCanvasProcessor(IEventListener eventListener, java.util.Map<java.lang.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- theIEventListenerthat will receive rendering notificationsadditionalContentOperators- an optional map of customIContentOperators for rendering instructions
-
-
Method Detail
-
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
public IContentOperator registerContentOperator(java.lang.String operatorString, IContentOperator operator)
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
public java.util.Collection<java.lang.String> getRegisteredOperatorStrings()
Gets theCollectioncontaining all the registered operators strings.- Returns:
Collectioncontaining all the registered operators strings.
-
reset
public void reset()
Resets the graphics state stack, matrices and resources.
-
getGraphicsState
public ParserGraphicsState getGraphicsState()
Gets the currentParserGraphicsState- Returns:
- the current
ParserGraphicsState
-
processContent
public void processContent(byte[] contentBytes, PdfResources resources)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
public void processPageContent(PdfPage page)
Processes PDF syntax. Note: If you re-use a givenPdfCanvasProcessor, you must callreset()- Parameters:
page- the page to process
-
getEventListener
public IEventListener getEventListener()
Accessor method for theIEventListenerobject 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.STROKEandPathRenderInfo.FILLvalues orPathRenderInfo.NO_OPrule- EitherPdfCanvasConstants.FillingRule.NONZERO_WINDINGorPdfCanvasConstants.FillingRule.EVEN_ODDIn case it isn't applicable pass anybytevalue.
-
invokeOperator
protected void invokeOperator(PdfLiteral operator, java.util.List<PdfObject> operands)
Invokes an operator.- Parameters:
operator- the PDF Syntax of the operatoroperands- a list with operands
-
getResources
protected PdfResources getResources()
-
populateXObjectDoHandlers
protected void populateXObjectDoHandlers()
-
getFont
protected PdfFont getFont(PdfDictionary fontDict)
Creates aPdfFontobject by a font dictionary. The font may have been cached in case it is an indirect object.- Parameters:
fontDict- thefont dictionaryto create the font from- Returns:
- the created font
-
beginMarkedContent
protected void beginMarkedContent(PdfName tag, PdfDictionary dict)
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
protected void eventOccurred(IEventData data, EventType type)
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
private void displayPdfString(PdfString string)
Displays text.- Parameters:
string- the text to display
-
displayXObject
private void displayXObject(PdfName resourceName)
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
private void displayImage(java.util.Stack<CanvasTag> canvasTagHierarchy, PdfStream imageStream, PdfName resourceName, boolean isInline)
-
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
private void initClippingPath(PdfPage page)
-
getColor
private static Color getColor(PdfColorSpace pdfColorSpace, java.util.List<PdfObject> operands, PdfResources resources)
Gets a color based on a list of operands and Color space.
-
getColor
private static Color getColor(int nOperands, java.util.List<PdfObject> operands)
Gets a color based on a list of operands.
-
getColorants
private static float[] getColorants(java.util.List<PdfObject> operands)
-
-