Example #1
0
        /// <summary>
        /// Replaces the cells from the table (tbl).
        /// </summary>
        /// <returns>The list of remaining rows that could not be inserted, you will have to create a new slide.</returns>
        public List <Cell[]> SetRows(IList <Cell[]> rows)
        {
            PptxSlide slide = this.slideTemplate;

            A.Table tbl = slide.FindTable(this.tblId);

            int tblRowsCount = RowsCount(tbl);

            // done starts at 1 instead of 0 because we don't care about the first row
            // The first row contains the titles for the columns
            int done = 1;

            for (int i = 0; i < rows.Count(); i++)
            {
                Cell[] row = rows[i];

                if (done < tblRowsCount)
                {
                    // a:tr
                    A.TableRow tr = GetRow(tbl, done);

                    // a:tc
                    foreach (A.TableCell tc in tr.Descendants <A.TableCell>())
                    {
                        foreach (Cell cell in row)
                        {
                            ReplaceTag(slide, tc, cell);
                        }
                    }

                    done++;
                }
                else
                {
                    break;
                }
            }

            // Remove the last remaining rows if any
            for (int row = tblRowsCount - 1; row >= done; row--)
            {
                A.TableRow tr = GetRow(tbl, row);
                tr.Remove();
            }

            // Save the latest slide
            // Mandatory otherwise the next time SetRows() is run (on a different table)
            // the rows from the previous tables will not contained the right data (from PptxParagraph.ReplaceTag())
            slide.Save();

            // Computes the remaining rows if any
            List <Cell[]> remainingRows = new List <Cell[]>();

            for (int row = done - 1; row < rows.Count; row++)
            {
                remainingRows.Add(rows[row]);
            }

            return(remainingRows);
        }
Example #2
0
 private static void ModifyPowerPointRowTextContent(OXD.TableRow headerRowTemplate, string txt)
 {
     if (null != headerRowTemplate)
     {
         var cells = headerRowTemplate.Descendants <OXD.TableCell>();
         if (null != cells)
         {
             foreach (var cell in cells)
             {
                 ModifyPowerPointCellTextContent(cell, txt);
             }
         }
     }
 }
Example #3
0
        private static void AddNewGridColumn(OXD.TableGrid tableGrid, OXD.TableRow headerRow, OXD.TableRow contentRow)
        {
            var columns = tableGrid.Descendants <OXD.GridColumn>();

            if (null != columns && columns.Any())
            {
                var    headerLastCell  = headerRow.Descendants <OXD.TableCell>().Last();
                var    contentLastCell = contentRow.Descendants <OXD.TableCell>().Last();
                double tableWidth      = columns.Sum(_ => Convert.ToInt32(_.Width.Value));
                var    newColWidth     = Math.Floor(tableWidth / columns.Count());
                foreach (var col in columns)
                {
                    col.Width = col.Width > 0 ? Convert.ToInt64(Math.Floor((tableWidth - newColWidth) / (tableWidth / col.Width))) : 0;
                }
                tableGrid.InsertAfter <OXD.GridColumn>(new OXD.GridColumn()
                {
                    Width = Convert.ToInt64(newColWidth)
                }, columns.Last());
                headerRow.InsertAfter <OXD.TableCell>((OXD.TableCell)headerLastCell.CloneNode(true), headerLastCell);
                contentRow.InsertAfter <OXD.TableCell>((OXD.TableCell)contentLastCell.CloneNode(true), contentLastCell);
            }
        }
Example #4
0
 /// <summary>
 /// Helper method.
 /// </summary>
 private static A.TableCell GetCell(A.TableRow tr, int column)
 {
     A.TableCell tc = tr.Descendants <A.TableCell>().ElementAt(column);
     return(tc);
 }
