|
| 1 | +--- |
| 2 | +title: Add Page Breaks Between Groups in RadGrid PDF Export |
| 3 | +description: Learn how to insert page breaks in RadGrid to ensure each group starts on a new page when exported to PDF. |
| 4 | +type: how-to |
| 5 | +page_title: Add Page Breaks Between Groups in RadGrid PDF Export |
| 6 | +slug: grid-add-page-breaks-between-groups-in-pdf-export |
| 7 | +tags: radgrid, export, pdf, group, page-break, asp.net ajax |
| 8 | +res_type: kb |
| 9 | +ticketid: 1667087 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | +<tbody> |
| 16 | +<tr> |
| 17 | +<td>Product</td> |
| 18 | +<td>RadGrid for ASP.NET AJAX</td> |
| 19 | +</tr> |
| 20 | +<tr> |
| 21 | +<td>Version</td> |
| 22 | +<td>2024.3.805</td> |
| 23 | +</tr> |
| 24 | +</tbody> |
| 25 | +</table> |
| 26 | + |
| 27 | +## Description |
| 28 | + |
| 29 | +When exporting a [RadGrid](https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/functionality/exporting/overview) to PDF, it's required to start each group on a new page. To achieve this, a page break needs to be inserted after each group's end. This KB article also answers the following questions: |
| 30 | +- How can I ensure each group in RadGrid starts on a new page when exporting to PDF? |
| 31 | +- Is it possible to insert page breaks in RadGrid's PDF export based on the group change? |
| 32 | +- How to customize RadGrid's PDF export to have each group on separate pages? |
| 33 | + |
| 34 | +## Solution |
| 35 | + |
| 36 | +To insert a page break on each group change when exporting RadGrid to PDF, follow these steps: |
| 37 | + |
| 38 | +1. **Enable the Group Footer** in the `MasterTableView` by setting the `ShowGroupFooter` property to `true`. |
| 39 | + |
| 40 | + ````ASPX |
| 41 | + <MasterTableView ShowGroupFooter="true"> |
| 42 | + ```` |
| 43 | +
|
| 44 | +2. **Inject the Page Break into the Footer of Each Group**. In the `PreRender` event of RadGrid, iterate through the Group Footer items and add the page break into their cells. |
| 45 | +
|
| 46 | + ````C# |
| 47 | + protected void RadGrid1_PreRender(object sender, EventArgs e) |
| 48 | + { |
| 49 | + // Get all GroupFooter items |
| 50 | + GridItem[] groupFooterItems = RadGrid1.MasterTableView.GetItems(GridItemType.GroupFooter); |
| 51 | +
|
| 52 | + // Iterate through all except the last |
| 53 | + for (int i = 0; i < groupFooterItems.Length - 1; i++) |
| 54 | + { |
| 55 | + // Inject the page break into the first cell of the Group Footer |
| 56 | + groupFooterItems[i].Cells[0].Text = "<?hard-pagebreak ?>"; |
| 57 | + } |
| 58 | + } |
| 59 | + ```` |
| 60 | +
|
| 61 | +3. **Optional: Hide the Group Footer**. If you do not wish to display the Group Footer in the exported PDF, use CSS to target the Footer element and hide it. |
| 62 | +
|
| 63 | + ````CSS |
| 64 | + <style> |
| 65 | + .RadGrid .rgMasterTable .rgFooter { |
| 66 | + display: none; |
| 67 | + } |
| 68 | + </style> |
| 69 | + ```` |
| 70 | +
|
| 71 | +This setup ensures that each group in the RadGrid starts on a new page in the exported PDF by utilizing a hard page break at the end of each group. |
| 72 | +
|
| 73 | +## See Also |
| 74 | +
|
| 75 | +- [RadGrid Export to PDF Documentation](https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/functionality/exporting/export-formats/export-to-pdf) |
| 76 | +- [RadGrid Grouping Overview](https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/data-editing/grouping/grouping) |
| 77 | +- [Customizing RadGrid PDF Export](https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/functionality/exporting/export-to-pdf/pdf-export-customization) |
0 commit comments