Table

Table (17)

Friday, 25 October 2019 09:26

Align Table in PowerPoint in C#

Written by support iceblue

Spire.Presentation supports setting alignment for table in a PowerPoint document. This article demonstrates how to align a table to the bottom of a PowerPoint slide using Spire.Presentation.

Below screenshot shows the original table before setting alignment:

Align Table in PowerPoint in C#

using Spire.Presentation;

namespace AlignTable
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load PowerPoint document
            Presentation ppt = new Presentation();
            ppt.LoadFromFile("Table.pptx");
            ITable table = null;
            //Loop through the shapes in the first slide
            foreach (IShape shape in ppt.Slides[0].Shapes)
            {
                //Find the table and align it to the bottom of the slide
                if (shape is ITable)
                {
                    table = (ITable)shape;
                    table.SetShapeAlignment(Spire.Presentation.ShapeAlignment.AlignBottom);
                }
            }

            //Save the resultant document
            ppt.SaveToFile("Result.pptx", FileFormat.Pptx2013);
        }
    }
}

Output:

Align Table in PowerPoint in C#

Wednesday, 26 September 2018 08:02

Split Table Cells in PowerPoint in C#

Written by support iceblue

This article is going to demonstrate how to split a specific table cell in PowerPoint using Spire.Presentation.

The original table:

Split Table Cells in PowerPoint in C#

Detail steps:

Step 1: Instantiate a Presentation object and load the PowerPoint file.

Presentation ppt = new Presentation();
ppt.LoadFromFile("Input.pptx");

Step 2: Get the first slide.

ISlide slide = ppt.Slides[0];

Step 3: Get the table on the slide.

ITable table = slide.Shapes[0] as ITable;

Step 4: Split the cell [1, 2] into 3 rows and 2 columns.

table[1, 2].Split(3, 2);

Step 5: Save the file.

ppt.SaveToFile("Split.pptx", FileFormat.Pptx2013);

Screenshot after splitting:

Split Table Cells in PowerPoint in C#

Full code:

using Spire.Presentation;

namespace Split_Table_Cells_in_PPT
{
    class Program
    {
        static void Main(string[] args)
        {
            //Instantiate a Presentation object
            Presentation ppt = new Presentation();
            //Load the PowerPoint file
            ppt.LoadFromFile("Input.pptx");

            //Get the first slide
            ISlide slide = ppt.Slides[0];

            //Get the table
            ITable table = slide.Shapes[0] as ITable;

            //Split cell [1, 2] into 3 rows and 2 columns
            table[1, 2].Split(3, 2);

            //Save the file
            ppt.SaveToFile("Split.pptx", FileFormat.Pptx2013);
        }
    }
}

Spire.Presentation supports to align text vertically in a table cell on the presentation slides. In this article, we will show you how to use C# to align the text in table cell and set the text direction. Firstly, view the screenshot on Microsoft PowerPoint of the vertical alignment and the text direction:

Vertically align the text in 

table cell

Detail steps:

Step 1: Create a new PowerPoint document and load the sample document from file.

Presentation ppt = new Presentation();
ppt.LoadFromFile("Sample1.pptx",FileFormat.Pptx2010);

Step 2: Get the table shape from the first presentation slide.

ITable table = null;
foreach (IShape shape in ppt.Slides[0].Shapes)
{
    if (shape is ITable)
    {
        table = (ITable)shape;

Step 3: Aligning the text vertically and set the text direction.

table[i, 0].TextAnchorType = TextAnchorType.Center;          
table[i, 0].VerticalTextType = VerticalTextType.Vertical270;

table[i, 1].TextAnchorType = TextAnchorType.Center;
table[i, 1].VerticalTextType = VerticalTextType.Horizontal;

table[i, 2].TextAnchorType = TextAnchorType.Center;
table[i, 2].VerticalTextType = VerticalTextType.Vertical;

Step 4: Save the document to file.

ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010);

Effective screenshot after set the vertical alignment and the text direction:

Vertically align the text in 

table cell

Full codes of vertical align the text and set the text direction.:

Presentation ppt = new Presentation();

ppt.LoadFromFile("Sample1.pptx",FileFormat.Pptx2010);

ITable table = null;
foreach (IShape shape in ppt.Slides[0].Shapes)
{
    if (shape is ITable)
    {
        table = (ITable)shape;

        for (int i = 0; i < table.ColumnsList.Count; i++)
        {
            table[i, 0].TextAnchorType = TextAnchorType.Center;                         
            table[i, 0].VerticalTextType = VerticalTextType.Vertical270;


            table[i, 1].TextAnchorType = TextAnchorType.Center;
            table[i, 1].VerticalTextType = VerticalTextType.Horizontal;

            table[i, 2].TextAnchorType = TextAnchorType.Center;
            table[i, 2].VerticalTextType = VerticalTextType.Vertical;
            
        }
    }
}

ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010);

