private static OpenXmlElement PartToOpenXmlElement(IPageElement part, WordprocessingDocument document) { var paragraph = part as Core.Paragraph; if (paragraph != null) { return(ParagraphConverter.Convert(paragraph, document)); } var table = part as Table; if (table != null) { return(TableConverter.Convert(table, document)); } throw new InvalidOperationException($"can't convert part of page with type [{part.GetType()}] to OpenXmlElement"); }
public static TableCell Convert(Cell cell, WordprocessingDocument document, Table table, int span) { var tableCell = new TableCell(); var cellProperties = new TableCellProperties(); var parameters = cell.Parameters; if (span > 1) { cellProperties.GridSpan = new GridSpan { Val = span } } ; cellProperties.TableCellVerticalAlignment = new TableCellVerticalAlignment { Val = GetVerticalAlignment(parameters.VerticalAlignment ?? VerticalAlignment.Bottom) }; cellProperties.TableCellMargin = GetMargin(parameters.MarginLeft ?? table.Parameters.CellMarginLeft, parameters.MarginRight ?? table.Parameters.CellMarginRight, parameters.MarginTop ?? table.Parameters.CellMarginTop, parameters.MarginBottom ?? table.Parameters.CellMarginBottom); if (parameters.MergeDown) { cellProperties.VerticalMerge = new VerticalMerge { Val = MergedCellValues.Restart } } ; else if (parameters.MergeUp) { cellProperties.VerticalMerge = new VerticalMerge(); } var borders = parameters.Borders ?? table.Parameters.Borders ?? Borders.None; if (borders != Borders.None) { cellProperties.TableCellBorders = new TableCellBorders { BottomBorder = borders.HasFlag(Borders.Bottom) ? GetBorder <BottomBorder>(parameters.BottomBorderStyle, parameters.BorderSize) : null, LeftBorder = borders.HasFlag(Borders.Left) ? GetBorder <LeftBorder>(parameters.LeftBorderStyle, parameters.BorderSize) : null, TopBorder = borders.HasFlag(Borders.Top) ? GetBorder <TopBorder>(parameters.TopBorderStyle, parameters.BorderSize) : null, RightBorder = borders.HasFlag(Borders.Right) ? GetBorder <RightBorder>(parameters.RightBorderStyle, parameters.BorderSize) : null } } ; if (parameters.TextDirection.HasValue) { cellProperties.TextDirection = new DocumentFormat.OpenXml.Wordprocessing.TextDirection { Val = ConvertTextDirection(parameters.TextDirection.Value) } } ; if (parameters.BackgroundColor.HasValue) { cellProperties.Shading = new Shading { Color = parameters.BackgroundColor.Value.ToHex(), Fill = "auto", Val = ShadingPatternValues.Solid } } ; tableCell.AppendChild(ParagraphConverter.Convert(parameters.Paragraph, document, table.Parameters.FontSize)); tableCell.TableCellProperties = cellProperties; return(tableCell); }