Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Tue Nov 17, 2020 6:37 am

Hi,
I have a Table binding in PPT page2.
I want to continue in page 3 if table rows are exceeding page 2.
Thanks in advance for your support.
Here is my code.

DataTable dt = new DataTable();
dt.Columns.Add();
dt.Columns.Add();
dt.Columns.Add();
dt.Columns.Add();

dt.Rows.Add("Heading 1", "Heading 2", "Heading 3", "Heading 4";
for (int i = 0; i < list.Count; i++)
{
list[i].WP = list[i].WP.Replace(Environment.NewLine, String.Empty).Replace(" ", String.Empty);
list[i].ED = list[i].ED.Replace(Environment.NewLine, String.Empty).Replace(" ", String.Empty);
list[i].EE = list[i].EE.Replace(Environment.NewLine, String.Empty).Replace(" ", String.Empty);

dt.Rows.Add(list[i].WP, list[i].ED, list[i].EE, "");
}

//set table widths and heights
double[] widths = new double[dt.Columns.Count];
double[] heights = new double[dt.Rows.Count];
for (int i = 0; i < widths.Length; i++)
{
if (i == 0)
{
widths[i] = 70;
}
else if (i == 1)
{
widths[i] = 300;
}
else if (i == 2)
{
widths[i] = 300;
}
else{
widths[i] = 50;
}
//widths[i] = 100;
}
for (int i = 0; i < heights.Length; i++)
{
heights[i] = 15;
}

//Adding created table to ppt
ITable table = ppt.Slides[1].Shapes.AppendTable(ppt.SlideSize.Size.Width /2 - 370, 155, widths, heights);

//set the style of table
table.StylePreset = TableStylePreset.LightStyle1Accent2;

for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
//Fil the table with data
table[j,i].TextFrame.Text = dt.Rows[i][j].ToString();
//Set the font
table[j,i].TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont("Arial");
table[j,i].TextFrame.Paragraphs[0].TextRanges[0].FontHeight = 10f;

table[j, i].TextFrame.Paragraphs[0].TextRanges[0].Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
table[j, i].TextFrame.Paragraphs[0].TextRanges[0].Fill.SolidColor.Color = Color.FromArgb(255,0,0);
}
}
//Set the alignment of the first row to Center
for (int i = 0; i < dt.Columns.Count; i++)
{
table[i,0].TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Center;
}

saleembaigmirza
 
Posts: 21
Joined: Fri Nov 13, 2020 4:15 pm

Tue Nov 17, 2020 8:09 am

Hello,

Thanks for your inquiry.
Please refer to the following code, and I also attached my input file and output file for your reference. If there are any questions, please provide your desired output for further investigation.
Code: Select all
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add();
            dt.Columns.Add();
            dt.Columns.Add();
            dt.Columns.Add();

            dt.Rows.Add("Heading 1", "Heading 2", "Heading 3", "Heading 4");
            for (int i = 0; i < 40; i++)
            {
                dt.Rows.Add(i, i, i, i);
            }

            Presentation ppt = new Presentation();
            ppt.LoadFromFile("test.pptx");
            //set the row height
            float rowHeight = 15;
            //set the table position
            float x = ppt.SlideSize.Size.Width / 2 - 370;
            float y = 155;

            //calculate the maximum height of the table
            float maxTableHeight = ppt.SlideSize.Size.Height - y;
            //calculate the maximum row count of the second slide
            int rowCount = (int)(maxTableHeight / rowHeight);

            //set the table width for each column
            double[] widths = new double[dt.Columns.Count];
            for (int i = 0; i < widths.Length; i++)
            {
                widths[i] = 100;
            }

            //if table rows don't exceed the second slide
            if (rowCount > dt.Rows.Count)
            {
                double[] heights = new double[dt.Rows.Count];
                for (int i = 0; i < heights.Length; i++)
                {
                    heights[i] = 15;
                }
                //add the table to the second slide
                CreatTable(ppt.Slides[1], widths,  heights, dt, x, y, 0, dt.Rows.Count);
            }
            //if table rows exceed the second slide
            else
            {
                //first table
                double[] heights1 = new double[rowCount];
                for (int i = 0; i < heights1.Length; i++)
                {
                    heights1[i] = 15;
                }
                CreatTable(ppt.Slides[1], widths, heights1, dt, x, y, 0, rowCount);
                //second table
                double[] heights2 = new double[dt.Rows.Count - rowCount];
                for (int i = 0; i < heights2.Length; i++)
                {
                    heights2[i] = 15;
                }         
                CreatTable(ppt.Slides[2], widths, heights2, dt, x, y, rowCount, dt.Rows.Count);
            }
           

            ppt.SaveToFile("result.pptx", FileFormat.Pptx2007);
        }

        public static void CreatTable(ISlide slide, double[] widths, double[] heights, DataTable dt, float x, float y, int startRow, int endRow)
        {
            ITable table = slide.Shapes.AppendTable(x, y, widths, heights);
            table.StylePreset = TableStylePreset.LightStyle1Accent2;
            for (int i = 0; i < endRow - startRow; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    table[j, i].TextFrame.Text = dt.Rows[i + startRow][j].ToString();
                    //Set the margins to 0
                    table[j, i].MarginTop = 0;
                    table[j, i].MarginBottom = 0;       
                    table[j, i].TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont("Arial");
                    table[j, i].TextFrame.Paragraphs[0].TextRanges[0].FontHeight = 10f;
                    table[j, i].TextFrame.Paragraphs[0].TextRanges[0].Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
                    table[j, i].TextFrame.Paragraphs[0].TextRanges[0].Fill.SolidColor.Color = Color.FromArgb(255, 0, 0);
                }
            }
        }


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Fri Nov 20, 2020 3:05 am

Hello,

Greetings from E-iceblue!
Have you tested my code? Looking forward to your feedback!

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Tue Mar 16, 2021 10:14 am

Hi Rachel,
Thanks for your support.

How can we bind the Heading1,2,3 & 4 in table of next slide as well.

saleembaigmirza
 
Posts: 21
Joined: Fri Nov 13, 2020 4:15 pm

Wed Mar 17, 2021 5:53 am

Hello,

Thanks for your response.

Please refer to the following modified code. Feel free to contact us if you have further questions.
Code: Select all
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add();
            dt.Columns.Add();
            dt.Columns.Add();
            dt.Columns.Add();

            dt.Rows.Add("Heading 1", "Heading 2", "Heading 3", "Heading 4");
            for (int i = 0; i < 40; i++)
            {
                dt.Rows.Add(i, i, i, i);
            }

            Presentation ppt = new Presentation();
            ppt.LoadFromFile("test.pptx");
            //set the row height
            float rowHeight = 15;
            //set the table position
            float x = ppt.SlideSize.Size.Width / 2 - 370;
            float y = 155;

            //calculate the maximum height of the table
            float maxTableHeight = ppt.SlideSize.Size.Height - y;
            //calculate the maximum row count of the second slide
            int rowCount = (int)(maxTableHeight / rowHeight);

            //set the table width for each column
            double[] widths = new double[dt.Columns.Count];
            for (int i = 0; i < widths.Length; i++)
            {
                widths[i] = 100;
            }

            //if table rows don't exceed the second slide
            if (rowCount > dt.Rows.Count)
            {
                double[] heights = new double[dt.Rows.Count];
                for (int i = 0; i < heights.Length; i++)
                {
                    heights[i] = 15;
                }
                //add the table to the second slide
                CreatTable(ppt.Slides[1], widths, heights, dt, x, y, 0, dt.Rows.Count);
            }
            //if table rows exceed the second slide
            else
            {
                //first table
                double[] heights1 = new double[rowCount];
                for (int i = 0; i < heights1.Length; i++)
                {
                    heights1[i] = 15;
                }
                CreatTable(ppt.Slides[1], widths, heights1, dt, x, y, 0, rowCount);
                //second table
                double[] heights2 = new double[dt.Rows.Count - rowCount + 1];
                for (int i = 0; i < heights2.Length; i++)
                {
                    heights2[i] = 15;
                }
                CreatTable(ppt.Slides[2], widths, heights2, dt, x, y, rowCount, dt.Rows.Count + 1);
            }


            ppt.SaveToFile("result.pptx", FileFormat.Pptx2007);
        }

        public static void CreatTable(ISlide slide, double[] widths, double[] heights, DataTable dt, float x, float y, int startRow, int endRow)
        {
            ITable table = slide.Shapes.AppendTable(x, y, widths, heights);
            table.StylePreset = TableStylePreset.LightStyle1Accent2;
            for (int i = 0; i < endRow - startRow; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    if (startRow != 0)
                    {
                        if (i == 0)
                        {
                            table[j, i].TextFrame.Text = dt.Rows[0][j].ToString();
                        }
                        else
                        {
                            table[j, i].TextFrame.Text = dt.Rows[i + startRow - 1][j].ToString();
                        }
                    }
                    else
                    {
                        table[j, i].TextFrame.Text = dt.Rows[i + startRow][j].ToString();
                    }
                    //Set the margins to 0
                    table[j, i].MarginTop = 0;
                    table[j, i].MarginBottom = 0;
                    table[j, i].TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont("Arial");
                    table[j, i].TextFrame.Paragraphs[0].TextRanges[0].FontHeight = 10f;
                    table[j, i].TextFrame.Paragraphs[0].TextRanges[0].Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
                    table[j, i].TextFrame.Paragraphs[0].TextRanges[0].Fill.SolidColor.Color = Color.FromArgb(255, 0, 0);
                }
            }
        }

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Mon Mar 22, 2021 5:27 am

Hi Rachel,
Thanks for your quick response.

With the shared code by you, last record is not binding.
For Eg: i have 14 records to bind.
In Second slide, 8 records are binding along with headings(in 1st row). So total 9 rows in second slide.
Now in Third slide, only 5 records are binding along with heading (in 1st row). So total 6 rows in third slide. But we need 7 rows (1 heading + 6 remaining row records.)

Please let me know, whether i'm doing any mistake.

saleembaigmirza
 
Posts: 21
Joined: Fri Nov 13, 2020 4:15 pm

Mon Mar 22, 2021 5:33 am

rachel.lei wrote:Hello,

Thanks for your inquiry.
Please refer to the following code, and I also attached my input file and output file for your reference. If there are any questions, please provide your desired output for further investigation.
Code: Select all
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add();
            dt.Columns.Add();
            dt.Columns.Add();
            dt.Columns.Add();

            dt.Rows.Add("Heading 1", "Heading 2", "Heading 3", "Heading 4");
            for (int i = 0; i < 40; i++)
            {
                dt.Rows.Add(i, i, i, i);
            }

            Presentation ppt = new Presentation();
            ppt.LoadFromFile("test.pptx");
            //set the row height
            float rowHeight = 15;
            //set the table position
            float x = ppt.SlideSize.Size.Width / 2 - 370;
            float y = 155;

            //calculate the maximum height of the table
            float maxTableHeight = ppt.SlideSize.Size.Height - y;
            //calculate the maximum row count of the second slide
            int rowCount = (int)(maxTableHeight / rowHeight);

            //set the table width for each column
            double[] widths = new double[dt.Columns.Count];
            for (int i = 0; i < widths.Length; i++)
            {
                widths[i] = 100;
            }

            //if table rows don't exceed the second slide
            if (rowCount > dt.Rows.Count)
            {
                double[] heights = new double[dt.Rows.Count];
                for (int i = 0; i < heights.Length; i++)
                {
                    heights[i] = 15;
                }
                //add the table to the second slide
                CreatTable(ppt.Slides[1], widths,  heights, dt, x, y, 0, dt.Rows.Count);
            }
            //if table rows exceed the second slide
            else
            {
                //first table
                double[] heights1 = new double[rowCount];
                for (int i = 0; i < heights1.Length; i++)
                {
                    heights1[i] = 15;
                }
                CreatTable(ppt.Slides[1], widths, heights1, dt, x, y, 0, rowCount);
                //second table
                double[] heights2 = new double[dt.Rows.Count - rowCount];
                for (int i = 0; i < heights2.Length; i++)
                {
                    heights2[i] = 15;
                }         
                CreatTable(ppt.Slides[2], widths, heights2, dt, x, y, rowCount, dt.Rows.Count);
            }
           

            ppt.SaveToFile("result.pptx", FileFormat.Pptx2007);
        }

        public static void CreatTable(ISlide slide, double[] widths, double[] heights, DataTable dt, float x, float y, int startRow, int endRow)
        {
            ITable table = slide.Shapes.AppendTable(x, y, widths, heights);
            table.StylePreset = TableStylePreset.LightStyle1Accent2;
            for (int i = 0; i < endRow - startRow; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    table[j, i].TextFrame.Text = dt.Rows[i + startRow][j].ToString();
                    //Set the margins to 0
                    table[j, i].MarginTop = 0;
                    table[j, i].MarginBottom = 0;       
                    table[j, i].TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont("Arial");
                    table[j, i].TextFrame.Paragraphs[0].TextRanges[0].FontHeight = 10f;
                    table[j, i].TextFrame.Paragraphs[0].TextRanges[0].Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
                    table[j, i].TextFrame.Paragraphs[0].TextRanges[0].Fill.SolidColor.Color = Color.FromArgb(255, 0, 0);
                }
            }
        }


Sincerely,
Rachel
E-iceblue support team




Hi Rachel,

With this code, rows are fixed in slide. (Eg: say 24 rows in this case)
Is there any way i can bind the table based on 2nd column row text length?
Please let me know for any clarifications.

saleembaigmirza
 
Posts: 21
Joined: Fri Nov 13, 2020 4:15 pm

Mon Mar 22, 2021 9:22 am

Hello,

Thanks for your feedback.
For issue 1, I modified the code (as shown below) so that there are only 14 records, and then tested but did not observe the behavior you mentioned. Attached is my output file. To help us better reproduce your issue and solve it, please provide your full test code (including your datatable data). Thanks in advance.
Code: Select all
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add();
            dt.Columns.Add();
            dt.Columns.Add();
            dt.Columns.Add();

            dt.Rows.Add("Heading 1", "Heading 2", "Heading 3", "Heading 4");
            for (int i = 0; i < 14; i++)
            {
                dt.Rows.Add(i, i, i, i);
            }

            Presentation ppt = new Presentation();
            ppt.LoadFromFile("test.pptx");
            //set the row height
            float rowHeight = 40;
            //set the table position
            float x = ppt.SlideSize.Size.Width / 2 - 370;
            float y = 155;

            //calculate the maximum height of the table
            float maxTableHeight = ppt.SlideSize.Size.Height - y;
            //calculate the maximum row count of the second slide
            int rowCount = (int)(maxTableHeight / rowHeight);

            //set the table width for each column
            double[] widths = new double[dt.Columns.Count];
            for (int i = 0; i < widths.Length; i++)
            {
                widths[i] = 100;
            }

            //if table rows don't exceed the second slide
            if (rowCount > dt.Rows.Count)
            {
                double[] heights = new double[dt.Rows.Count];
                for (int i = 0; i < heights.Length; i++)
                {
                    heights[i] = 40;
                }
                //add the table to the second slide
                CreatTable(ppt.Slides[1], widths, heights, dt, x, y, 0, dt.Rows.Count);
            }
            //if table rows exceed the second slide
            else
            {
                //first table
                double[] heights1 = new double[rowCount];
                for (int i = 0; i < heights1.Length; i++)
                {
                    heights1[i] = 40;
                }
                CreatTable(ppt.Slides[1], widths, heights1, dt, x, y, 0, rowCount);
                //second table
                double[] heights2 = new double[dt.Rows.Count - rowCount + 1];
                for (int i = 0; i < heights2.Length; i++)
                {
                    heights2[i] = 40;
                }
                CreatTable(ppt.Slides[2], widths, heights2, dt, x, y, rowCount, dt.Rows.Count + 1);
            }


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

        public static void CreatTable(ISlide slide, double[] widths, double[] heights, DataTable dt, float x, float y, int startRow, int endRow)
        {
            ITable table = slide.Shapes.AppendTable(x, y, widths, heights);
            table.StylePreset = TableStylePreset.LightStyle1Accent2;
            for (int i = 0; i < endRow - startRow; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    if (startRow != 0)
                    {
                        if (i == 0)
                        {
                            table[j, i].TextFrame.Text = dt.Rows[0][j].ToString();
                        }
                        else
                        {
                            table[j, i].TextFrame.Text = dt.Rows[i + startRow - 1][j].ToString();
                        }
                    }
                    else
                    {
                        table[j, i].TextFrame.Text = dt.Rows[i + startRow][j].ToString();
                    }
                    //Set the margins to 0
                    table[j, i].MarginTop = 0;
                    table[j, i].MarginBottom = 0;
                    table[j, i].TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont("Arial");
                    table[j, i].TextFrame.Paragraphs[0].TextRanges[0].FontHeight = 10f;
                    table[j, i].TextFrame.Paragraphs[0].TextRanges[0].Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
                    table[j, i].TextFrame.Paragraphs[0].TextRanges[0].Fill.SolidColor.Color = Color.FromArgb(255, 0, 0);
                }
            }
        }


For issue 2, please refer to the following code.
Code: Select all
        static float tableMarginL = 10;
        static float tableMarginR = 10;
        static int fontSize = 10;
        static double lineSpacing = 150;
        public static void Run()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add();
            dt.Columns.Add();
            dt.Columns.Add();
            dt.Columns.Add();
            int dtRowCount = 20;
            dt.Rows.Add("Heading 1", "Heading 2", "Heading 3", "Heading 4");
            for (int i = 0; i < dtRowCount; i++)
            {
                dt.Rows.Add(i, "second colunm text", i, i);
            }

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

            //set the table position
            float x = ppt.SlideSize.Size.Width / 2 - 370;
            float y = 155;

            //calculate the maximum height of the table
            float maxTableHeight = ppt.SlideSize.Size.Height - y;

            //set the table width for each column
            double[] widths = new double[dt.Columns.Count];
            for (int i = 0; i < widths.Length; i++)
            {
                widths[i] = 100;
            }
            double[] heights = new double[dt.Rows.Count];

            for (int i = 0; i < heights.Length; i++)
            {
                double columnWidth = 100;
                System.Drawing.Font font = new System.Drawing.Font("Arial", fontSize);
                //!!!Note that this unit is pixel. The defualt uint in our Spire.Presentation is point.
                SizeF size = System.Windows.Forms.TextRenderer.MeasureText(dt.Rows[i][1].ToString(), font);
                float width = (size.Width / 96) * 72 + tableMarginL + tableMarginR;
                int lineCount = (int)Math.Ceiling(width / columnWidth);//Calculate how many rows there are based on the column width
                double rowHeight = lineCount * fontSize * 1.2f * lineSpacing / 100;//Calculate the row height
                heights[i] = rowHeight;
            }

            int endIndex = 0;
            double currentHeight = heights[endIndex];
            while (currentHeight < maxTableHeight && endIndex < dt.Rows.Count - 1)
            {
                currentHeight += heights[++endIndex];
            }
            if (endIndex == dtRowCount)
            {
                CreatTable(ppt.Slides[1], widths, heights, dt, x, y, 0, dt.Rows.Count);
            }
            else
            {
                double[] heights1 = new double[endIndex];
                for (int i = 0; i < heights1.Length; i++)
                {
                    heights1[i] = heights[i];
                }
                CreatTable(ppt.Slides[1], widths, heights1, dt, x, y, 0, endIndex);
                //second table
                double[] heights2 = new double[dt.Rows.Count - endIndex + 1];
                for (int i = 0; i < heights2.Length; i++)
                {
                    if (i == 0)
                    {
                        heights2[i] = heights[i];
                    }
                    else
                    {
                        heights2[i] = heights[i + endIndex - 1];
                    }
                }
                CreatTable(ppt.Slides[2], widths, heights2, dt, x, y, endIndex, dt.Rows.Count + 1);
            }

            ppt.SaveToFile("result.pptx", FileFormat.Pptx2007);
            System.Diagnostics.Process.Start("result.pptx");
        }
        public static void CreatTable(ISlide slide, double[] widths, double[] heights, DataTable dt, float x, float y, int startRow, int endRow)
        {
            ITable table = slide.Shapes.AppendTable(x, y, widths, heights);
            table.StylePreset = TableStylePreset.LightStyle1Accent2;
            for (int i = 0; i < endRow - startRow; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    if (startRow != 0)
                    {
                        if (i == 0)
                        {
                            table[j, i].TextFrame.Text = dt.Rows[0][j].ToString();
                        }
                        else
                        {
                            table[j, i].TextFrame.Text = dt.Rows[i + startRow - 1][j].ToString();
                        }
                    }
                    else
                    {
                        table[j, i].TextFrame.Text = dt.Rows[i + startRow][j].ToString();
                    }

                    //Set the margins to 0
                    table[j, i].MarginTop = 0;
                    table[j, i].MarginBottom = 0;
                    table[j, i].MarginLeft = tableMarginL;
                    table[j, i].MarginRight = tableMarginR;
                    table[j, i].TextFrame.Paragraphs[0].LineSpacing = 150;
                    table[j, i].TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont("Arial");
                    table[j, i].TextFrame.Paragraphs[0].TextRanges[0].FontHeight = 10f;
                    table[j, i].TextFrame.Paragraphs[0].TextRanges[0].Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
                    table[j, i].TextFrame.Paragraphs[0].TextRanges[0].Fill.SolidColor.Color = Color.FromArgb(255, 0, 0);
                }
            }
        }
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Thu Apr 01, 2021 7:17 am

Hello,

Greetings from E-iceblue!
Has your issue been resolved? Looking forward to your feedback!

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.PDF