Example #1
0
        protected override void ProcessTable(HWPFDocumentCore wordDocument, XmlElement flow, Table table)
        {
            XmlElement tableHeader = htmlDocumentFacade.CreateTableHeader();
            XmlElement tableBody   = htmlDocumentFacade.CreateTableBody();

            int[] tableCellEdges = WordToHtmlUtils.BuildTableCellEdgesArray(table);
            int   tableRows      = table.NumRows;

            int maxColumns = int.MinValue;

            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);

                XmlElement    tableRowElement = htmlDocumentFacade.CreateTableRow();
                StringBuilder tableRowStyle   = new StringBuilder();
                WordToHtmlUtils.AddTableRowProperties(tableRow, tableRowStyle);

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

                    if (tableCell.IsVerticallyMerged() && !tableCell.IsFirstVerticallyMerged())
                    {
                        currentEdgeIndex += getTableCellEdgesIndexSkipCount(table, r, tableCellEdges, currentEdgeIndex, c, tableCell);
                        continue;
                    }

                    XmlElement 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", colSpan.ToString());
                    }

                    int rowSpan = GetNumberRowsSpanned(table, r, c,
                                                       tableCell);
                    if (rowSpan > 1)
                    {
                        tableCellElement.SetAttribute("rowspan", rowSpan.ToString());
                    }

                    ProcessParagraphes(wordDocument, tableCellElement, tableCell, 0 /*table.TableLevel Todo: */);

                    if (!tableCellElement.HasChildNodes)
                    {
                        tableCellElement.AppendChild(htmlDocumentFacade.CreateParagraph());
                    }
                    if (tableCellStyle.Length > 0)
                    {
                        htmlDocumentFacade.AddStyleClass(tableCellElement, tableCellElement.LocalName, tableCellStyle.ToString());
                    }

                    tableRowElement.AppendChild(tableCellElement);
                }

                if (tableRowStyle.Length > 0)
                {
                    tableRowElement.SetAttribute("class", htmlDocumentFacade.GetOrCreateCssClass("tr", "r", tableRowStyle.ToString()));
                }

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

            XmlElement tableElement = htmlDocumentFacade.CreateTable();

            tableElement.SetAttribute("class",
                                      htmlDocumentFacade.GetOrCreateCssClass(tableElement.LocalName, "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 [", table.StartOffset.ToString(), "; ", table.EndOffset.ToString(), ")");
            }
        }