Esempio n. 1
0
        public TableRow(int startIdx, int endIdx, Table parent, int levelNum)
            : base(startIdx, endIdx, parent)
        {

            Paragraph last = GetParagraph(NumParagraphs - 1);
            _papx = last._papx;
            _tprops = TableSprmUncompressor.UncompressTAP(_papx);
            _levelNum = levelNum;
            initCells();

        }
        private Dictionary <string, string> getFileDict(string docFileName, out string dh)
        {
            dh = "";
            var dict = new Dictionary <string, string>();

            if (File.Exists(docFileName))
            {
                using (Stream stream = new FileStream(docFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    HWPFDocument hd             = new HWPFDocument(stream);
                    var          paraTable      = hd.ParagraphTable;
                    Range        rang           = hd.GetRange();
                    int          paragraphCount = rang.NumParagraphs;
                    for (int i = 0; i < paragraphCount; i++)
                    {
                        var pph  = rang.GetParagraph(i);
                        var text = pph.Text.Replace(":", ":").Replace(" ", "").Trim();
                        if (text.StartsWith("档号:"))
                        {
                            dh = text.Replace("档号:", "").Trim();
                            break;
                        }
                    }
                    rang = hd.GetRange();
                    TableIterator it = new TableIterator(rang);
                    while (it.HasNext())
                    {
                        NPOI.HWPF.UserModel.Table tb = (NPOI.HWPF.UserModel.Table)it.Next();
                        for (int i = 0; i < tb.NumRows; i++)
                        {
                            var row       = tb.GetRow(i);
                            var cellCount = row.numCells();
                            if (cellCount > 1)
                            {
                                var key   = row.GetCell(0).Text.Trim().Replace("\a", "");
                                var value = row.GetCell(1).Text.Trim().Replace("\a", "");
                                if (!dict.ContainsKey(key))
                                {
                                    dict.Add(key, value);
                                }
                            }
                        }
                    }
                }
            }
            return(dict);
        }
Esempio n. 3
0
        /**
     * Creates array of all possible cell edges. In HTML (and FO) cells from
     * different rows and same column should have same width, otherwise spanning
     * shall be used.
     * 
     * @param table
     *            table to build cell edges array from
     * @return array of cell edges (including leftest one) in twips
     */
        public static int[] BuildTableCellEdgesArray(Table table)
        {
            SortedList<int, int> edges = new SortedList<int, int>();

            for (int r = 0; r < table.NumRows; r++)
            {
                TableRow tableRow = table.GetRow(r);
                for (int c = 0; c < tableRow.NumCells(); c++)
                {
                    TableCell tableCell = tableRow.GetCell(c);
                    if (!edges.ContainsKey(tableCell.GetLeftEdge()))
                        edges.Add(tableCell.GetLeftEdge(), 0);
                    if (!edges.ContainsKey(tableCell.GetLeftEdge() + tableCell.GetWidth()))
                        edges.Add(tableCell.GetLeftEdge() + tableCell.GetWidth(), 0);
                }
            }

            int[] result = new int[edges.Count];

            edges.Keys.CopyTo(result, 0);
            return result;
        }
Esempio n. 4
0
        protected override void ProcessTable(HWPFDocumentCore wordDocument, XmlElement flow,
                Table table)
        {
            XmlElement tableHeader = foDocumentFacade.CreateTableHeader();
            XmlElement tableBody = foDocumentFacade.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 = foDocumentFacade.CreateTableRow();
                WordToFoUtils.SetTableRowProperties(tableRow, tableRowElement);

                // 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 = foDocumentFacade.CreateTableCell();
                    WordToFoUtils.SetTableCellProperties(tableRow, tableCell,
                            tableCellElement, r == 0, r == tableRows - 1, c == 0,
                            c == rowCells - 1);

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

                    if (colSpan == 0)
                        continue;

                    if (colSpan != 1)
                        tableCellElement.SetAttribute("number-columns-spanned", (colSpan).ToString());

                    int rowSpan = GetNumberRowsSpanned(table, r, c, tableCell);
                    if (rowSpan > 1)
                        tableCellElement.SetAttribute("number-rows-spanned", (rowSpan).ToString());

                    ProcessParagraphes(wordDocument, tableCellElement, tableCell,
                            table.TableLevel);

                    if (!tableCellElement.HasChildNodes)
                    {
                        tableCellElement.AppendChild(foDocumentFacade
                                .CreateBlock());
                    }

                    tableRowElement.AppendChild(tableCellElement);
                }

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

            XmlElement tableElement = foDocumentFacade.CreateTable();
            tableElement.SetAttribute("table-layout", "fixed");
            if (tableHeader.HasChildNodes)
            {
                tableElement.AppendChild(tableHeader);
            }
            if (tableBody.HasChildNodes)
            {
                tableElement.AppendChild(tableBody);
                flow.AppendChild(tableElement);
            }
            else
            {
                logger.Log(POILogger.WARN, "Table without body starting on offset " + table.StartOffset + " -- " + table.EndOffset);
            }
        }
Esempio n. 5
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(), ")");
            }
        }
Esempio n. 6
0
 internal Paragraph(int startIdx, int endIdx, Table parent)
     : base(startIdx, endIdx, parent)
 {
     InitAll();
     PAPX papx = (PAPX)_paragraphs[_parEnd - 1];
     _props = papx.GetParagraphProperties(_doc.GetStyleSheet());
     _papx = papx.GetSprmBuf();
     _istd = papx.GetIstd();
 }
        protected override void ProcessTable(HWPFDocumentCore wordDocument, XmlElement flow,
                Table table)
        {
            int tableRows = table.NumRows;
            for (int r = 0; r < tableRows; r++)
            {
                TableRow tableRow = table.GetRow(r);

                XmlElement tableRowElement = textDocumentFacade.CreateTableRow();

                int rowCells = tableRow.NumCells();
                for (int c = 0; c < rowCells; c++)
                {
                    TableCell tableCell = tableRow.GetCell(c);

                    XmlElement tableCellElement = textDocumentFacade.CreateTableCell();

                    if (c != 0)
                        tableCellElement.AppendChild(textDocumentFacade
                                .CreateText("\t"));
                    ProcessCharacters(wordDocument, table.TableLevel, tableCell, tableCellElement);
                    tableRowElement.AppendChild(tableCellElement);
                }

                tableRowElement.AppendChild(textDocumentFacade.CreateText("\n"));
                flow.AppendChild(tableRowElement);
            }
        }