Example #5
0
        private static void UpdatePowerPointBlock(ReportData client, OpenXmlPartContainer container, OpenXmlElement block, TableDefinition content, Dictionary <string, string> options)
        {
            if (null != content && block is OXP.GraphicFrame)
            {
                OXD.Table initTable = (OXD.Table)block.Descendants <OXD.Table>().FirstOrDefault();
                if (null != initTable)
                {
                    try
                    {
                        OXD.Table    table              = initTable.CloneNode(true) as OXD.Table;
                        OXD.TableRow headerRowTemplate  = table.Descendants <OXD.TableRow>().First().CloneNode(true) as OXD.TableRow;
                        OXD.TableRow contentRowTemplate = table.Descendants <OXD.TableRow>().Skip(1).First().CloneNode(true) as OXD.TableRow;

                        ModifyPowerPointRowTextContent(headerRowTemplate, string.Empty);
                        ModifyPowerPointRowTextContent(contentRowTemplate, string.Empty);

                        #region Column Number Management
                        List <OXD.GridColumn> columns = table.TableGrid.Descendants <OXD.GridColumn>().ToList();
                        if (columns.Count < content.NbColumns)
                        {
                            int nbNewColumn = content.NbColumns - columns.Count;
                            for (int i = 0, lim = nbNewColumn; i < lim; i++)
                            {
                                AddNewGridColumn(table.TableGrid, headerRowTemplate, contentRowTemplate);
                            }
                        }
                        else if (columns.Count > content.NbColumns)
                        {
                            for (int i = content.NbColumns, lim = columns.Count; i < lim; i++)
                            {
                                RemoveLastGridColumn(table.TableGrid);
                            }
                        }
                        #endregion Column Number Management

                        int idx   = 0;
                        int nbrow = 0;
                        List <OXD.TableCell> headerCells  = headerRowTemplate.Descendants <OXD.TableCell>().Select(_ => _.CloneNode(true) as OXD.TableCell).ToList();
                        List <OXD.TableCell> contentCells = contentRowTemplate.Descendants <OXD.TableCell>().Select(_ => _.CloneNode(true) as OXD.TableCell).ToList();
                        headerRowTemplate.RemoveAllChildren <OXD.TableCell>();
                        OXD.TableRow row = headerRowTemplate;

                        table.RemoveAllChildren <OXD.TableRow>();
                        foreach (var item in content.Data)
                        {
                            OXD.TableCell cell = null;
                            if (content.HasColumnHeaders && 0 == nbrow)
                            {
                                cell = headerCells[idx].CloneNode(true) as OXD.TableCell;
                            }
                            else
                            {
                                cell = contentCells[idx].CloneNode(true) as OXD.TableCell;
                            }
                            ModifyPowerPointCellTextContent(cell, item);

                            //row.Append(cell); => in office 2016, element <extLst> should absolutely be in the latest position in a row
                            row.InsertBefore <OXD.TableCell>(cell, row.Descendants <OXD.ExtensionList>().FirstOrDefault());

                            idx = ++idx % content.NbColumns;
                            if (0 == idx)
                            {
                                if (null != row)
                                {
                                    table.Append(row);
                                    nbrow++;
                                }
                                row = contentRowTemplate.CloneNode(true) as OXD.TableRow;
                                row.RemoveAllChildren <OXD.TableCell>();
                            }
                        }
                        initTable.Parent.ReplaceChild(table, initTable);
                    }
                    catch (Exception exception)
                    {
                        LogHelper.Instance.LogErrorFormat("An unhandled exception was thrown during table block content generation : '{0}'", exception.ToString());
                        if (null != initTable)
                        {
                            if (null != initTable.Descendants <OXD.TableRow>() && 1 > initTable.Descendants <OXD.TableRow>().Count())
                            {
                                foreach (var row in initTable.Descendants <OXD.TableRow>().Skip(1))
                                {
                                    ModifyPowerPointRowTextContent(row, string.Empty);
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                LogHelper.Instance.LogErrorFormat("Impossible to load data in table block with a block source of type \"{0}\"", null != block ? block.GetType().ToString() : "null");
            }
        }
Example #6
0
        private static void UpdatePowerPointBlock(ReportData client, OpenXmlPartContainer container, OpenXmlElement block, TableDefinition content, Dictionary <string, string> options)
        {
            if (null != content && block is OXP.GraphicFrame)
            {
                var       randomValue = new Random();
                OXD.Table initTable   = block.Descendants <OXD.Table>().FirstOrDefault();
                if (null == initTable)
                {
                    return;
                }
                try
                {
                    OXD.Table    table              = initTable.CloneNode(true) as OXD.Table;
                    OXD.TableRow headerRowTemplate  = table?.Descendants <OXD.TableRow>().First().CloneNode(true) as OXD.TableRow;
                    OXD.TableRow contentRowTemplate = table?.Descendants <OXD.TableRow>().Skip(1).First().CloneNode(true) as OXD.TableRow;

                    ModifyPowerPointRowTextContent(headerRowTemplate, string.Empty);
                    ModifyPowerPointRowTextContent(contentRowTemplate, string.Empty);

                    #region Column Number Management
                    List <OXD.GridColumn> columns = table?.TableGrid.Descendants <OXD.GridColumn>().ToList();
                    if (columns != null && columns.Count < content.NbColumns)
                    {
                        int nbNewColumn = content.NbColumns - columns.Count;
                        for (int i = 0, lim = nbNewColumn; i < lim; i++)
                        {
                            AddNewGridColumn(table.TableGrid, headerRowTemplate, contentRowTemplate);
                        }
                    }
                    else if (columns != null && columns.Count > content.NbColumns)
                    {
                        for (int i = content.NbColumns, lim = columns.Count; i < lim; i++)
                        {
                            RemoveLastGridColumn(table.TableGrid);
                        }
                    }
                    #endregion Column Number Management

                    int idx   = 0;
                    int nbrow = 0;
                    List <OXD.TableCell> headerCells  = headerRowTemplate?.Descendants <OXD.TableCell>().Select(_ => _.CloneNode(true) as OXD.TableCell).ToList();
                    List <OXD.TableCell> contentCells = contentRowTemplate?.Descendants <OXD.TableCell>().Select(_ => _.CloneNode(true) as OXD.TableCell).ToList();
                    headerRowTemplate?.RemoveAllChildren <OXD.TableCell>();
                    OXD.TableRow row = headerRowTemplate;

                    table?.RemoveAllChildren <OXD.TableRow>();
                    for (int i = 0; i < content.Data.Count(); i++)
                    {
                        string        item = content.Data.ToArray()[i];
                        OXD.TableCell cell;
                        if (content.HasColumnHeaders && 0 == nbrow)
                        {
                            cell = headerCells?[idx].CloneNode(true) as OXD.TableCell;
                        }
                        else
                        {
                            cell = contentCells?[idx].CloneNode(true) as OXD.TableCell;
                        }
                        ModifyPowerPointCellTextContent(cell, item);

                        if (content.HasCellsAttributes())
                        {
                            CellAttributes attributes = content.CellsAttributes.FirstOrDefault(a => a.Index == i);
                            if (attributes?.BackgroundColor != null)
                            {
                                Color myColor = attributes.BackgroundColor;
                                OXD.RgbColorModelHex backColor = new OXD.RgbColorModelHex()
                                {
                                    Val = $"{myColor.R:X2}{myColor.G:X2}{myColor.B:X2}"
                                };
                                OXD.SolidFill solidFill = new OXD.SolidFill();
                                solidFill.Append(backColor);
                                OXD.TableCellProperties props     = cell?.Descendants <OXD.TableCellProperties>().FirstOrDefault();
                                OXD.TableCellProperties new_props = (props != null) ? props.CloneNode(true) as OXD.TableCellProperties : new OXD.TableCellProperties();

                                OXD.SolidFill oldFill = new_props?.Descendants <OXD.SolidFill>().FirstOrDefault();
                                oldFill?.Remove();
                                new_props?.InsertAfter(solidFill, new_props.LastChild);
                                if (props != null)
                                {
                                    cell.ReplaceChild(new_props, props);
                                }
                                else
                                {
                                    cell?.AppendChild(new_props);
                                }
                            }
                        }

                        //row.Append(cell); => in office 2016, element <extLst> should absolutely be in the latest position in a row
                        row?.InsertBefore(cell, row.Descendants <OXD.ExtensionList>().FirstOrDefault());

                        OXD.ExtensionList init_extlst = row?.Descendants <OXD.ExtensionList>().FirstOrDefault();
                        if (init_extlst != null)
                        {
                            OXD.Extension         init_ext   = init_extlst.Descendants <OXD.Extension>().FirstOrDefault();
                            OpenXmlUnknownElement init_rowId = init_ext?.GetFirstChild <OpenXmlUnknownElement>();
                            OpenXmlUnknownElement new_rowId  = init_rowId?.CloneNode(true) as OpenXmlUnknownElement;
                            if (new_rowId != null)
                            {
                                OpenXmlAttribute val = new_rowId.GetAttributes().FirstOrDefault();
                                val.Value = randomValue.Next().ToString();
                                new_rowId.SetAttribute(val);
                                init_ext.ReplaceChild(new_rowId, init_rowId);
                            }
                        }

                        idx = ++idx % content.NbColumns;
                        if (0 != idx)
                        {
                            continue;
                        }
                        // ReSharper disable once ConditionIsAlwaysTrueOrFalse
                        if (null != row)
                        {
                            table.Append(row);
                            nbrow++;
                        }
                        row = contentRowTemplate?.CloneNode(true) as OXD.TableRow;
                        row?.RemoveAllChildren <OXD.TableCell>();
                    }
                    initTable.Parent.ReplaceChild(table, initTable);
                }
                catch (Exception exception)
                {
                    LogHelper.Instance.LogErrorFormat("An unhandled exception was thrown during table block content generation : '{0}'", exception.ToString());
                    if (initTable.Descendants <OXD.TableRow>() != null && !initTable.Descendants <OXD.TableRow>().Any())
                    {
                        foreach (var row in initTable.Descendants <OXD.TableRow>().Skip(1))
                        {
                            ModifyPowerPointRowTextContent(row, string.Empty);
                        }
                    }
                }
            }
            else
            {
                LogHelper.Instance.LogErrorFormat("Impossible to load data in table block with a block source of type \"{0}\"", block?.GetType().ToString() ?? "null");
            }
        }