Package com.itextpdf.text.pdf.parser
Class PdfContentStreamProcessor
- java.lang.Object
-
- com.itextpdf.text.pdf.parser.PdfContentStreamProcessor
-
public class PdfContentStreamProcessor extends java.lang.Object
Processor for a PDF content Stream.- Since:
- 2.1.4
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
PdfContentStreamProcessor.BeginMarkedContent
A content operator implementation (BMC).private static class
PdfContentStreamProcessor.BeginMarkedContentDictionary
A content operator implementation (BDC).private static class
PdfContentStreamProcessor.BeginText
A content operator implementation (BT).private static class
PdfContentStreamProcessor.ClipPath
A content operator implementation (W, W*)private static class
PdfContentStreamProcessor.CloseSubpath
A content operator implementation (h).private static class
PdfContentStreamProcessor.Curve
A content operator implementation (c).private static class
PdfContentStreamProcessor.CurveFirstPointDuplicated
A content operator implementation (v).private static class
PdfContentStreamProcessor.CurveFourhPointDuplicated
A content operator implementation (y).private static class
PdfContentStreamProcessor.Do
A content operator implementation (Do).private static class
PdfContentStreamProcessor.EndMarkedContent
A content operator implementation (EMC).private static class
PdfContentStreamProcessor.EndPath
A content operator implementation (n).private static class
PdfContentStreamProcessor.EndText
A content operator implementation (ET).private static class
PdfContentStreamProcessor.FormXObjectDoHandler
An XObject subtype handler for FORMprivate static class
PdfContentStreamProcessor.IgnoreOperatorContentOperator
A content operator implementation (unregistered).private static class
PdfContentStreamProcessor.IgnoreXObjectDoHandler
An XObject subtype handler that does nothingprivate static class
PdfContentStreamProcessor.ImageXObjectDoHandler
An XObject subtype handler for IMAGEprivate static class
PdfContentStreamProcessor.LineTo
A content operator implementation (l).private static class
PdfContentStreamProcessor.ModifyCurrentTransformationMatrix
A content operator implementation (cm).private static class
PdfContentStreamProcessor.MoveNextLineAndShowText
A content operator implementation (').private static class
PdfContentStreamProcessor.MoveNextLineAndShowTextWithSpacing
A content operator implementation (").private static class
PdfContentStreamProcessor.MoveTo
A content operator implementation (m).private static class
PdfContentStreamProcessor.PaintPath
A content operator implementation (S, s, f, F, f*, B, B*, b, b*).private static class
PdfContentStreamProcessor.PopGraphicsState
A content operator implementation (Q).private static class
PdfContentStreamProcessor.ProcessGraphicsStateResource
A content operator implementation (gs).private static class
PdfContentStreamProcessor.PushGraphicsState
A content operator implementation (q).private static class
PdfContentStreamProcessor.Rectangle
A content operator implementation (re).private static class
PdfContentStreamProcessor.ResourceDictionary
A resource dictionary that allows stack-like behavior to support resource dictionary inheritanceprivate static class
PdfContentStreamProcessor.SetCMYKFill
A content operator implementation (rg).private static class
PdfContentStreamProcessor.SetCMYKStroke
A content operator implementation (RG).private static class
PdfContentStreamProcessor.SetColorFill
A content operator implementation (sc / scn).private static class
PdfContentStreamProcessor.SetColorSpaceFill
A content operator implementation (cs).private static class
PdfContentStreamProcessor.SetColorSpaceStroke
A content operator implementation (CS).private static class
PdfContentStreamProcessor.SetColorStroke
A content operator implementation (SC / SCN).private static class
PdfContentStreamProcessor.SetGrayFill
A content operator implementation (g).private static class
PdfContentStreamProcessor.SetGrayStroke
A content operator implementation (G).private static class
PdfContentStreamProcessor.SetLineCap
A content operator implementation (J).private static class
PdfContentStreamProcessor.SetLineDashPattern
A content operator implementation (d).private static class
PdfContentStreamProcessor.SetLineJoin
A content operator implementation (j).private static class
PdfContentStreamProcessor.SetLineWidth
A content operator implementation (w).private static class
PdfContentStreamProcessor.SetMiterLimit
A content operator implementation (M).private static class
PdfContentStreamProcessor.SetRGBFill
A content operator implementation (rg).private static class
PdfContentStreamProcessor.SetRGBStroke
A content operator implementation (RG).private static class
PdfContentStreamProcessor.SetTextCharacterSpacing
A content operator implementation (Tc).private static class
PdfContentStreamProcessor.SetTextFont
A content operator implementation (Tf).private static class
PdfContentStreamProcessor.SetTextHorizontalScaling
A content operator implementation (Tz).private static class
PdfContentStreamProcessor.SetTextLeading
A content operator implementation (TL).private static class
PdfContentStreamProcessor.SetTextRenderMode
A content operator implementation (Tr).private static class
PdfContentStreamProcessor.SetTextRise
A content operator implementation (Ts).private static class
PdfContentStreamProcessor.SetTextWordSpacing
A content operator implementation (Tw).private static class
PdfContentStreamProcessor.ShowText
A content operator implementation (Tj).private static class
PdfContentStreamProcessor.ShowTextArray
A content operator implementation (TJ).private static class
PdfContentStreamProcessor.TextMoveNextLine
A content operator implementation (T*).private static class
PdfContentStreamProcessor.TextMoveStartNextLine
A content operator implementation (Td).private static class
PdfContentStreamProcessor.TextMoveStartNextLineWithLeading
A content operator implementation (TD).private static class
PdfContentStreamProcessor.TextSetTextMatrix
A content operator implementation (Tm).
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<java.lang.Integer,java.lang.ref.WeakReference<CMapAwareDocumentFont>>
cachedFonts
static java.lang.String
DEFAULTOPERATOR
Default operatorprivate java.util.Stack<GraphicsState>
gsStack
Stack keeping track of the graphics state.private java.util.Stack<MarkedContentInfo>
markedContentStack
A stack containing marked content info.private java.util.Map<java.lang.String,ContentOperator>
operators
A map with all supported operators (PDF syntax).private RenderListener
renderListener
Listener that will be notified of render eventsprivate PdfContentStreamProcessor.ResourceDictionary
resources
Resources for the content stream.private Matrix
textLineMatrix
Text line matrix.private Matrix
textMatrix
Text matrix.private java.util.Map<PdfName,XObjectDoHandler>
xobjectDoHandlers
A map with all supported XObject handlers
-
Constructor Summary
Constructors Constructor Description PdfContentStreamProcessor(RenderListener renderListener)
Creates a new PDF Content Stream Processor that will send it's output to the designated render listener.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
applyTextAdjust(float tj)
Adjusts the text matrix for the specified adjustment value (see TJ operator in the PDF spec for information)private void
beginMarkedContent(PdfName tag, PdfDictionary dict)
Add to the marked content stackprivate void
beginText()
Used to trigger beginTextBlock on the renderListenerprivate void
clipPath(int rule)
private void
displayPdfString(PdfString string)
Displays text.private void
displayXObject(PdfName xobjectName)
Displays an XObject using the registered handler for this XObject's subtypeprivate void
endMarkedContent()
Remove the latest marked content from the stack.private void
endText()
Used to trigger endTextBlock on the renderListenerprivate static BaseColor
getColor(int nOperands, java.util.List<PdfObject> operands)
Gets a color based on a list of operands.private static BaseColor
getColor(PdfName colorSpace, java.util.List<PdfObject> operands)
Gets a color based on a list of operands.private CMapAwareDocumentFont
getFont(PdfDictionary fontResource)
private CMapAwareDocumentFont
getFont(PRIndirectReference ind)
Gets the font pointed to by the indirect reference.java.util.Collection<java.lang.String>
getRegisteredOperatorStrings()
RenderListener
getRenderListener()
Accessor method for the RenderListener object maintained in this class.GraphicsState
gs()
Returns the current graphics state.protected void
handleInlineImage(InlineImageInfo info, PdfDictionary colorSpaceDic)
Callback when an inline image is found.private void
invokeOperator(PdfLiteral operator, java.util.ArrayList<PdfObject> operands)
Invokes an operator.private void
modifyPath(int operation, java.util.List<java.lang.Float> segmentData)
Modifies the current path.private void
paintPath(int operation, int rule, boolean close)
Displays the current path.private void
populateOperators()
Loads all the supported graphics and text state operators in a map.private void
populateXObjectDoHandlers()
void
processContent(byte[] contentBytes, PdfDictionary resources)
Processes PDF syntax.ContentOperator
registerContentOperator(java.lang.String operatorString, ContentOperator operator)
Registers a content operator that will be called when the specified operator string is encountered during content processing.XObjectDoHandler
registerXObjectDoHandler(PdfName xobjectSubType, XObjectDoHandler 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 Detail
-
DEFAULTOPERATOR
public static final java.lang.String DEFAULTOPERATOR
Default operator- Since:
- 5.0.1
- See Also:
- Constant Field Values
-
operators
private final java.util.Map<java.lang.String,ContentOperator> operators
A map with all supported operators (PDF syntax).
-
resources
private PdfContentStreamProcessor.ResourceDictionary resources
Resources for the content stream.
-
gsStack
private final java.util.Stack<GraphicsState> gsStack
Stack keeping track of the graphics state.
-
textMatrix
private Matrix textMatrix
Text matrix.
-
textLineMatrix
private Matrix textLineMatrix
Text line matrix.
-
renderListener
private final RenderListener renderListener
Listener that will be notified of render events
-
xobjectDoHandlers
private final java.util.Map<PdfName,XObjectDoHandler> xobjectDoHandlers
A map with all supported XObject handlers
-
cachedFonts
private final java.util.Map<java.lang.Integer,java.lang.ref.WeakReference<CMapAwareDocumentFont>> cachedFonts
-
markedContentStack
private final java.util.Stack<MarkedContentInfo> markedContentStack
A stack containing marked content info.- Since:
- 5.0.2
-
-
Constructor Detail
-
PdfContentStreamProcessor
public PdfContentStreamProcessor(RenderListener renderListener)
Creates a new PDF Content Stream Processor that will send it's output to the designated render listener.- Parameters:
renderListener
- theRenderListener
that will receive rendering notifications
-
-
Method Detail
-
populateXObjectDoHandlers
private void populateXObjectDoHandlers()
-
registerXObjectDoHandler
public XObjectDoHandler registerXObjectDoHandler(PdfName xobjectSubType, XObjectDoHandler 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
- Since:
- 5.0.1
-
getFont
private CMapAwareDocumentFont getFont(PRIndirectReference ind)
Gets the font pointed to by the indirect reference. The font may have been cached.- Parameters:
ind
- the indirect reference ponting to the font- Returns:
- the font
- Since:
- 5.0.6
-
getFont
private CMapAwareDocumentFont getFont(PdfDictionary fontResource)
-
populateOperators
private void populateOperators()
Loads all the supported graphics and text state operators in a map.
-
registerContentOperator
public ContentOperator registerContentOperator(java.lang.String operatorString, ContentOperator 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 DEFAULTOPERATOR for a catch-all operatoroperator
- the operator that will receive notification when the operator is encountered- Returns:
- the existing registered operator, if any
- Since:
- 2.1.7
-
getRegisteredOperatorStrings
public java.util.Collection<java.lang.String> getRegisteredOperatorStrings()
- Returns:
Collection
containing all the registered operators strings- Since:
- 5.5.6
-
reset
public void reset()
Resets the graphics state stack, matrices and resources.
-
gs
public GraphicsState gs()
Returns the current graphics state.- Returns:
- the graphics state
-
invokeOperator
private void invokeOperator(PdfLiteral operator, java.util.ArrayList<PdfObject> operands) throws java.lang.Exception
Invokes an operator.- Parameters:
operator
- the PDF Syntax of the operatoroperands
- a list with operands- Throws:
java.lang.Exception
-
beginMarkedContent
private 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- Since:
- 5.0.2
-
endMarkedContent
private void endMarkedContent()
Remove the latest marked content from the stack. Keeps track of the BMC, BDC and EMC operators.- Since:
- 5.0.2
-
beginText
private void beginText()
Used to trigger beginTextBlock on the renderListener
-
endText
private void endText()
Used to trigger endTextBlock on the renderListener
-
displayPdfString
private void displayPdfString(PdfString string)
Displays text.- Parameters:
string
- the text to display
-
displayXObject
private void displayXObject(PdfName xobjectName) throws java.io.IOException
Displays an XObject using the registered handler for this XObject's subtype- Parameters:
xobjectName
- the name of the XObject to retrieve from the resource dictionary- Throws:
java.io.IOException
-
paintPath
private void paintPath(int operation, int rule, boolean close)
Displays the current path.- Parameters:
operation
- One of the possible combinations ofPathPaintingRenderInfo.STROKE
andPathPaintingRenderInfo.FILL
values orPathPaintingRenderInfo.NO_OP
rule
- EitherPathPaintingRenderInfo.NONZERO_WINDING_RULE
orPathPaintingRenderInfo.EVEN_ODD_RULE
In case it isn't applicable pass anybyte
value.close
- Indicates whether the path should be closed or not.- Since:
- 5.5.6
-
modifyPath
private void modifyPath(int operation, java.util.List<java.lang.Float> segmentData)
Modifies the current path.- Parameters:
operation
- Indicates which path-construction operation should be performed.segmentData
- Contains x, y components of points of a new segment being added to the current path. E.g. x1 y1 x2 y2 x3 y3 etc. It's ignored for "close subpath" operarion (h).
-
clipPath
private void clipPath(int rule)
-
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
-
processContent
public void processContent(byte[] contentBytes, PdfDictionary resources)
Processes PDF syntax. Note: If you re-use a givenPdfContentStreamProcessor
, you must callreset()
- Parameters:
contentBytes
- the bytes of a content streamresources
- the resources that come with the content stream
-
handleInlineImage
protected void handleInlineImage(InlineImageInfo info, PdfDictionary colorSpaceDic)
Callback when an inline image is found. This requires special handling because inline images don't follow the standard operator syntax- Parameters:
info
- the inline imagecolorSpaceDic
- the color space for the inline immage
-
getRenderListener
public RenderListener getRenderListener()
Accessor method for the RenderListener object maintained in this class. Necessary for implementing custom ContentOperator implementations.- Returns:
- the renderListener
-
getColor
private static BaseColor getColor(PdfName colorSpace, java.util.List<PdfObject> operands)
Gets a color based on a list of operands.
-
-