在 PowerPoint 文件中将 table 拆分为多张幻灯片

Split table to multiple slides in PowerPoint file

我正在使用 GemBox.Presentation and I'm creating a large table in my PPTX file. Similar to this example,例如:

PresentationDocument presentation = new PresentationDocument();
Slide slide = presentation.Slides.AddNew(SlideLayoutType.Custom);

int rowCount = 100;

int columnCount = 4;
int columnWidth = 5;

Table table = slide.Content.AddTable(1, 1, columnCount * columnWidth, 0, LengthUnit.Centimeter);

for (int i = 0; i < columnCount; i++)
    table.Columns.AddNew(Length.From(5, LengthUnit.Centimeter));

for (int r = 0; r < rowCount; r++)
{
    TableRow row = table.Rows.AddNew(0);
    for (int c = 0; c < columnCount; c++)
    {
        TableCell cell = row.Cells.AddNew();
        TextParagraph paragraph = cell.Text.AddParagraph();
        TextRun run = paragraph.AddRun(string.Format("Cell {0}-{1}", r + 1, c + 1));
    }
}

presentation.Save("output.pptx");

不出所料,table 不适合幻灯片:

所以我需要将此 table 拆分为多个 table 或多张幻灯片,以便每个 table 适合其幻灯片并且所有行都可见。

我该怎么做?
如何确定新的 TableRow 是否会超过 Slide 高度?

如果您有动态行高(例如,某些单元格可能包含多行文本),则您需要对内容进行分页。

例如,参见以下内容:

table.Frame.FormatDrawing(new PaginatorOptions() { UpdateTableRowHeights = true });

DrawingLayout tableLayout = table.Frame.Layout;
double maxHeight = presentation.SlideSize.Height - tableLayout.Top;
double currentHeight = 0;

TableRowCollection sourceRows = table.Rows;
TableRowCollection newRows = null;
int currentRowIndex = 0;

// Split the main table into multiple new tables based on the row heights.
while (currentRowIndex < sourceRows.Count)
{
    currentHeight += sourceRows[currentRowIndex].Height;

    // Create new slide with new table.
    if (currentHeight > maxHeight)
    {
        currentHeight = sourceRows[currentRowIndex].Height;

        Slide newSlide = presentation.Slides.AddNew(SlideLayoutType.Blank);
        Table newTable = newSlide.Content.AddTable(tableLayout.Left, tableLayout.Top, tableLayout.Width, 0);

        foreach (var column in table.Columns)
            newTable.Columns.AddClone(column);
        
        newRows = newTable.Rows;
    }

    // Move row from the main table to a new table.
    if (newRows != null)
    {
        newRows.AddClone(sourceRows[currentRowIndex]);
        sourceRows.RemoveAt(currentRowIndex);
    }
    else
    {
        ++currentRowIndex;
    }
}

presentation.Save("output.pptx");

如果您有恒定的行高,如您的屏幕截图所示,那么您可以简化它。

例如像下面这样:

int rowsPerSlide = 16;

TableRowCollection rows = table.Rows;
DrawingLayout layout = table.Frame.Layout;

for (int t = 1, tablesCount = (int)Math.Ceiling(rows.Count / (double)rowsPerSlide); t < tablesCount; t++)
{
    Slide newSlide = presentation.Slides.AddNew(SlideLayoutType.Blank);
    Table newTable = newSlide.Content.AddTable(layout.Left, layout.Top, layout.Width, 0);

    foreach (var column in table.Columns)
        newTable.Columns.AddClone(column);

    for (int r = rowsPerSlide, rowsCount = Math.Min(rowsPerSlide * 2, rows.Count); r < rowsCount; r++)
    {
        newTable.Rows.AddClone(rows[rowsPerSlide]);
        rows.RemoveAt(rowsPerSlide);
    }
}

presentation.Save("output.pptx");