1

我正在使用GemBox.Presentation并在我的 PPTX 文件中创建一个大表。与此示例类似,例如:

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");

正如预期的那样,表格不适合幻灯片:

带有不适合的大桌子的 PowerPoint 幻灯片

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

我怎样才能做到这一点?
我怎样才能知道新TableRow的是否会超过Slide高度?

4

1 回答 1

3

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

例如,请参阅以下内容:

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");
于 2020-06-22T06:04:26.097 回答