With the help of Spire.Presentation, we can easily set the border type and color of a whole table on the presentation slides. This article will focus on demonstrating how to set the border for the table in C#.

Firstly, view the 12 border types for the table on PowerPoint file:

Set the border type and color for the table on Presentation slides

How to set the border type and color for an existing table on presentation slide:

//create a presentation document and load the file from disk
Presentation presentation = new Presentation();
presentation.LoadFromFile("Sample.pptx");

//get the table from the first slide of the sample document
ISlide slide = presentation.Slides[0];
ITable table = slide.Shapes[1] as ITable;
     
//set the border type as Inside and the border color as blue
table.SetTableBorder(TableBorderType.Inside, 1, Color.Blue);

//save the document to file
presentation.SaveToFile("Insideborder.pptx", FileFormat.Pptx2010);

Effective screenshot after set the border type for an existing table on presentation slide:

Set the border type and color for the table on Presentation slides

How to set the border type and color for newly added tables on presentation slide:

//create a presentation document 
Presentation presentation = new Presentation();

//set the table width and height for each table cell
double[] tableWidth = new double[] { 100, 100, 100, 100, 100 };
double[] tableHeight = new double[] { 20, 20 };

//traverse all the border type of the table
foreach (TableBorderType item in Enum.GetValues(typeof(TableBorderType)))

//add a table to the presentation slide with the setting width and height
{ ITable itable = presentation.Slides.Append().Shapes.AppendTable(100, 100, tableWidth, tableHeight); 

//add some text to the table cell
itable.TableRows[0][0].TextFrame.Text = "Row";
itable.TableRows[1][0].TextFrame.Text = "Column"; 

//set the border type, border width and the border color for the table
itable.SetTableBorder(item, 1.5, Color.Red); 

}

//save the document to file
presentation.SaveToFile("Addtablewithborder.pptx", FileFormat.Pptx2010);

Set the border type and color for the table on Presentation slides

This article demonstrates how to set the row height and column width of an existing table in PowerPoint document using Spire.Presentation in C# and VB.NET.

The following screenshot shows the table before setting row height and column width.

Set Table Row Height and Column Width in PowerPoint

Detail steps:

Step 1: Instantiate a Presentation object and load the PowerPoint document.

Presentation ppt = new Presentation();
ppt.LoadFromFile("Input.pptx");

Step 2: Get the first slide.

ISlide slide = ppt.Slides[0];

Step 3: Get the first table on the slide.

ITable table = ppt.Slides[0].Shapes[0] as ITable;

Step 4: Set table row height and column width.

table.TableRows[1].Height = 50;
table.ColumnsList[1].Width = 100;

Step 5: Save the document.

ppt.SaveToFile("Output.pptx", FileFormat.Pptx2013);

Screenshot:

Set Table Row Height and Column Width in PowerPoint

Full code:

