Articles in this section

How to set custom column width while exporting to PDF in WPF DataGrid?

WPF DataGrid (SfDataGrid) does not provide the direct support to set custom column width while exporting to PDF. You can set custom column width while exporting to PDF by overriding SetColumnWidth method in SfDataGridToPdfConverter.

public class SfDataGridToPdfConverterExt : SfDataGridToPdfConverter
{ 
        public override PdfDocument ExportToPdf(SfDataGrid sfgrid)
        {
            return base.ExportToPdf(sfgrid);
        }
 
        public override PdfDocument ExportToPdf(SfDataGrid sfgrid, ICollectionViewAdv view, PdfExportingOptions pdfExportingOptions)
        {
            return base.ExportToPdf(sfgrid, view, pdfExportingOptions);
        }
 
        public override PdfDocument ExportToPdf(SfDataGrid sfgrid, PdfExportingOptions pdfExportingOptions)
        {
            return base.ExportToPdf(sfgrid, pdfExportingOptions);
        }
 
        private int startColIndex = 0;
 
        protected override void SetColumnWidth(SfDataGrid sfgrid, PdfGrid pdfGrid, PdfExportingOptions pdfExportingOptions)
        {
            var gridColumns = pdfExportingOptions.Columns;
 
            var groupColumnDescriptionsCount = (int)pdfExportingOptions.GetType().GetProperty("GroupColumnDescriptionsCount", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(pdfExportingOptions);
            
            int columnIndex = groupColumnDescriptionsCount > 0 ? groupColumnDescriptionsCount : startColIndex;
            //After grouping while export to pdf by setting the Export group option as false, need to skip indent column count for setting the columns actual width of PDf grid.
            columnIndex = pdfExportingOptions.ExportGroups ? columnIndex : 0;
            foreach (var column in gridColumns)
            {
                var gridColumn = sfgrid.Columns[column];
 
                var actualWidth = (float)(gridColumn.ActualWidth / 1.5);
 
                //set the width for paritucular column while pdf Exporting
                pdfGrid.Columns[columnIndex].Width = 200;
                columnIndex++;
            }
        }
}

Shows the customizing column width while exporting to PDF in SfDataGridTake a moment to peruse the WPF DataGrid - Export To PDF documentation, where you can find about export to PDF with code examples.

You can download the example from GitHub.


Conclusion

I hope you enjoyed learning about how to set custom column width while exporting to PDF in WPF DataGrid.

You can refer to our WPF DataGrid feature tour page to learn about its other groundbreaking feature representations and documentationto understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied