Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Sat Mar 08, 2025 4:33 pm

I am trying to create ppt from the span using spire in asp.net 4.8 project but the span is not detected
private int GetColSpan(Table table, int row, int col)
{
TableCell cell = table.Rows[row].Cells[col];
int colSpan = 1;

// Check subsequent columns in the same row
for (int c = col + 1; c < table.Rows[row].Cells.Count; c++)
{
TableCell nextCell = table.Rows[row].Cells[c];

// If the next cell is part of the same merged cell, it will be missing in the PPT table
if (nextCell.Paragraphs.Count == 0)
{
colSpan++;
}
else
{
break;
}
}
return colSpan;
}

private int GetRowSpan(Table table, int row, int col)
{
TableCell cell = table.Rows[row].Cells[col];
int rowSpan = 1;

for (int r = row + 1; r < table.Rows.Count; r++)
{
if (col >= table.Rows[r].Cells.Count) break; // Column index out of range

TableCell belowCell = table.Rows[r].Cells[col];

// If below cell is empty, it's merged
if (belowCell.Paragraphs.Count == 0)
{
rowSpan++;
}
else
{
break;
}
}
return rowSpan;
}






private ISlide AddTableToSlide(ISlide slide, Table table, ref float lastYPosition, float maxSlideHeight)
{
Presentation presentation = slide.Presentation;
float margin = 50f;
float tableTop = lastYPosition + 20f;
float availableHeight = maxSlideHeight - tableTop - 100;

int rows = table.Rows.Count;
int maxCols = table.Rows.Cast<Spire.Doc.TableRow>().Max(r => r.Cells.Count);

float tableWidth = presentation.SlideSize.Size.Width - (2 * margin);
float DefaultColWidth = tableWidth / maxCols;

double[] columnWidths = new double[maxCols];
for (int c = 0; c < maxCols; c++)
{
columnWidths[c] = DefaultColWidth;
}


// Approximate row heights (Ensure a minimum height)
double[] rowHeights = new double[rows];
for (int r = 0; r < rows; r++)
{
rowHeights[r] = Math.Max(table.Rows[r].Height, 30); // Ensure rows have a minimum height
}

if ((availableHeight - rowHeights[0]) <= 0)
{
slide = presentation.Slides.Append();
availableHeight = maxSlideHeight - 100;
}

int currentRow = 0;
while (currentRow < rows)
{
ISlide currentSlide = slide;

var tempAvailHeight = (double)availableHeight;
var tempRowHeights = new List<double>();
for (int i = currentRow; i < rows; i++)
{
if (rowHeights[i] < tempAvailHeight)
{
tempRowHeights.Add(rowHeights[i]);
tempAvailHeight -= rowHeights[i];
}
}


// Create a new table on the slide
ITable pptTable = currentSlide.Shapes.AppendTable(50, tableTop, columnWidths, tempRowHeights.ToArray());

Dictionary<(int, int), bool> processedCells = new Dictionary<(int, int), bool>();

int slideRowCount = 0;

// Ensure the header row is added first
if (table.Rows[0].Cells.Count > 0)
{
for (int col = 0; col < maxCols; col++)
{
TableCell headerCell = table.Rows[0].Cells[col];
pptTable[col, 0].TextFrame.Text = headerCell.Paragraphs.Count > 0 ? headerCell.Paragraphs[0].Text : "";
}
slideRowCount++;
currentRow++;
}

for (; currentRow < rows; currentRow++)
{
float rowHeight = (float)rowHeights[currentRow];

// If the current row cannot fit, move to a new slide
if (slideRowCount > 0 && rowHeight > availableHeight)
{
currentSlide = presentation.Slides.Append();
tableTop = 50f;
availableHeight = maxSlideHeight - 230;

var tempAvailHeightNewSlide = (double)availableHeight;
var tempRowHeightsNewSlide = new List<double>();
tempRowHeightsNewSlide.Add(rowHeights[0]);
for (int i = currentRow; i < rows; i++)
{
if (rowHeights[i] < tempAvailHeightNewSlide)
{
tempRowHeightsNewSlide.Add(rowHeights[i]);
tempAvailHeightNewSlide -= rowHeights[i];
}
}


// Create a new table on the new slide
pptTable = currentSlide.Shapes.AppendTable(50, tableTop, columnWidths, tempRowHeightsNewSlide.ToArray());
processedCells.Clear();
slideRowCount = 0;
// Repeat the header row
if (table.Rows[0].Cells.Count > 0)
{
for (int col = 0; col < maxCols; col++)
{
TableCell headerCell = table.Rows[0].Cells[col];
pptTable[col, 0].TextFrame.Text = headerCell.Paragraphs.Count > 0 ? headerCell.Paragraphs[0].Text : "";
}
slideRowCount++;
}
}

int colIndex = 0;
for (int col = 0; col < table.Rows[currentRow].Cells.Count; col++)
{
if (processedCells.ContainsKey((currentRow, colIndex)))
{
colIndex++;
continue;
}

TableCell wordCell = table.Rows[currentRow].Cells[col];
string cellText = wordCell.Paragraphs.Count > 0 ? wordCell.Paragraphs[0].Text : "";

int colSpan = GetColSpan(table, currentRow, col);
int rowSpan = GetRowSpan(table, currentRow, col);

pptTable[colIndex, slideRowCount].TextFrame.Text = cellText;

if (colSpan > 1 || rowSpan > 1)
{
int endCol = colIndex + colSpan - 1;
int endRow = slideRowCount + rowSpan - 1;

if (endCol < maxCols)
{
pptTable.MergeCells(pptTable[colIndex, slideRowCount], pptTable[endCol, endRow], false);
}
}

for (int i = 0; i < rowSpan; i++)
{
for (int j = 0; j < colSpan; j++)
{
processedCells[(currentRow + i, colIndex + j)] = true;
}
}

colIndex += colSpan;
}

slideRowCount++;
availableHeight -= rowHeight;
}

lastYPosition = tableTop + pptTable.Height + 20f;
}

return slide;
}
This is the code I am using Can you help me with this it is urgent to me

TechVardha
 
Posts: 1
Joined: Sat Mar 08, 2025 4:28 pm

Mon Mar 10, 2025 10:17 am

Hello,

Thanks for your inquiry.
I guess the reason for your problem is that you don't get the correct merge information of the cell. The following code snippet can get the number of columns and rows spanned by the cell. This should help you solve your problem.
Code: Select all
foreach (Table table in section.Tables)
{
    RowCollection rows = table.Rows;
    for (int i = 0; i < rows.Count; i++)
    {
        Spire.Doc.Collections.CellCollection cols = rows[i].Cells;
        for (int j = 0; j < cols.Count; j++)
        {
            TableCell cell = table[i, j];
            // Get colSpan
            int colSpan = cell.GridSpan;
           
            Console.WriteLine($"cell[{i},{j}]---colSpan: {colSpan}");
           
            // Get rowSpan
            if (cell.CellFormat.VerticalMerge == CellMerge.Start)
            {
                int num = 1;
                for (int k = i; k < rows.Count - 1; k++)
                {
                    if (table[k, j].CellFormat.VerticalMerge == CellMerge.Continue)
                    {
                        num++;
                    }
                }               
            Console.WriteLine($"cell[{i},{j}]---rowSpan: {num}");
               
            }
        }
    }
}

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 757
Joined: Mon Dec 27, 2021 2:23 am

Return to Spire.Doc