KeepTogether Sample

Posted by: jonhenning on 5 September 2025, 7:44 pm EST

  • Posted 5 September 2025, 7:44 pm EST

    I am trying your KeepTogether sample found here

    https://developer.mescius.com/document-solutions/dot-net-excel-api/docs/online/templates/paginated-templates/pagination-properties-functions#keeptogether

    I am including a sample console app that runs this code and generates a PDF. Why does this KeepTogether seem to be ignored?template.zip

  • Posted 8 September 2025, 7:30 am EST

    Hi,

    Thank you for sharing the sample project. To ensure that the template breaks correctly into pages and that groups stay together when breaking vertically, you need to first set TemplateOptions.PaginationMode to true as shown below:

    workbook.Names.Add("TemplateOptions.PaginationMode", "true");

    Please see the updated sample project for reference - template_updated.zip

    Best Regards,

    Kartik

  • Posted 8 September 2025, 11:32 am EST - Updated 8 September 2025, 1:25 pm EST

    Thank you for the quick reply. Unfortunately, in creating my sample I had forgotten that I was including this in the Name Manager in the template and did not include it in the sample I sent. (your sample works now) I have updated the example in the new attachment to get closer to what I am trying to accomplish. You will see that in my template now I cannot seem to get the footer to stay on one page. Can you suggest how I can make this work properly?template.zip

    Note: I realize that the scenario I have shown in my simplified example could be fixed by stating that the CP (Count Per Page) be less than 18. However in my real example I have another table shown below this list that is only displayed on the last page. When this is rendered on the last page, the footer has the same issue as my simplified example.

  • Posted 9 September 2025, 6:54 am EST

    Hi,

    Thank you for sharing the updated sample project. We were able to reproduce the behavior you mentioned. The pagination in DsExcel appears to be working correctly and the behavior you observe is due to insufficient space on the page to accommodate all of the data, which naturally causes some content to break onto a new page.

    This does not seem to be an issue with the KeepTogether setting, as it is designed to “keep the cell and its descendants on the same page as much as possible while paginating vertically", provided that one page can fit all the content - which, in your case, it cannot.

    We also verified the behavior by exporting the processed workbook to .xlsx, and the pagination worked as expected, breaking the content across different sheets. Exporting a sheet from MS Excel to PDF showed similar behavior, where part of the content is shifted to a new page because one page cannot fit everything.

    The best solution for you would be to either reduce the size of the rows in your template so they fit on one page, or use the worksheet.PageSetup.BestFitRows = true; option, allowing DsExcel to adjust the rows automatically.

    Attachment: template_cust.zip

    Best Regards,

    Kartik

  • Posted 10 September 2025, 12:28 am EST

    Hi,

    We investigated further and found two additional properties that you can use to fit the content on a single page:

    worksheet.PageSetup.FitToPagesTall = 1;
    worksheet.PageSetup.FitToPagesWide = 1;

    Please test these and let us know if they meet your requirements.

    Best Regards,

    Kartik

  • Posted 10 September 2025, 10:37 am EST - Updated 10 September 2025, 10:42 am EST

    Thanks for the response. Unfortunately, the actual report running will have a variable number of rows in the list (1 - 1000). Then below that list on the last page only there will be another table with a variable number of rows (1 - 10). Each page will have a footer.

    So it is not possible to tweak for one number of rows as it will break with the other variations. The only way to make this work would be to limit the number of rows in the first list in such a way that would allow for up to 10 rows in the last page list + the footer height. So if for example we can fit a total of 15 rows on the page without it breaking the footer apart we would have to limit the main table to 5 rows as the last page table could be up to 10 rows. If we have 100 main table rows, then we would only be putting 5 rows per page so we can make room for the last page. This is not the desired output as it is 20 pages where we could have fit this on 8 pages (100 main table rows + 10 last page rows = 110 / 15 rows per page).

    In the most ideal situation I would state that the footer should never appear on a page by itself. What I was hoping for in this case was to never have the footer be split between pages.

  • Posted 12 September 2025, 10:07 am EST

    Hi,

    Apologies for the delay in response.

    We are reviewing your requirement to see if we can provide a suitable solution and will update you as soon as possible.

    Best Regards,

    Kartik

  • Posted 15 September 2025, 8:33 am EST

    Hi,

    Apologies for the delayed response. Since we require additional information from you, which might be private, we have created a new support case for you on our private support portal. You can access this case by going to the My Support section of your Mescius account, where you will find the support ticket with Case ID: CAS-59145-Y0Y2G3

    We have already replied there and kindly request you to continue the discussion in that thread to avoid any confusion.

    Best Regards,

    Kartik

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels