【问题标题】:Split table to multiple slides in PowerPoint file将表格拆分为 PowerPoint 文件中的多张幻灯片
【发布时间】:2020-10-07 00:13:48
【问题描述】:

我正在使用GemBox.Presentation,并且正在我的 PPTX 文件中创建一个大表。类似于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");

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

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

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

【问题讨论】:

    标签: c# table-splitting gembox-presentation


    【解决方案1】:

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

    例如,请参阅以下内容:

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

    【讨论】:

      猜你喜欢
      • 2021-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-10
      相关资源
      最近更新 更多