Exemplo n.º 1
0
  protected void processNoteAutonumbered(
      HWPFDocument doc, String type, int noteIndex, Element block, Range noteTextRange) {
    final String textIndex = String.valueOf(noteIndex + 1);
    final String textIndexClass =
        htmlDocumentFacade.getOrCreateCssClass("a", "vertical-align:super;font-size:smaller;");
    final String forwardNoteLink = type + "note_" + textIndex;
    final String backwardNoteLink = type + "note_back_" + textIndex;

    Element anchor = htmlDocumentFacade.createHyperlink("#" + forwardNoteLink);
    anchor.setAttribute("name", backwardNoteLink);
    anchor.setAttribute("class", textIndexClass + " " + type + "noteanchor");
    anchor.setTextContent(textIndex);
    block.appendChild(anchor);

    if (notes == null) {
      notes = htmlDocumentFacade.createBlock();
      notes.setAttribute("class", "notes");
    }

    Element note = htmlDocumentFacade.createBlock();
    note.setAttribute("class", type + "note");
    notes.appendChild(note);

    Element bookmark = htmlDocumentFacade.createBookmark(forwardNoteLink);
    bookmark.setAttribute("href", "#" + backwardNoteLink);
    bookmark.setTextContent(textIndex);
    bookmark.setAttribute("class", textIndexClass + " " + type + "noteindex");
    note.appendChild(bookmark);
    note.appendChild(htmlDocumentFacade.createText(" "));

    Element span = htmlDocumentFacade.getDocument().createElement("span");
    span.setAttribute("class", type + "notetext");
    note.appendChild(span);

    this.blocksProperies.add(new BlockProperies("", -1));
    try {
      processCharacters(doc, Integer.MIN_VALUE, noteTextRange, span);
    } finally {
      this.blocksProperies.pop();
    }
  }
Exemplo n.º 2
0
  /** 解析table */
  protected void processTable(HWPFDocument hwpfDocument, Element flow, Table table) {
    Element tableHeader = htmlDocumentFacade.createTableHeader();
    Element tableBody = htmlDocumentFacade.createTableBody();

    final int[] tableCellEdges = WordToHtmlUtils.buildTableCellEdgesArray(table);
    final int tableRows = table.numRows();

    int maxColumns = Integer.MIN_VALUE;
    for (int r = 0; r < tableRows; r++) {
      maxColumns = Math.max(maxColumns, table.getRow(r).numCells());
    }

    for (int r = 0; r < tableRows; r++) {
      TableRow tableRow = table.getRow(r);

      Element tableRowElement = htmlDocumentFacade.createTableRow();
      StringBuilder tableRowStyle = new StringBuilder();

      WordToHtmlUtils.addTableRowProperties(tableRow, tableRowStyle);

      // index of current element in tableCellEdges[]
      int currentEdgeIndex = 0;
      final int rowCells = tableRow.numCells();
      for (int c = 0; c < rowCells; c++) {
        TableCell tableCell = tableRow.getCell(c);

        if (tableCell.isVerticallyMerged() && !tableCell.isFirstVerticallyMerged()) {
          currentEdgeIndex += getNumberColumnsSpanned(tableCellEdges, currentEdgeIndex, tableCell);
          continue;
        }

        Element tableCellElement;
        if (tableRow.isTableHeader()) {
          tableCellElement = htmlDocumentFacade.createTableHeaderCell();
        } else {
          tableCellElement = htmlDocumentFacade.createTableCell();
        }
        StringBuilder tableCellStyle = new StringBuilder();
        WordToHtmlUtils.addTableCellProperties(
            tableRow,
            tableCell,
            r == 0,
            r == tableRows - 1,
            c == 0,
            c == rowCells - 1,
            tableCellStyle);

        int colSpan = getNumberColumnsSpanned(tableCellEdges, currentEdgeIndex, tableCell);
        currentEdgeIndex += colSpan;

        if (colSpan == 0) continue;

        if (colSpan != 1) tableCellElement.setAttribute("colspan", String.valueOf(colSpan));

        final int rowSpan = getNumberRowsSpanned(table, tableCellEdges, r, c, tableCell);
        if (rowSpan > 1) tableCellElement.setAttribute("rowspan", String.valueOf(rowSpan));

        processParagraphes(hwpfDocument, tableCellElement, tableCell, table.getTableLevel());

        if (!tableCellElement.hasChildNodes()) {
          tableCellElement.appendChild(htmlDocumentFacade.createParagraph());
        }
        if (tableCellStyle.length() > 0)
          htmlDocumentFacade.addStyleClass(
              tableCellElement, tableCellElement.getTagName(), tableCellStyle.toString());

        tableRowElement.appendChild(tableCellElement);
      }

      if (tableRowStyle.length() > 0)
        tableRowElement.setAttribute(
            "class", htmlDocumentFacade.getOrCreateCssClass("r", tableRowStyle.toString()));

      if (tableRow.isTableHeader()) {
        tableHeader.appendChild(tableRowElement);
      } else {
        tableBody.appendChild(tableRowElement);
      }
    }

    final Element tableElement = htmlDocumentFacade.createTable();
    tableElement.setAttribute(
        "class",
        htmlDocumentFacade.getOrCreateCssClass(
            "t", "table-layout:fixed;border-collapse:collapse;border-spacing:0;"));
    if (tableHeader.hasChildNodes()) {
      tableElement.appendChild(tableHeader);
    }
    if (tableBody.hasChildNodes()) {
      tableElement.appendChild(tableBody);
      flow.appendChild(tableElement);
    } else {
      logger.log(
          POILogger.WARN,
          "Table without body starting at [",
          Integer.valueOf(table.getStartOffset()),
          "; ",
          Integer.valueOf(table.getEndOffset()),
          ")");
    }
  }