[C#]
using Spire.Presentation;

namespace Set_table_column_width_and_row_height
{
    class Program
    {
        static void Main(string[] args)
        {
            Presentation ppt = new Presentation();
            ppt.LoadFromFile("Input.pptx");

            ISlide slide = ppt.Slides[0];            

            ITable table = ppt.Slides[0].Shapes[0] as ITable;

            table.TableRows[1].Height = 50;
            table.ColumnsList[1].Width = 100;

            ppt.SaveToFile("Output.pptx", FileFormat.Pptx2013);
        }
    }
}
[VB.NET]
Imports Spire.Presentation

Namespace Set_table_column_width_and_row_height
	Class Program
		Private Shared Sub Main(args As String())
			Dim ppt As New Presentation()
			ppt.LoadFromFile("Input.pptx")

			Dim slide As ISlide = ppt.Slides(0)

			Dim table As ITable = TryCast(ppt.Slides(0).Shapes(0), ITable)

			table.TableRows(1).Height = 50
			table.ColumnsList(1).Width = 100

			ppt.SaveToFile("Output.pptx", FileFormat.Pptx2013)
		End Sub
	End Class
End Namespace

With the help of Spire.Presentation, developers can set the horizontal alignment for the text on presentation slides easily. This article will focus on show you how to set the horizontal alignment for the text on the table of the presentation slides in C#. There are five options for the text alignment on the Spire.Presentation: Left, Right, Center, Justify and None.

At first, please check a document with default alignment (Left) prepared. Then, different alignment styles will be applied for the different rows on the table.

How to set vertical alignment for the text in table on presentation slides

Step 1: Create a presentation document and load the sample document from file.

Presentation presentation = new Presentation();
presentation.LoadFromFile("Sample.pptx",FileFormat.Pptx2010);

Step 2: Get the table from the sample document.

ITable table = null;

foreach (IShape shape in presentation.Slides[0].Shapes)
{
    if (shape is ITable)
    {
        table = (ITable)shape;

Step 3: Align text horizontally.

for (int i = 0; i < table.ColumnsList.Count; i++)
{
   
    table[i, 0].TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Right;
    table[i, 1].TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Center;
    table[i, 2].TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Left;
    table[i, 3].TextFrame.Paragraphs[0].Alignment = TextAlignmentType.None;
    table[i, 4].TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Justify;

}

Step 4: Save the document to file.

presentation.SaveToFile("tableresult.pptx", FileFormat.Pptx2010);

Effective screenshot after apply the horizontal alignment for the text on table:

How to set vertical alignment for the text in table on presentation slides

Full codes of how to set horizontal alignment for the text on the presentation slide's table:

static void Main(string[] args)
{
        
        Presentation presentation = new Presentation();
        presentation.LoadFromFile("Sample.pptx",FileFormat.Pptx2010);

        ITable table = null;

        foreach (IShape shape in presentation.Slides[0].Shapes)
        {
            if (shape is ITable)
            {
                table = (ITable)shape;

                for (int i = 0; i < table.ColumnsList.Count; i++)
                {
                   
                    table[i, 0].TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Right;
                    table[i, 1].TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Center;
                    table[i, 2].TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Left;
                    table[i, 3].TextFrame.Paragraphs[0].Alignment = TextAlignmentType.None;
                    table[i, 4].TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Justify;

                
                }
            }
        }
        presentation.SaveToFile("tableresult.pptx", FileFormat.Pptx2010);
    }

Borders can make the associative perception of a table stronger, but sometimes, especially in PowerPoint, you may want to remove them in order to achieve a better visual effect. In this part, we're going to introduce how to set and remove border of an existing table in PowerPoint by using Spire.Presentation for .NET.

Before start, please download and install Spire.Presentation correctly, after that add the Spire.Presentation.dll file as the reference of your project.

Detail steps overview:

Set table border:

Step 1: Create a new presentation instance and load the sample document from file.

Presentation presentation = new Presentation();
presentation.LoadFromFile("testTable.pptx");

Step 2: Find the table by looping through all the slides, and then set borders for it. Users can set different border styles according to their requirements.

foreach (ISlide slide in presentation.Slides)
{
    foreach (IShape shape in slide.Shapes)
    {
        if (shape is ITable)
        {
            foreach (TableRow row in (shape as ITable).TableRows)
            {
                foreach (Cell cell in row)
                {
                    cell.BorderTop.FillType = FillFormatType.Solid;
                    cell.BorderBottom.FillType = FillFormatType.Solid;
                    cell.BorderLeft.FillType = FillFormatType.Solid;
                    cell.BorderRight.FillType = FillFormatType.Solid;
                }
            }
        }
    }
}

Step 3: Save the file as Border.pptx.

presentation.SaveToFile("Border.pptx", FileFormat.Pptx2010);

Remove table border:

Remove table border is very simple, refer to the following codes:

cell.BorderTop.FillType = FillFormatType.None;
cell.BorderBottom.FillType = FillFormatType.None;
cell.BorderLeft.FillType = FillFormatType.None;
cell.BorderRight.FillType = FillFormatType.None;

Effective screenshots:

Before:

How to Set and Remove Table Border in PowerPoint

After:

How to Set and Remove Table Border in PowerPoint

Full codes:

using Spire.Presentation;
using Spire.Presentation.Drawing;

namespace Set_and_Remove_Table_Border_in_PPT
{
    class Program
    {
        static void Main(string[] args)
        {
            Presentation presentation = new Presentation();
            presentation.LoadFromFile("testTable.pptx");
            foreach (ISlide slide in presentation.Slides)
            {
                foreach (IShape shape in slide.Shapes)
                {
                    if (shape is ITable)
                    {
                        foreach (TableRow row in (shape as ITable).TableRows)
                        {
                            foreach (Cell cell in row)
                            {
                                //Add top border and set its FillType as Solid.
                                cell.BorderTop.FillType = FillFormatType.Solid;
                                //Remove top border
                                //cell.BorderTop.FillType = FillFormatType.None;

                                cell.BorderBottom.FillType = FillFormatType.Solid;
                                //cell.BorderBottom.FillType = FillFormatType.None;

                                cell.BorderLeft.FillType = FillFormatType.Solid;
                                //cell.BorderLeft.FillType = FillFormatType.None;

                                cell.BorderRight.FillType = FillFormatType.Solid;
                                //cell.BorderRight.FillType = FillFormatType.None;
                            }
                        }
                    }
                }
            }
            presentation.SaveToFile("Border.pptx", FileFormat.Pptx2010);
        }
    }
}

Adding an image in table cell can make your PPT files more different. Spire.Presentation allows developers add images to table cells. Here we introduce an example to add an image to a table cell in PPT.

Step 1: Create a new PPT document instance.

Presentation presentation = new Presentation();

Step 2: Call AppendTable() method to create a table and set the table style.

ISlide slide = presentation.Slides[0];
Double[] widths = new double[] { 100, 100};
Double[] heights = new double[] { 100, 100};
ITable table = presentation.Slides[0].Shapes.AppendTable(100, 80, widths, heights);
table.StylePreset = TableStylePreset.LightStyle1Accent2;

Step 3: Use table[0, 0].FillFormat.PictureFill.Picture.EmbedImage to embed the image to the cell. The FillType can be changed.

IImageData imgData = presentation.Images.Append(Image.FromFile("1.jpg"));
table[0, 0].FillFormat.FillType = FillFormatType.Picture;
table[0, 0].FillFormat.PictureFill.Picture.EmbedImage = imgData;
table[0, 0].FillFormat.PictureFill.FillType = PictureFillType.Stretch;

Step 4: Save and review.

presentation.SaveToFile("table.pptx", FileFormat.Pptx2010);
System.Diagnostics.Process.Start("table.pptx");

Here is the screenshot:

How to add an image in Table Cell via Spire.Presentation

Spire.Presentation is a powerful and standalone .NET component which designed for developers to operate the PowerPoint documents in C# and VB.NET. Spire.Presentation enable developers to insert a new table, remove rows or columns in an existing table, and remove the whole table from the presentation slides. This article we will show you how to add a row to an existing table in presentation slide by using C# code.

Step 1: Create Presentation instance and load file.

Presentation ppt = new Presentation();
ppt.LoadFromFile("table.pptx");

Step 2: Get the table within the PowerPoint document.

ITable table = ppt.Slides[0].Shapes[4] as ITable;

Step 3: Add a new row into the PowerPoint table and set the data for the cells in the new row.

//Get the first row
TableRow row = table.TableRows[0];
//Clone the row and add it to the end of table
table.TableRows.Append(row);
int rowCount = table.TableRows.Count;
//Get the last row
TableRow lastRow = table.TableRows[rowCount - 1];
//Set new data of the first cell of last row
lastRow[0].TextFrame.Text = " The first cell";
//Set new data of the second cell of last row
lastRow[1].TextFrame.Text = " The second cell";

Step 4: Save the document and preview it.

ppt.SaveToFile("result.pptx", FileFormat.Pptx2010);
System.Diagnostics.Process.Start("result.pptx");

Effective screenshot:

How to add a row to an existing table in PowerPoint documents

Full codes:

static void Main(string[] args)
{
    Presentation ppt = new Presentation();
    ppt.LoadFromFile("table.pptx");

    ITable table = ppt.Slides[0].Shapes[4] as ITable;
    TableRow row = table.TableRows[0];
    table.TableRows.Append(row);
    int rowCount = table.TableRows.Count;
    TableRow lastRow = table.TableRows[rowCount - 1];
    lastRow[0].TextFrame.Text = "The first cell";
    lastRow[1].TextFrame.Text = "The second cell";

    ppt.SaveToFile("result.pptx", FileFormat.Pptx2010);
    System.Diagnostics.Process.Start("result.pptx");

}

Built-in table styles are predefined formatting options that can be quickly applied to any table, greatly enhancing its appearance and readability. As same as MS PowerPoint, Spire.Presentation provides such a feature to give your table a professional look by applying a certain built-in table style. This article presents how this purpose can be achieved using Spire.Presentation with C#, VB.NET.

As we can see from below picture, plain tale is not that attractive before applying any style.

How to Apply Built-in Style to PowerPoint Table in C#, VB.NET

Code Snippet for Applying Table Style:

Step 1: Create an instance of presentation and load the test file.

Presentation ppt = new Presentation("test.pptx", FileFormat.Pptx2010);

Step 2: Get the table from presentation slide.

ITable table = ppt.Slides[0].Shapes[1] as ITable;

Step 3: Choose one table style from TableStylePreset and apply it to selected table.

table.StylePreset = TableStylePreset.MediumStyle2Accent2;

Step 4: Save the file.

ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010);

Output:

How to Apply Built-in Style to PowerPoint Table in C#, VB.NET

Full Code:

[C#]
Presentation ppt = new Presentation("test.pptx", FileFormat.Pptx2010);
ITable table = ppt.Slides[0].Shapes[1] as ITable;
table.StylePreset = TableStylePreset.MediumStyle2Accent2;
ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010);
[VB.NET]
Dim ppt As New Presentation("test.pptx", FileFormat.Pptx2010)
Dim table As ITable = TryCast(ppt.Slides(0).Shapes(1), ITable)
table.StylePreset = TableStylePreset.MediumStyle2Accent2
ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010)
Page 1 of 2