Creating a Document with Alternating Full-Page Machine Part Images and Tables

I have a table with machine parts that spans several pages. I would like to generate a document that is supposed to printed and bound. It should show on the left side a full side image showing the machine parts and on the right side the table. So I need alternating on one page the image and on the next page the table.
I could use dividing the table into smaller parts and check if they fit on the page using the following method: Can you tell if a table will fit on one page?
But is there a more elegant solution?

Hi,

We are actually currently working on an API that will simplify this task. We plan to introduce support for retrieving the content of each individual page and the ability to process those content.

With this, you will be able to determine if the table occupies multiple pages and you’ll be able to insert new content between the pages. This feature should be available next month.

For now, the simplest way would be to actually use GemBox.Pdf.
So, you would first save that document with a large Table element into a PDF file using GemBox.Document.
Then you would load that PDF into PdfDocument using GemBox.Pdf, insert new PdfPage objects into the PdfDocument.Pages collection, and add desired images to those new pages.

I hope this helps.

Regards,
Mario

The new API sounds promising, thanks.

Hi,

Please try using this latest NuGet package:
Install-Package GemBox.Document -Version 35.0.1495-hotfix

We have added a new API to DocumentModelPage which can enable you to identify the page’s end.
For example, something like this:

static void Main()
{
    var document = new DocumentModel();

    // TODO: Create a table with machine parts.
    var table = new Table(document, 100, 2, (r, c) => new TableCell(document, new Paragraph(document, $"Cell ({r},{c})")));
    table.TableFormat.PreferredWidth = new TableWidth(100, TableWidthUnit.Percentage);
    table.TableFormat.DefaultCellPadding = new Padding(10);

    // Add table.
    var section = new Section(document, table);
    document.Sections.Add(section);

    // Set the AllowBreakAcrossPages to false so that it's easier to split the table.
    foreach (var row in table.Rows)
        row.RowFormat.AllowBreakAcrossPages = false;

    var paginator = document.GetPaginator(new PaginatorOptions() { UpdateTableColumnWidths = true, UpdateTableCellWidths = true });

    // Split the table into multiple tables, one for each page.
    foreach (var page in paginator.Pages.Reverse().SkipLast(1))
        SplitTable(page.Range.Start);

    // Insert images between tables.
    for (int i = 0; i < section.Blocks.Count; i+=2)
    {
        var paragraph = new Paragraph(document,
            new Picture(document, "machine-part.png"),
            new SpecialCharacter(document, SpecialCharacterType.PageBreak));
        paragraph.ParagraphFormat.PageBreakBefore = true;
        section.Blocks.Insert(i, paragraph);
    }

    document.Save("output.pdf");
}

static void SplitTable(IList<ContentPosition> positions)
{
    var row = (TableRow)positions.First().Parent.GetParentElements(ElementType.TableRow).First();
    var table = row.Parent;
    int tableIndex = table.ParentCollection.IndexOf(table);

    var cloneTable = table.Clone(false);
    table.ParentCollection.Insert(tableIndex + 1, cloneTable);

    int rowIndex = table.Rows.IndexOf(row);
    while (rowIndex < table.Rows.Count)
    {
        var cloneRow = table.Rows[rowIndex].Clone(true);
        cloneTable.Rows.Add(cloneRow);
        table.Rows.RemoveAt(rowIndex);
    }
}

Regards,
Mario