예제 #1
0
        public static TableRow GetTableRow(string[] data, int[] widths = null, bool header = false, bool verticalText = false)
        {
            var tr = new TableRow();

            for (int i = 0; i < data.Length; i++)
            {
                string d   = data[i];
                var    tc  = new TableCell();
                var    run = new Run(new Text(d));
                if (header)
                {
                    run.RunProperties = new RunProperties(new Bold());
                }
                var par = new Paragraph(run);

                ApplyStyleToParagraph(par, "TableContents");
                if (widths != null && widths.Length == data.Length)
                {
                    var tabProps =
                        new TableCellProperties(
                            new TableCellWidth {
                        Type = TableWidthUnitValues.Pct, Width = $"{widths[i] * 50 }"
                    },
                            new TableCellVerticalAlignment {
                        Val = TableVerticalAlignmentValues.Center
                    });
                    if (verticalText)
                    {
                        tabProps.AppendChild(new TextDirection()
                        {
                            Val = TextDirectionValues.BottomToTopLeftToRight
                        });
                    }
                    tc.Append(tabProps);
                }
                else
                {
                    var tabProps = new TableCellProperties(new TableCellWidth {
                        Type = TableWidthUnitValues.Auto
                    });
                    if (verticalText)
                    {
                        tabProps.AppendChild(new TextDirection()
                        {
                            Val = TextDirectionValues.BottomToTopLeftToRight
                        });
                        tabProps.AppendChild(new TableRowHeight()
                        {
                            Val = Convert.ToUInt32("1500")
                        });
                    }
                    tc.Append(tabProps);
                }
                tc.Append(par);
                tr.Append(tc);
            }
            return(tr);
        }
