Beispiel #1
0
  @Override
  protected void processParagraphes(
      HWPFDocument wordDocument, Element flow, Range range, int currentTableLevel) {
    // TODO  mc process paragraphes

    final ListTables listTables = wordDocument.getListTables();
    int currentListInfo = 0;

    final int paragraphs = range.numParagraphs();
    for (int p = 0; p < paragraphs; p++) {
      Paragraph paragraph = range.getParagraph(p);

      //			加入图片
      CharacterRun cr = paragraph.getCharacterRun(0);
      this.processImage(flow, cr);
      //          table
      if (paragraph.isInTable() && paragraph.getTableLevel() != currentTableLevel) {
        if (paragraph.getTableLevel() < currentTableLevel)
          throw new IllegalStateException(
              "Trying to process table cell with higher level ("
                  + paragraph.getTableLevel()
                  + ") than current table level ("
                  + currentTableLevel
                  + ") as inner table part");

        Table table = range.getTable(paragraph);
        processTable(wordDocument, flow, table);

        p += table.numParagraphs();
        p--;
        continue;
      }
      //          换页
      if (paragraph.text().equals("\u000c")) {
        processPageBreak(wordDocument, flow);
      }
      if (paragraph.getIlfo() != currentListInfo) {
        currentListInfo = paragraph.getIlfo();
      }
      //          嵌套段落
      if (currentListInfo != 0) {
        if (listTables != null) {

          final ListFormatOverride listFormatOverride = listTables.getOverride(paragraph.getIlfo());

          String label = getBulletText(listTables, paragraph, listFormatOverride.getLsid());

          if ("".equals(label)) {
            itemSymbol = true;
            /*
            Element span = htmlDocumentFacade.getDocument().createElement("span");
            span.setAttribute("style", "font-size:12.0pt;line-height:150%;font-family:Wingdings;mso-ascii-font-family:Wingdings;mso-hide:none;mso-ansi-language:EN-US;mso-fareast-language:ZH-CN;font-weight:normal;mso-bidi-font-weight:normal;font-style:normal;mso-bidi-font-style:normal;text-underline:windowtext none;text-decoration:none;background:transparent");
            span.setTextContent("Ø");

            flow.appendChild(span);
            */
          }

          processParagraph(wordDocument, flow, currentTableLevel, paragraph, label);
        } else {
          logger.log(
              POILogger.WARN,
              "Paragraph #"
                  + paragraph.getStartOffset()
                  + "-"
                  + paragraph.getEndOffset()
                  + " has reference to list structure #"
                  + currentListInfo
                  + ", but listTables not defined in file");

          processParagraph(
              wordDocument, flow, currentTableLevel, paragraph, AbstractWordUtils.EMPTY);
        }
      } else {
        processParagraph(wordDocument, flow, currentTableLevel, paragraph, AbstractWordUtils.EMPTY);
      }
    }
  }
Beispiel #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()),
          ")");
    }
  }