none
Add/Remove Columns in pptx using Open XML RRS feed

  • Question

  • Hi,
     
    I need help!
    I am trying to make a presentation in power point using open xml.
    I am using a template for a table there is one row with 5 columns in pptx. First I create a clone of this template slide and add some rows in it. I am able to add more rows in this table. also I am able to clear the contents of the cell, but have been unable to find documentation to delete the column as to shift the other cells left when the column is deleted. How may I delete the column using openxml?

    Please help me how I can add/remove columns in pptx file using openxml


     

    Thanks!
    Kuldeep Dwivedi
    Monday, January 30, 2012 1:22 PM

Answers

  • Hi Kuldeep,

     

    Thanks for posting in the MSDN Forum.

     

    This snippet will show you how to add or remove columns. This is only a simple sample for you. In the sample I add/remove columns from a presentation which only has one slide and one table in it.

     

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Drawing;
    using P = DocumentFormat.OpenXml.Presentation;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            private string Path { set; get; }
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                OpenFileDialog objOpenFileDialog = new OpenFileDialog();
                objOpenFileDialog.Filter = "PPT (*.pptx)|*.pptx";
                objOpenFileDialog.ShowDialog();
                Path = objOpenFileDialog.FileName;
                if (Path.Length > 0)
                {
                    button2.Enabled = true;
                    button3.Enabled = true;
                }
            }
    
            private Table GetTable(PresentationDocument Part)
            {
                PresentationPart objPresentationPart = Part.PresentationPart;
                SlidePart objSlidePart = objPresentationPart.SlideParts
                    .First<SlidePart>();
                P.Slide objSlide = objSlidePart.Slide;
                P.CommonSlideData objCommonSlideData = objSlide.CommonSlideData;
                P.ShapeTree objShapeTree = objCommonSlideData.ShapeTree;
                P.GraphicFrame objGraphicFrame = objShapeTree
                    .Descendants<P.GraphicFrame>().FirstOrDefault();
                Graphic objGraphic = objGraphicFrame.Graphic;
                GraphicData objGraphicData = objGraphic.GraphicData;
                Table table = objGraphicData.Descendants<Table>().FirstOrDefault();
                return table;
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                if (Path.Length > 0)
                {
                    using (PresentationDocument objPresentationDocument = 
                        PresentationDocument.Open(Path,true))
                    {
                        Table objTable = GetTable(objPresentationDocument);
                        TableGrid objTableGrid = objTable.TableGrid;
                        GridColumn objGridColumn = objTableGrid
                            .Descendants<GridColumn>().FirstOrDefault();
                        objGridColumn.Remove();
                        List<TableRow> Rows = objTable.Descendants<TableRow>()
                            .ToList();
                        for (int i = 0; i < Rows.Count; i++)
                        {
                            TableRow Row = Rows[i];
                            TableCell objTableCell = Row.Descendants<TableCell>()
                                .FirstOrDefault();
                            objTableCell.Remove();
                        }
                        objPresentationDocument.PresentationPart.SlideParts
                            .First<SlidePart>().Slide.Save();
                    }
                }
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                if (Path.Length > 0)
                {
                    using (PresentationDocument objPresentationDocument =
                        PresentationDocument.Open(Path, true))
                    {
                        Table objTable = GetTable(objPresentationDocument);
                        TableGrid objTableGrid = objTable.TableGrid;
                        GridColumn objGridColumn = new GridColumn()
                        { 
                            Width = 1234000L 
                        };
                        objTableGrid.Append(objGridColumn);
                        List<TableRow> Rows = objTable.Descendants<TableRow>()
                            .ToList();
                        for (int i = 0; i < Rows.Count; i++)
                        {
                            TableRow Row = Rows[i];
                            TableCell newCell = new TableCell();
                            TextBody objTextBody = new TextBody();
                            Paragraph objParagraph = new Paragraph();
                            TableCellProperties objTableCellProperties = 
                                new TableCellProperties();
                            BodyProperties objBodyProperties = new BodyProperties();
                            objTextBody.Append(objBodyProperties);
                            objTextBody.Append(objParagraph);
                            newCell.Append(objTextBody);
                            newCell.Append(objTableCellProperties);
                            Row.Append(newCell);
                        }
                        objPresentationDocument.PresentationPart.SlideParts
                            .First<SlidePart>().Slide.Save();
                    }
                }
            }
        }
    }
    

    I hope it can help you.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Wednesday, February 1, 2012 9:55 AM
    Moderator