Aspose.pdf Repeat Table Headers

Hi,

We are trying to repeat table headers across multiple pages while generating a pdf document. This feature is available in aspose.word “builder.getRowFormat().setHeadingFormat”. Is there something similar in aspose.pdf. Please provide some solution for implementing this feature.

@adevabhaktuni

Here is a code example that you can use as a start. The documentation will help you with much more things you can do.

public void MainCode()
{
    PartialPath = $@"{prefixPath}\Tables\TableWithHeaderInAllPages"; // Location of your input/output files

    LoadLicence(); // Load your License

    var doc = DocumentCreate(); // Read your document

    Logic(doc);

    doc.Save($"{PartialPath}_output.pdf");
}

private void Logic(Document doc)
{
    // Portrait
    var pagePortrait = doc.Pages.Add();
    var fontPortrait = FontRepository.FindFont("Calibri");
    pagePortrait.SetPageSize(width: PageSize.PageLetter.Width, height: PageSize.PageLetter.Height); // portrait dimensions     
    pagePortrait.PageInfo.Margin = new MarginInfo(left: 25, top: 25, right: 25, bottom: 25);
    var headersPortrait = new[]
    {
        CreateColumn("Column One P", fontPortrait, 20),
        CreateColumn("Column Two P", fontPortrait, 20),
        CreateColumn("Column Three P", fontPortrait, 20),                
    };

    var tablePortrait = new Table();            
    pagePortrait.Paragraphs.Add(tablePortrait);

    var titleRowPortrait = tablePortrait.Rows.Add();
    for (var colIndex = 1; colIndex <= headersPortrait.Length; colIndex++)
    {
        var cell = titleRowPortrait.Cells.Add();
        cell.Border = new BorderInfo(BorderSide.Bottom);
        cell.Paragraphs.Add(headersPortrait[colIndex - 1]);
    }

    // Some ramdon text
    for (var rowIndex = 1; rowIndex <= 100; rowIndex++)
    {
        var row = tablePortrait.Rows.Add();

        for (var colIndex = 1; colIndex <= headersPortrait.Length; colIndex++)
        {
            row.Cells.Add($"Row:{rowIndex} Col:{colIndex}");
        }
    }

    tablePortrait.RepeatingRowsCount = 1; // This will repeat the first row, which is the header
    tablePortrait.ColumnAdjustment = ColumnAdjustment.AutoFitToWindow; // Sp you dont have to set it manually

    // Landscape
    var pageLandscape = doc.Pages.Add();
    var fontLandscape = FontRepository.FindFont("Arial");
    pageLandscape.SetPageSize(width: PageSize.PageLetter.Height, height: PageSize.PageLetter.Width); // landscape dimensions     
    pageLandscape.PageInfo.Margin = new MarginInfo(left: 25, top: 25, right: 25, bottom: 25);
    var headersLandscape = new[]
    {
        CreateColumn("Column One L", fontLandscape, 18),
        CreateColumn("Column Two L", fontLandscape, 18),
        CreateColumn("Column Three L", fontLandscape, 18),
        CreateColumn("Column Fourth L", fontLandscape, 18),
        CreateColumn("Column Fifth L", fontLandscape, 18),
    };

    
    var tableLandscape = new Table();
    pageLandscape.Paragraphs.Add(tableLandscape);

    var titleRowLandscape = tableLandscape.Rows.Add();
    for (var colIndex = 1; colIndex <= headersLandscape.Length; colIndex++)
    {
        var cell = titleRowLandscape.Cells.Add();
        cell.Border = new BorderInfo(BorderSide.Bottom);
        cell.Paragraphs.Add(headersLandscape[colIndex - 1]);
    }

    // Some ramdon text
    for (var rowIndex = 1; rowIndex <= 100; rowIndex++)
    {
        var row = tableLandscape.Rows.Add();

        for (var colIndex = 1; colIndex <= headersLandscape.Length; colIndex++)
        {
            row.Cells.Add($"Row:{rowIndex} Col:{colIndex}");
        }
    }

    tableLandscape.RepeatingRowsCount = 1; // This will repeat the first row, which is the header
    tableLandscape.ColumnAdjustment = ColumnAdjustment.AutoFitToWindow;            
}



private static TextFragment CreateColumn(string colText, Font font, int fontSize)
{
    var tsColName = new TextSegment(colText);            

    var tf = new TextFragment();
    tf.TextState.Font = font;
    tf.TextState.FontSize = fontSize;

    tf.Segments.Add(tsColName);

    return tf;
}

The output generated:
TableWithHeaderInAllPages_output.pdf (337.5 KB)

I have realized that our application uses only aspose.words and after transforming all the content into the document builder , it converts it into a pdf/doc as requested.
So the property builder.getRowFormat().setHeadingFormat works when we save it as word doc, but when we save it as pdf, table headers do not repeat across pages.
The solution provided by you works when we build document using aspose.pdf. Is there a way we can still use aspose.words and still achieve repeating table headers when saved as pdf?

@adevabhaktuni, I suggest you do the question in the appropriate forum.

You can use this as a reference: