Exemplo n.º 1
0
 /**
  * Processes all actions contained in the cell.
  *
  * @param element an element in the cell
  * @param action an action that should be coupled to the cell
  * @param allActions
  */
 protected void processActions(Element element, PdfAction action, ArrayList allActions) {
   if (element.type() == Element.ANCHOR) {
     String url = ((Anchor) element).reference();
     if (url != null) {
       action = new PdfAction(url);
     }
   }
   Iterator i;
   switch (element.type()) {
     case Element.PHRASE:
     case Element.SECTION:
     case Element.ANCHOR:
     case Element.CHAPTER:
     case Element.LISTITEM:
     case Element.PARAGRAPH:
       for (i = ((ArrayList) element).iterator(); i.hasNext(); ) {
         processActions((Element) i.next(), action, allActions);
       }
       break;
     case Element.CHUNK:
       allActions.add(action);
       break;
     case Element.LIST:
       for (i = ((List) element).getItems().iterator(); i.hasNext(); ) {
         processActions((Element) i.next(), action, allActions);
       }
       break;
     default:
       int n = element.getChunks().size();
       while (n-- > 0) allActions.add(action);
       break;
   }
 }
Exemplo n.º 2
0
  /**
   * Constructs a new RtfList for the specified List.
   *
   * @param doc The RtfDocument this RtfList belongs to
   * @param list The List this RtfList is based on
   */
  public RtfList(RtfDocument doc, List list) {
    super(doc);

    this.listNumber = document.getDocumentHeader().getListNumber(this);

    this.items = new ArrayList();
    if (list.getSymbolIndent() > 0 && list.getIndentationLeft() > 0) {
      this.firstIndent = (int) (list.getSymbolIndent() * RtfElement.TWIPS_FACTOR * -1);
      this.leftIndent =
          (int) ((list.getIndentationLeft() + list.getSymbolIndent()) * RtfElement.TWIPS_FACTOR);
    } else if (list.getSymbolIndent() > 0) {
      this.firstIndent = (int) (list.getSymbolIndent() * RtfElement.TWIPS_FACTOR * -1);
      this.leftIndent = (int) (list.getSymbolIndent() * RtfElement.TWIPS_FACTOR);
    } else if (list.getIndentationLeft() > 0) {
      this.firstIndent = 0;
      this.leftIndent = (int) (list.getIndentationLeft() * RtfElement.TWIPS_FACTOR);
    } else {
      this.firstIndent = 0;
      this.leftIndent = 0;
    }
    this.rightIndent = (int) (list.getIndentationRight() * RtfElement.TWIPS_FACTOR);
    this.symbolIndent =
        (int) ((list.getSymbolIndent() + list.getIndentationLeft()) * RtfElement.TWIPS_FACTOR);

    if (list instanceof RomanList) {
      if (list.isLowercase()) {
        this.listType = LIST_TYPE_LOWER_ROMAN;
      } else {
        this.listType = LIST_TYPE_UPPER_ROMAN;
      }
    } else if (list.isNumbered()) {
      this.listType = LIST_TYPE_NUMBERED;
    } else if (list.isLettered()) {
      if (list.isLowercase()) {
        this.listType = LIST_TYPE_LOWER_LETTERS;
      } else {
        this.listType = LIST_TYPE_UPPER_LETTERS;
      }
    }

    for (int i = 0; i < list.getItems().size(); i++) {
      try {
        Element element = (Element) list.getItems().get(i);
        if (element.type() == Element.CHUNK) {
          element = new ListItem((Chunk) element);
        }
        if (element instanceof ListItem) {
          this.alignment = ((ListItem) element).getAlignment();
        }
        RtfBasicElement rtfElement = doc.getMapper().mapElement(element);
        if (rtfElement instanceof RtfList) {
          ((RtfList) rtfElement).setListNumber(listNumber);
          ((RtfList) rtfElement).setListLevel(listLevel + 1);
          ((RtfList) rtfElement).setParent(this);
        } else if (rtfElement instanceof RtfListItem) {
          ((RtfListItem) rtfElement).setParent(this);
          ((RtfListItem) rtfElement).inheritListSettings(listNumber, listLevel + 1);
        }
        items.add(rtfElement);
      } catch (DocumentException de) {
        de.printStackTrace();
      }
    }

    if (this.listLevel == 0) {
      correctIndentation();
    }

    fontNumber =
        new RtfFont(document, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL, new Color(0, 0, 0)));
    if (list.getSymbol() != null
        && list.getSymbol().getFont() != null
        && !list.getSymbol().getContent().startsWith("-")
        && list.getSymbol().getContent().length() > 0) {
      // only set this to bullet symbol is not default
      this.fontBullet = new RtfFont(document, list.getSymbol().getFont());
      this.bulletCharacter = list.getSymbol().getContent().substring(0, 1);
    } else {
      this.fontBullet =
          new RtfFont(document, new Font(Font.SYMBOL, 10, Font.NORMAL, new Color(0, 0, 0)));
    }
  }
Exemplo n.º 3
0
  /**
   * Constructs a <CODE>PdfCell</CODE>-object.
   *
   * @param cell the original <CODE>Cell</CODE>
   * @param rownumber the number of the <CODE>Row</CODE> the <CODE>Cell</CODE> was in.
   * @param left the left border of the <CODE>PdfCell</CODE>
   * @param right the right border of the <CODE>PdfCell</CODE>
   * @param top the top border of the <CODE>PdfCell</CODE>
   * @param cellspacing the cellspacing of the <CODE>Table</CODE>
   * @param cellpadding the cellpadding of the <CODE>Table</CODE>
   */
  public PdfCell(
      Cell cell,
      int rownumber,
      float left,
      float right,
      float top,
      float cellspacing,
      float cellpadding) {
    // constructs a Rectangle (the bottomvalue will be changed afterwards)
    super(left, top, right, top);
    // copying the other Rectangle attributes from class Cell
    cloneNonPositionParameters(cell);
    this.cellpadding = cellpadding;
    this.cellspacing = cellspacing;
    this.verticalAlignment = cell.verticalAlignment();
    this.useAscender = cell.isUseAscender();
    this.useDescender = cell.isUseDescender();
    this.useBorderPadding = cell.isUseBorderPadding();

    // initialisation of some parameters
    PdfChunk chunk;
    Element element;
    PdfChunk overflow;
    lines = new ArrayList();
    images = new ArrayList();
    leading = cell.leading();
    int alignment = cell.horizontalAlignment();
    left += cellspacing + cellpadding;
    right -= cellspacing + cellpadding;

    left += getBorderWidthInside(LEFT);
    right -= getBorderWidthInside(RIGHT);

    contentHeight = 0;

    rowspan = cell.rowspan();

    ArrayList allActions;
    int aCounter;
    // we loop over all the elements of the cell
    for (Iterator i = cell.getElements(); i.hasNext(); ) {
      element = (Element) i.next();
      switch (element.type()) {
        case Element.JPEG:
        case Element.IMGRAW:
        case Element.IMGTEMPLATE:
          addImage((Image) element, left, right, 0.4f * leading, alignment); //
          break;
          // if the element is a list
        case Element.LIST:
          if (line != null && line.size() > 0) {
            line.resetAlignment();
            addLine(line);
          }
          allActions = new ArrayList();
          processActions(element, null, allActions);
          aCounter = 0;
          ListItem item;
          // we loop over all the listitems
          for (Iterator items = ((List) element).getItems().iterator(); items.hasNext(); ) {
            item = (ListItem) items.next();
            line = new PdfLine(left + item.indentationLeft(), right, alignment, item.leading());
            line.setListItem(item);
            for (Iterator j = item.getChunks().iterator(); j.hasNext(); ) {
              chunk = new PdfChunk((Chunk) j.next(), (PdfAction) (allActions.get(aCounter++)));
              while ((overflow = line.add(chunk)) != null) {
                addLine(line);
                line = new PdfLine(left + item.indentationLeft(), right, alignment, item.leading());
                chunk = overflow;
              }
              line.resetAlignment();
              addLine(line);
              line = new PdfLine(left + item.indentationLeft(), right, alignment, leading);
            }
          }
          line = new PdfLine(left, right, alignment, leading);
          break;
          // if the element is something else
        default:
          allActions = new ArrayList();
          processActions(element, null, allActions);
          aCounter = 0;

          float currentLineLeading = leading;
          float currentLeft = left;
          float currentRight = right;
          if (element instanceof Phrase) {
            currentLineLeading = ((Phrase) element).leading();
          }
          if (element instanceof Paragraph) {
            Paragraph p = (Paragraph) element;
            currentLeft += p.indentationLeft();
            currentRight -= p.indentationRight();
          }
          if (line == null) {
            line = new PdfLine(currentLeft, currentRight, alignment, currentLineLeading);
          }
          // we loop over the chunks
          ArrayList chunks = element.getChunks();
          if (chunks.isEmpty()) {
            addLine(line); // add empty line - all cells need some lines even if they are empty
            line = new PdfLine(currentLeft, currentRight, alignment, currentLineLeading);
          } else {
            for (Iterator j = chunks.iterator(); j.hasNext(); ) {
              Chunk c = (Chunk) j.next();
              chunk = new PdfChunk(c, (PdfAction) (allActions.get(aCounter++)));
              while ((overflow = line.add(chunk)) != null) {
                addLine(line);
                line = new PdfLine(currentLeft, currentRight, alignment, currentLineLeading);
                chunk = overflow;
              }
            }
          }
          // if the element is a paragraph, section or chapter, we reset the alignment and add the
          // line
          switch (element.type()) {
            case Element.PARAGRAPH:
            case Element.SECTION:
            case Element.CHAPTER:
              line.resetAlignment();
              flushCurrentLine();
          }
      }
    }
    flushCurrentLine();
    if (lines.size() > cell.getMaxLines()) {
      while (lines.size() > cell.getMaxLines()) {
        removeLine(lines.size() - 1);
      }
      if (cell.getMaxLines() > 0) {
        String more = cell.getShowTruncation();
        if (more != null && more.length() > 0) {
          // Denote that the content has been truncated
          lastLine = (PdfLine) lines.get(lines.size() - 1);
          if (lastLine.size() >= 0) {
            PdfChunk lastChunk = lastLine.getChunk(lastLine.size() - 1);
            float moreWidth = new PdfChunk(more, lastChunk).width();
            while (lastChunk.toString().length() > 0
                && lastChunk.width() + moreWidth > right - left) {
              // Remove characters to leave room for the 'more' indicator
              lastChunk.setValue(lastChunk.toString().substring(0, lastChunk.length() - 1));
            }
            lastChunk.setValue(lastChunk.toString() + more);
          } else {
            lastLine.add(new PdfChunk(new Chunk(more), null));
          }
        }
      }
    }
    // we set some additional parameters
    if (useDescender && lastLine != null) {
      contentHeight -= lastLine.getDescender();
    }

    // adjust first line height so that it touches the top
    if (lines.size() > 0) {
      firstLine = (PdfLine) lines.get(0);
      float firstLineRealHeight = firstLineRealHeight();
      contentHeight -= firstLine.height();
      firstLine.height = firstLineRealHeight;
      contentHeight += firstLineRealHeight;
    }

    float newBottom = top - contentHeight - (2f * cellpadding()) - (2f * cellspacing());
    newBottom -= getBorderWidthInside(TOP) + getBorderWidthInside(BOTTOM);
    setBottom(newBottom);

    this.rownumber = rownumber;
  }