Class PdfCanvasProcessor


  • public class PdfCanvasProcessor
    extends java.lang.Object
    Processor for a PDF content stream.
    • 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
      • 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.
      • 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 - the IEventListener that 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 - the IEventListener that will receive rendering notifications
        additionalContentOperators - an optional map of custom IContentOperators 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 handler
        handler - 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 operator
        operator - 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 the Collection 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.
      • processContent

        public void processContent​(byte[] contentBytes,
                                   PdfResources resources)
        Processes PDF syntax. Note: If you re-use a given PdfCanvasProcessor, you must call reset()
        Parameters:
        contentBytes - the bytes of a content stream
        resources - 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 given PdfCanvasProcessor, you must call reset()
        Parameters:
        page - the page to process
      • getEventListener

        public IEventListener getEventListener()
        Accessor method for the IEventListener 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.
      • invokeOperator

        protected void invokeOperator​(PdfLiteral operator,
                                      java.util.List<PdfObject> operands)
        Invokes an operator.
        Parameters:
        operator - the PDF Syntax of the operator
        operands - a list with operands
      • getXObjectStream

        protected PdfStream getXObjectStream​(PdfName xobjectName)
      • populateXObjectDoHandlers

        protected void populateXObjectDoHandlers()
      • getFont

        protected PdfFont getFont​(PdfDictionary fontDict)
        Creates a PdfFont object by a font dictionary. The font may have been cached in case it is an indirect object.
        Parameters:
        fontDict - the font dictionary to 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 content
        dict - 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 data
        type - 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)