예제 #2
0
        private static TableCell SetupCell(HetOwner owner, double widthInCm, double start)
        {
            try
            {
                var tableCell = new TableCell();

                var tableCellProperties = new TableCellProperties();
                tableCellProperties.AppendChild(new TableCellWidth {
                    Width = CentimeterToDxa(widthInCm).ToString(), Type = TableWidthUnitValues.Dxa
                });
                tableCellProperties.AppendChild(new TableCellVerticalAlignment()
                {
                    Val = TableVerticalAlignmentValues.Center
                });
                tableCell.AppendChild(tableCellProperties);

                var paragraphProperties = new ParagraphProperties();

                var paragraphMarkRunProperties = new ParagraphMarkRunProperties();
                paragraphMarkRunProperties.AppendChild(new Color {
                    Val = "000000"
                });
                paragraphMarkRunProperties.AppendChild(new RunFonts {
                    Ascii = "Arial"
                });
                paragraphMarkRunProperties.AppendChild(new FontSize()
                {
                    Val = "13pt"
                });
                paragraphProperties.AppendChild(paragraphMarkRunProperties);

                paragraphProperties.AppendChild(new Justification {
                    Val = JustificationValues.Left
                });
                paragraphProperties.AppendChild(new Indentation {
                    Start = CentimeterToDxa(start).ToString()
                });

                var paragraph = new Paragraph();
                paragraph.AppendChild(paragraphProperties);

                if (owner != null)
                {
                    PopulateParagraph(owner, paragraph);
                }

                tableCell.AppendChild(paragraph);

                return(tableCell);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
예제 #3
0
        public void SetBackgroundColorOfTableCell(SdtElement contentControl, string color = "D9D9D9")
        {
            if (contentControl == null)
            {
                throw new ArgumentNullException("contentControl");
            }

            var tableCell = contentControl.Parent.Parent as TableCell;

            if (tableCell != null)
            {
                var tableCellProperties = new TableCellProperties( );

                var shading =
                    tableCellProperties.AppendChild(new Shading( )
                {
                    Val       = ShadingPatternValues.Solid,
                    Color     = color,
                    Fill      = "FFFFFF",
                    ThemeFill = ThemeColorValues.Background1
                });

                tableCell.AppendChild(tableCellProperties);
            }
        }
예제 #4
0
 /// <summary>
 /// Add border values to the cell properties
 /// </summary>
 /// <param name="cellProp"></param>
 /// <param name="cell"></param>
 /// <param name="isInAlternateRow"></param>
 private static void AddMargin(this TableCellProperties cellProp, Cell cell)
 {
     if (cell.Margin != null)
     {
         cellProp.AppendChild(new TableCellMargin()
         {
             LeftMargin = new LeftMargin()
             {
                 Width = cell.Margin.Left.ToString(CultureInfo.InvariantCulture), Type = TableWidthUnitValues.Dxa
             },
             TopMargin = new TopMargin()
             {
                 Width = cell.Margin.Top.ToString(CultureInfo.InvariantCulture), Type = TableWidthUnitValues.Dxa
             },
             RightMargin = new RightMargin()
             {
                 Width = cell.Margin.Right.ToString(CultureInfo.InvariantCulture), Type = TableWidthUnitValues.Dxa
             },
             BottomMargin = new BottomMargin()
             {
                 Width = cell.Margin.Bottom.ToString(CultureInfo.InvariantCulture), Type = TableWidthUnitValues.Dxa
             }
         });
     }
 }
예제 #5
0
        /// <summary>
        /// Add border values to the cell properties
        /// </summary>
        /// <param name="cellProp"></param>
        /// <param name="cell"></param>
        private static void AddJustifications(this TableCellProperties cellProp, Cell cell)
        {
            if (cell.VerticalAlignment.HasValue)
            {
                cellProp.AppendChild(new TableCellVerticalAlignment {
                    Val = cell.VerticalAlignment.Value.ToOOxml()
                });
            }

            if (cell.TextDirection.HasValue)
            {
                cellProp.AppendChild(new TextDirection {
                    Val = cell.TextDirection.ToOOxml()
                });
            }
        }
예제 #6
0
 /// <summary>
 /// Add border values to the cell properties
 /// </summary>
 /// <param name="cellProp"></param>
 /// <param name="cell"></param>
 /// <param name="isInAlternateRow"></param>
 private static void AddBorders(this TableCellProperties cellProp, Cell cell)
 {
     if (cell.Borders != null)
     {
         TableCellBorders borders = cell.Borders.RenderCellBorder();
         cellProp.AppendChild(borders);
     }
 }
예제 #7
0
        private static TableCell CreateCell(string text, bool?isThead = false)
        {
            var cell      = new TableCell();
            var paragraph = new Paragraph(new Run(new Text(text)));
            ParagraphProperties paragraphProperties = new ParagraphProperties();
            TableCellProperties cellProperties      = new TableCellProperties();

            cellProperties.AppendChild(new TableCellVerticalAlignment()
            {
                Val = TableVerticalAlignmentValues.Center
            });
            cellProperties.AppendChild(new Justification()
            {
                Val = JustificationValues.Center
            });

            TableCellWidth cellWidth = new TableCellWidth()
            {
                Type = TableWidthUnitValues.Auto
            };

            if (isThead.HasValue && isThead.Value)
            {
                paragraphProperties.AppendChild(new Justification()
                {
                    Val = JustificationValues.Center
                });
            }
            else
            {
                paragraphProperties.AppendChild(new Justification()
                {
                    Val = JustificationValues.Left
                });
            }
            paragraphProperties.AppendChild(new TextAlignment()
            {
                Val = VerticalTextAlignmentValues.Center
            });
            cell.AppendChild(cellWidth);
            cell.AppendChild(cellProperties);
            cell.AppendChild(paragraphProperties);
            cell.AppendChild(paragraph);
            return(cell);
        }
예제 #8
0
 private void AddTableHeaderStyle(TableRow wordTableHeader)
 {
     foreach (TableCell tableCell in wordTableHeader.ChildElements)
     {
         Shading shading = new Shading()
         {
             Color = "Automatic",
             Fill  = "C8C8C8", //Background color
             Val   = ShadingPatternValues.Clear
         };
         TableCellProperties tableCellProperties = new TableCellProperties();
         tableCellProperties.AppendChild(shading);
         tableCell.TableCellProperties = tableCellProperties;
     }
 }
예제 #9
0
        private static TableCell SetupCell(string text, bool center = false)
        {
            try
            {
                TableCell tableCell = new TableCell();

                TableCellProperties tableCellProperties = new TableCellProperties();

                // border & padding
                TableCellBorders borders = new TableCellBorders();

                TopBorder topBorder = new TopBorder {
                    Val = new EnumValue <BorderValues>(BorderValues.Thick), Color = "A1A2A3"
                };
                borders.AppendChild(topBorder);

                BottomBorder bottomBorder = new BottomBorder {
                    Val = new EnumValue <BorderValues>(BorderValues.Thick), Color = "A1A2A3"
                };
                borders.AppendChild(bottomBorder);

                RightBorder rightBorder = new RightBorder {
                    Val = new EnumValue <BorderValues>(BorderValues.Nil)
                };
                borders.AppendChild(rightBorder);

                LeftBorder leftBorder = new LeftBorder {
                    Val = new EnumValue <BorderValues>(BorderValues.Nil)
                };
                borders.AppendChild(leftBorder);

                TableCellMargin margin    = new TableCellMargin();
                TopMargin       topMargin = new TopMargin()
                {
                    Width = "40"
                };
                BottomMargin bottomMargin = new BottomMargin()
                {
                    Width = "40"
                };
                margin.AppendChild(topMargin);
                margin.AppendChild(bottomMargin);

                tableCellProperties.AppendChild(borders);
                tableCellProperties.AppendChild(margin);

                tableCell.AppendChild(tableCellProperties);

                // add text (with specific formatting)
                Paragraph paragraph = new Paragraph()
                {
                    RsidParagraphAddition = "00607D74", RsidRunAdditionDefault = "00607D74", ParagraphId = "6ED85602", TextId = "77777777"
                };

                ParagraphProperties        paragraphProperties        = new ParagraphProperties();
                ParagraphMarkRunProperties paragraphMarkRunProperties = new ParagraphMarkRunProperties();

                paragraphMarkRunProperties.AppendChild(new Color {
                    Val = "000000"
                });
                paragraphMarkRunProperties.AppendChild(new RunFonts {
                    Ascii = "Arial"
                });
                paragraphMarkRunProperties.AppendChild(new FontSize()
                {
                    Val = "7pt"
                });

                Justification justification = new Justification()
                {
                    Val = JustificationValues.Left
                };
                if (center)
                {
                    justification.Val = JustificationValues.Center;
                }

                paragraphProperties.AppendChild(paragraphMarkRunProperties);
                paragraphProperties.AppendChild(justification);
                paragraph.AppendChild(paragraphProperties);

                paragraph.AppendChild(new Text(text));

                // add to table cell
                tableCell.AppendChild(paragraph);

                return(tableCell);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
예제 #10
0
        private void AddSecondPageTable(Body body)
        {
            Table           personalInfoTable = new Table();
            TableProperties tableProps        = new TableProperties();

            AddTableBorders(tableProps);
            TableStyle tableStyle = new TableStyle()
            {
                Val = "TableGrid"
            };
            TableWidth tableWidth = new TableWidth()
            {
                Width = "5000", Type = TableWidthUnitValues.Pct
            };

            tableProps.Append(tableStyle, tableWidth);
            TableGrid tableGrid = new TableGrid();

            personalInfoTable.AppendChild(tableProps);
            for (int x = 0; x < 6; x++)
            {
                tableGrid.AppendChild(new GridColumn());
            }
            personalInfoTable.AppendChild(tableGrid);
            for (int x = 0; x < 7; x++)
            {
                TableRow         pesonalTableRow = new TableRow();
                List <TableCell> cells           = new List <TableCell>();
                for (int i = 0; i < 6; i++)
                {
                    TableCell           cell = new TableCell();
                    TableCellProperties tableCellProperties = new TableCellProperties();
                    tableCellProperties.TableCellVerticalAlignment     = new TableCellVerticalAlignment();
                    tableCellProperties.TableCellVerticalAlignment.Val = TableVerticalAlignmentValues.Center;
                    VerticalMerge   verticalMerge   = new VerticalMerge();
                    HorizontalMerge horizontalMerge = new HorizontalMerge();
                    if (x == 0)
                    {
                        horizontalMerge.Val = i == 0 ? MergedCellValues.Restart : MergedCellValues.Continue;
                        tableCellProperties.AppendChild(horizontalMerge);
                        if (i == 0)
                        {
                            SetCellText(cell, "Time Table", true);
                        }
                        else
                        {
                            SetCellText(cell, "", false);
                        }
                    }
                    else if (i == 0)
                    {
                        verticalMerge.Val = x == 1 ? MergedCellValues.Restart : MergedCellValues.Continue;
                        tableCellProperties.AppendChild(verticalMerge);
                        if (x == 1)
                        {
                            SetCellText(cell, "Hours", true);
                        }
                        else
                        {
                            SetCellText(cell, "", false);
                        }
                    }
                    else if (x == 1)
                    {
                        switch (i)
                        {
                        case 1:
                            SetCellText(cell, "Mon", true);
                            break;

                        case 2:
                            SetCellText(cell, "Tue", true);
                            break;

                        case 3:
                            SetCellText(cell, "Wed", true);
                            break;

                        case 4:
                            SetCellText(cell, "Thu", true);
                            break;

                        case 5:
                            SetCellText(cell, "Fri", true);
                            break;
                        }
                    }
                    else if (x == 4)
                    {
                        horizontalMerge.Val = i == 1 ? MergedCellValues.Restart : MergedCellValues.Continue;
                        tableCellProperties.AppendChild(horizontalMerge);
                        if (i == 1)
                        {
                            SetCellText(cell, "Lunch", true);
                        }
                        else
                        {
                            SetCellText(cell, "", false);
                        }
                    }
                    else if (x == 2 || x == 5)
                    {
                        switch (i)
                        {
                        case 1:
                        case 3:
                            SetCellText(cell, "Science", false);
                            break;

                        case 2:
                        case 4:
                            SetCellText(cell, "Maths", false);
                            break;
                        }
                    }
                    else if (x == 3 || x == 6)
                    {
                        switch (i)
                        {
                        case 1:
                        case 4:
                            SetCellText(cell, "Social", false);
                            break;

                        case 2:
                            SetCellText(cell, "History", false);
                            break;

                        case 3:
                            SetCellText(cell, "English", false);
                            break;
                        }
                    }
                    if (x == 2 && i == 5)
                    {
                        SetCellText(cell, "Arts", false);
                    }
                    else if (x == 3 && i == 5)
                    {
                        SetCellText(cell, "Sports", false);
                    }
                    else if (i == 5 && (x == 5 || x == 6))
                    {
                        if (x == 5)
                        {
                            verticalMerge.Val = MergedCellValues.Restart;
                            SetCellText(cell, "Project", false);
                        }
                        else
                        {
                            verticalMerge.Val = MergedCellValues.Continue;
                            SetCellText(cell, "", false);
                        }
                        tableCellProperties.AppendChild(verticalMerge);
                    }
                    cell.AppendChild(tableCellProperties);
                    cells.Add(cell);
                }
                pesonalTableRow.Append(cells);
                personalInfoTable.AppendChild(pesonalTableRow);
            }
            body.AppendChild(personalInfoTable);
        }
예제 #11
0
        /// <summary>
        /// Render a cell in a word document
        /// </summary>
        /// <param name="cell"></param>
        /// <param name="document"></param>
        /// <param name="parent"></param>
        /// <param name="context"></param>
        /// <param name="documentPart"></param>
        /// <param name="isInAlternateRow"></param>
        /// <param name="formatProvider"></param>
        /// <returns></returns>
        public static TableCell Render(this Cell cell,
                                       Models.Document document,
                                       OpenXmlElement parent,
                                       ContextModel context,
                                       OpenXmlPart documentPart,
                                       bool isInAlternateRow,
                                       IFormatProvider formatProvider)
        {
            context.ReplaceItem(cell, formatProvider);

            TableCell wordCell = new TableCell();

            TableCellProperties cellProp = new TableCellProperties();

            wordCell.AppendChild(cellProp);

            if (isInAlternateRow)
            {
                cell.ReplaceAlternateConfiguration();
            }

            cellProp.AddBorders(cell);
            cellProp.AddShading(cell);
            cellProp.AddMargin(cell);
            cellProp.AddJustifications(cell);

            if (cell.CellWidth != null)
            {
                cellProp.AppendChild(new TableCellWidth()
                {
                    Width = cell.CellWidth.Width, Type = cell.CellWidth.Type.ToOOxml()
                });
            }

            // manage cell column and row span
            if (cell.ColSpan > 1)
            {
                cellProp.AppendChild(new GridSpan()
                {
                    Val = cell.ColSpan
                });
            }
            if (cell.Fusion)
            {
                if (cell.FusionChild)
                {
                    cellProp.AppendChild(new VerticalMerge()
                    {
                        Val = MergedCellValues.Continue
                    });
                }
                else
                {
                    cellProp.AppendChild(new VerticalMerge()
                    {
                        Val = MergedCellValues.Restart
                    });
                }
            }

            if (!cell.Show)
            {
                return(wordCell);
            }

            if (cell.ChildElements.Any(x => x is Models.TemplateModel))
            {
                // Need to replace elements :
                for (int i = 0; i < cell.ChildElements.Count; i++)
                {
                    var e = cell.ChildElements[i];

                    if (e is TemplateModel)
                    {
                        var elements = (e as TemplateModel).ExtractTemplateItems(document);
                        if (i == cell.ChildElements.Count - 1)
                        {
                            cell.ChildElements.AddRange(elements);
                        }
                        else
                        {
                            cell.ChildElements.InsertRange(i + 1, elements);
                        }
                    }
                }
            }

            if (cell.ChildElements.Any(x => x is Models.Paragraph) || cell.ChildElements.Any(x => x is ForEach))
            {
                foreach (var element in cell.ChildElements)
                {
                    element.InheritFromParent(cell);
                    if (element is Models.Paragraph ||
                        element is ForEach)
                    {
                        element.Render(document, wordCell, context, documentPart, formatProvider);
                    }
                    else
                    {
                        var paragraph = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
                        if (cell.Justification.HasValue)
                        {
                            var ppr = new ParagraphProperties();
                            ppr.AppendChild(new Justification()
                            {
                                Val = cell.Justification.Value.ToOOxml()
                            });
                            paragraph.AppendChild(ppr);
                        }
                        wordCell.AppendChild(paragraph);
                        var r = new Run();
                        paragraph.AppendChild(r);
                        element.Render(document, r, context, documentPart, formatProvider);
                    }
                }
            }
            else
            {
                // fill cell content (need at least an empty paragraph)
                var paragraph = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
                if (cell.Justification.HasValue)
                {
                    var ppr = new ParagraphProperties();
                    ppr.AppendChild(new Justification()
                    {
                        Val = cell.Justification.Value.ToOOxml()
                    });
                    paragraph.AppendChild(ppr);
                }
                wordCell.AppendChild(paragraph);
                var r = new Run();
                paragraph.AppendChild(r);
                foreach (var element in cell.ChildElements)
                {
                    element.InheritFromParent(cell);
                    element.Render(document, r, context, documentPart, formatProvider);
                }
            }

            return(wordCell);
        }
예제 #12
0
        private static TableCell SetupHeaderCell(string text, string width, bool center = false)
        {
            try
            {
                TableCell tableCell = new TableCell();

                TableCellProperties tableCellProperties = new TableCellProperties();
                TableCellWidth      tableCellWidth      = new TableCellWidth()
                {
                    Width = width, Type = TableWidthUnitValues.Dxa
                };
                Shading shading = new Shading()
                {
                    Val = ShadingPatternValues.Clear, Fill = "FFFFFF", Color = "auto"
                };

                // border & padding
                TableCellBorders borders = new TableCellBorders();

                TopBorder topBorder = new TopBorder {
                    Val = new EnumValue <BorderValues>(BorderValues.Thick), Color = "000000"
                };
                borders.AppendChild(topBorder);

                BottomBorder bottomBorder = new BottomBorder {
                    Val = new EnumValue <BorderValues>(BorderValues.Thick), Color = "000000"
                };
                borders.AppendChild(bottomBorder);

                TableCellMargin margin    = new TableCellMargin();
                TopMargin       topMargin = new TopMargin()
                {
                    Width = "40"
                };
                BottomMargin bottomMargin = new BottomMargin()
                {
                    Width = "40"
                };
                margin.AppendChild(topMargin);
                margin.AppendChild(bottomMargin);

                tableCellProperties.AppendChild(tableCellWidth);
                tableCellProperties.AppendChild(shading);
                tableCellProperties.AppendChild(borders);
                tableCellProperties.AppendChild(margin);
                tableCellProperties.AppendChild(new TableCellVerticalAlignment()
                {
                    Val = TableVerticalAlignmentValues.Center
                });

                tableCell.AppendChild(tableCellProperties);

                // add text (with specific formatting)
                Paragraph paragraph = new Paragraph()
                {
                    RsidParagraphAddition = "00607D74", RsidRunAdditionDefault = "00607D74", ParagraphId = "6ED85602", TextId = "77777777"
                };

                ParagraphProperties        paragraphProperties        = new ParagraphProperties();
                ParagraphMarkRunProperties paragraphMarkRunProperties = new ParagraphMarkRunProperties();

                paragraphMarkRunProperties.AppendChild(new Color {
                    Val = "000000"
                });
                paragraphMarkRunProperties.AppendChild(new RunFonts {
                    Ascii = "Arial"
                });
                paragraphMarkRunProperties.AppendChild(new FontSize()
                {
                    Val = "8pt"
                });
                paragraphMarkRunProperties.AppendChild(new Bold());

                Justification justification = new Justification()
                {
                    Val = JustificationValues.Left
                };
                if (center)
                {
                    justification.Val = JustificationValues.Center;
                }

                paragraphProperties.AppendChild(paragraphMarkRunProperties);
                paragraphProperties.AppendChild(justification);
                paragraph.AppendChild(paragraphProperties);

                paragraph.AppendChild(new Text(text));

                // add to table cell
                tableCell.AppendChild(paragraph);

                return(tableCell);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }