Class AbstractTagProcessor

java.lang.Object
com.itextpdf.tool.xml.html.AbstractTagProcessor
All Implemented Interfaces:
CssAppliersAware, TagProcessor
Direct Known Subclasses:
Anchor, Body, Break, Div, Header, HorizontalRule, Image, Link, Meta, NonSanitizedTag, OrderedUnorderedList, OrderedUnorderedListItem, ParaGraph, Span, Style, Table, TableData, TableRow, Title, XML

public abstract class AbstractTagProcessor extends Object implements TagProcessor, CssAppliersAware
Abstract TagProcessor that allows setting the configuration object to a protected member variable.
Implements TagProcessor.startElement(WorkerContext, Tag) and TagProcessor.endElement(WorkerContext, Tag, List) to calculate font sizes and add new pages if needed.
Extend from this class instead of implementing TagProcessor to benefit from auto fontsize metric conversion to pt and page-break-before/after insertion. Override start(WorkerContext, Tag) and end(WorkerContext, Tag, List) in your extension.
  • Field Details

    • fontsizeTrans

      private final FontSizeTranslator fontsizeTrans
    • cssAppliers

      private CssAppliers cssAppliers
    • ignoreDirAttribute

      private Set<String> ignoreDirAttribute
      For some tags, if they have their own not inherited DIR attribute, this attribute will definitely not be applied for itext layout. For the most common such tags we use this set to ignore DIR attribute, in order to avoid unnecessary adjustments in XmlWorker. However if parent of these tags have DIR attribute, it may be applied to these tags.
    • tree

      private List<Tag> tree
  • Constructor Details

    • AbstractTagProcessor

      public AbstractTagProcessor()
  • Method Details

    • getCSSResolver

      public CSSResolver getCSSResolver(WorkerContext context) throws NoCustomContextException
      Utility method that fetches the CSSResolver from the if any and if it uses the default key.
      Parameters:
      context - the WorkerContext
      Returns:
      CSSResolver
      Throws:
      NoCustomContextException - if the context of the CssResolverPipeline could not be found.
    • getHtmlPipelineContext

      public HtmlPipelineContext getHtmlPipelineContext(WorkerContext context) throws NoCustomContextException
      Utility method that fetches the HtmlPipelineContext used if any and if it uses the default key.
      Parameters:
      context - the WorkerContext
      Returns:
      a HtmlPipelineContext
      Throws:
      NoCustomContextException - if the context of the HtmlPipelineContext could not be found.
    • startElement

      public final List<Element> startElement(WorkerContext ctx, Tag tag)
      Calculates any found font size to pt values and set it in the CSS before calling start(WorkerContext, Tag).
      Checks for CSS.Property.PAGE_BREAK_BEFORE, if the value is always a Chunk.NEXTPAGE added before the implementors start(WorkerContext, Tag) method.
      Specified by:
      startElement in interface TagProcessor
      Parameters:
      ctx - the WorkerContext
      tag - the tag encountered
      Returns:
      Element an Element to add to the current content;
    • start

      public List<Element> start(WorkerContext ctx, Tag tag)
      Classes extending AbstractTagProcessor should override this method for actions that should be done in TagProcessor.startElement(WorkerContext, Tag). The startElement(WorkerContext, Tag) calls this method after or before doing certain stuff, (see it's description).
      Parameters:
      ctx - the WorkerContext
      tag - the tag
      Returns:
      an element to be added to current content, may be null
    • content

      public List<Element> content(WorkerContext ctx, Tag tag, String content)
      Description copied from interface: TagProcessor
      This method is called if there is text content encountered between the opening and closing tags this TagProcessor is mapped to.
      Specified by:
      content in interface TagProcessor
      Parameters:
      ctx - the WorkerContext
      tag - the tag encountered
      content - the text content between the tags this TagProcessor is mapped to.
      Returns:
      the element to add to the currentContent list
    • getParentDirection

      private String getParentDirection()
    • getRunDirection

      protected int getRunDirection(Tag tag)
    • textContent

      protected List<Element> textContent(WorkerContext ctx, Tag tag, String content)
    • endElement

      public final List<Element> endElement(WorkerContext ctx, Tag tag, List<Element> currentContent)
      Checks for CSS.Property.PAGE_BREAK_AFTER, if the value is always a Chunk.NEXTPAGE is added to the currentContentList after calling end(WorkerContext, Tag, List).
      Specified by:
      endElement in interface TagProcessor
      Parameters:
      ctx - the WorkerContext
      tag - the tag encountered
      currentContent - a list of content possibly created by TagProcessing of inner tags, and by startElement and content methods of this TagProcessor .
      Returns:
      the resulting element to add to the document or a content stack.
    • end

      public List<Element> end(WorkerContext ctx, Tag tag, List<Element> currentContent)
      Classes extending AbstractTagProcessor should override this method for actions that should be done in TagProcessor.endElement(WorkerContext, Tag, List). The endElement(WorkerContext, Tag, List) calls this method after or before doing certain stuff, (see it's description).
      Parameters:
      ctx - the WorkerContext
      tag - the tag
      currentContent - the content created from e.g. inner tags, inner content and not yet added to document.
      Returns:
      a List containing iText Element objects
    • isStackOwner

      public boolean isStackOwner()
      Defaults to false.
      Specified by:
      isStackOwner in interface TagProcessor
      Returns:
      true if the tag implementation must keep it's own currentContent stack.
      See Also:
    • currentContentToParagraph

      public List<Element> currentContentToParagraph(List<Element> currentContent, boolean addNewLines, boolean applyCSS, Tag tag, WorkerContext ctx)
      Adds currentContent list to a paragraph element. If addNewLines is true a Paragraph object is returned, else a NoNewLineParagraph object is returned.
      Parameters:
      currentContent - List of the current elements to be added.
      addNewLines - boolean to declare which paragraph element should be returned, true if new line should be added or not.
      applyCSS - true if CSS should be applied on the paragraph.
      tag - the relevant tag.
      ctx - the WorkerContext.
      Returns:
      a List of paragraphs.
    • currentContentToParagraph

      public final List<Element> currentContentToParagraph(List<Element> currentContent, boolean addNewLines)
      Default apply CSS to false and tag to null.
      Parameters:
      currentContent - List of the current elements to be added.
      addNewLines - boolean to declare which paragraph element should be returned, true if new line should be added or not.
      Returns:
      a List with paragraphs
      See Also:
    • setCssAppliers

      public void setCssAppliers(CssAppliers cssAppliers)
      Specified by:
      setCssAppliers in interface CssAppliersAware
      Parameters:
      cssAppliers - the CssAppliers
    • getCssAppliers

      public CssAppliers getCssAppliers()
      Specified by:
      getCssAppliers in interface CssAppliersAware
      Returns:
      the CssAppliers
    • createParagraph

      protected Paragraph createParagraph()
    • doRtlIndentCorrections

      protected void doRtlIndentCorrections(Indentable p)
    • invertTextAlignForParagraph

      protected void invertTextAlignForParagraph(Paragraph p)
    • invertTextAlignForParagraph

      protected void invertTextAlignForParagraph(NoNewLineParagraph p)
    • updateParagraphFontIfNeeded

      protected void updateParagraphFontIfNeeded(Phrase p, Element child)
      In case child font is of bigger size than paragraph font, text overlapping may occur. This happens because leading of the lines in paragraph is set based on paragraph font.