void ProcessWordTable(WordDocHolder docHolder, Table table, int maxRowsToProcess) { var rows = table.Descendants <TableRow>().ToList(); TableWidthInfo widthInfo = InitializeTableWidthInfo(docHolder, table); int saveRowsCount = TableRows.Count; int maxCellsCount = 0; for (int r = 0; r < rows.Count(); ++r) { List <OpenXmlWordCell> newRow = new List <OpenXmlWordCell>(); int sumspan = 0; var row = rows[r]; int rowGridBefore = GetRowGridBefore(row); bool isEmpty = true; foreach (var rowCell in row.Elements <TableCell>()) { var c = new OpenXmlWordCell(docHolder, widthInfo, rowCell, TableRows.Count, sumspan); if (newRow.Count == 0) { c.MergedColsCount += rowGridBefore; } newRow.Add(c); sumspan += c.MergedColsCount; isEmpty = isEmpty && c.IsEmpty; } if (isEmpty) { continue; } maxCellsCount = Math.Max(newRow.Count, maxCellsCount); if (r == 0 && TableRows.Count > 0 && BigramsHolder.CheckMergeRow( TableRows.Last().ConvertAll(x => x.Text), newRow.ConvertAll(x => x.Text))) { MergeRow(TableRows.Last(), newRow); } else { TableRows.Add(newRow); } if ((maxRowsToProcess != -1) && (TableRows.Count >= maxRowsToProcess)) { break; } } if (maxCellsCount <= 4 || CheckNameColumnIsEmpty(TableRows, saveRowsCount)) { //remove this suspicious table TableRows.RemoveRange(saveRowsCount, TableRows.Count - saveRowsCount); } }
public OpenXmlWordCell(WordDocHolder docHolder, TableWidthInfo tableWidth, TableCell inputCell, int row, int column) { InitTextProperties(docHolder, inputCell); var vmerge = inputCell.TableCellProperties.GetFirstChild <VerticalMerge>(); if (vmerge == null) { IsVerticallyMerged = false; } else { if (vmerge == null || vmerge.Val == null || vmerge.Val == MergedCellValues.Continue) { IsVerticallyMerged = true; } else { //vmerge.Val == MergedCellValues.Restart IsVerticallyMerged = false; } } var gridSpan = inputCell.TableCellProperties.GetFirstChild <GridSpan>(); IsMerged = gridSpan != null && gridSpan.Val > 1; FirstMergedRow = -1; // init afterwards MergedRowsCount = -1; // init afterwards MergedColsCount = (gridSpan == null) ? 1 : (int)gridSpan.Val; Row = row; Col = column; if (inputCell.TableCellProperties != null && inputCell.TableCellProperties.TableCellWidth != null && inputCell.TableCellProperties.TableCellWidth.Type != null && inputCell.TableCellProperties.TableCellWidth.Type != TableWidthUnitValues.Auto ) { CellWidth = TableWidthInfo.TryReadWidth( inputCell.TableCellProperties.TableCellWidth.Width, inputCell.TableCellProperties.TableCellWidth.Type, tableWidth.TableWidthInPixels); } else { if (Col < tableWidth.ColumnWidths.Count) { CellWidth = tableWidth.ColumnWidths[Col]; } } AdditTableIndention = tableWidth.TableIndentionInPixels; }
TableWidthInfo InitializeTableWidthInfo(WordDocHolder docHolder, Table table) { TableWidthInfo widthInfo = new TableWidthInfo(); TableProperties tProp = table.GetFirstChild <TableProperties>(); if (tProp != null) { if (tProp.TableWidth != null) { widthInfo.TableWidthInPixels = TableWidthInfo.TryReadWidth( tProp.TableWidth.Width, tProp.TableWidth.Type, docHolder.DocumentPageSizeInPixels); } if (tProp.TableIndentation != null) { widthInfo.TableIndentionInPixels = TableWidthInfo.TryReadWidth( tProp.TableIndentation.Width, tProp.TableIndentation.Type, docHolder.DocumentPageSizeInPixels); } widthInfo.TableIndentionInPixels += docHolder.DocumentPageLeftMaginInPixels; } else { widthInfo.TableWidthInPixels = docHolder.DocumentPageSizeInPixels; } TableGrid tGrid = table.GetFirstChild <TableGrid>(); if (tGrid != null) { widthInfo.ColumnWidths = new List <int>(); foreach (var col in tGrid.Elements <GridColumn>()) { widthInfo.ColumnWidths.Add( TableWidthInfo.TryReadWidth( col.Width, TableWidthUnitValues.Dxa, widthInfo.TableWidthInPixels)); } } return(widthInfo); }
void InitPageSize() { var docPart = WordDocument.MainDocumentPart; var pageSize = docPart.Document.Descendants <PageSize>().FirstOrDefault(); int pageDxa = 11906; // letter size is ISO 216 A4 (210x297mm if (pageSize != null) { pageDxa = (int)(uint)pageSize.Width; } DocumentPageSizeInPixels = TableWidthInfo.DxaToPixels(pageDxa); var pageMargin = docPart.Document.Descendants <PageMargin>().FirstOrDefault(); int pageMarginDxa = 0; // letter size is ISO 216 A4 (210x297mm if (pageMargin != null && pageMargin.Left != null) { pageMarginDxa = (int)(uint)pageMargin.Left; } DocumentPageLeftMaginInPixels = TableWidthInfo.DxaToPixels(pageMarginDxa); }