This is a migrated thread and some comments may be shown as answers.

Ability to change column width for ExportToPdf

3 Answers 223 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Russell
Top achievements
Rank 1
Russell asked on 07 Aug 2017, 06:19 PM

I am trying to figure out if it is possible to set the AutoFitColumnWidth or control the width of a column for the PDF output of a WPF Grid?

 

I have the following code for setting up my export options.

public void ExportToPdf(RadGridView grid)
{
    grid.ElementExportingToDocument -= Grid_ElementExportingToDocument;
    grid.ElementExportingToDocument += Grid_ElementExportingToDocument;
     
    var exportOptions = new GridViewDocumentExportOptions
    {
        AutoFitColumnsWidth = false,
        ExcludedColumns = { grid.Columns[9], grid.Columns[10], grid.Columns[11], grid.Columns[12] },
        ShowColumnHeaders = grid.ShowColumnHeaders
    };
 
    var dialog = new SaveFileDialog
    {
        DefaultExt = "pdf",
        FileName = SelectedCollege.CollegeCD + " - Roster.pdf",
        Filter = "Pdf Files|*.pdf"
    };
 
    if (dialog.ShowDialog() == true)
    {
        using (var stream = dialog.OpenFile())
        {
            grid.ExportToPdf(stream, exportOptions);
        }
    }           
}

 

I do not want ALL columns to auto size, because I have columns with single digit values in them and they look terrible squished together. I REALLY would like to set the width manually so the output looks good. 

My Grid columns are all Width="*" on the XAML side. And as you can see I am subscribing to the "ElementExportingToDocument" event and styling the grid to look the way it needs to look as far as foreground and background sizes. 

Are we limited only to the CellSelectionStyle properties when it comes to customizing the export? 

3 Answers, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 10 Aug 2017, 12:04 PM
Hi Russell,

To achieve the desired behavior you can use the extension method related to the exporting of RadGridView - ExportToWorkbook(). Then you can change Columns Width of the created Workbook and after that Using PdfFormatProvider to export a Workbook to a PDF format. 

I hope this helps.

Regards,
Martin Vatev
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
0
Russell
Top achievements
Rank 1
answered on 24 Aug 2017, 07:37 PM

Gotcha. Thanks Martin. I will give that a shot.

It would be a HUGE help in the future to expand on the CellSelectionStyle or make a ColumnStyle or different classes for the GridViewDocumentVisualExportParameters. Right now cell style is used for each ExportElement enum, even if its row, header, cell or table. The export feature was one of the main reasons we decided to go with Telerik for WPF and other items and having to hook unrelated pieces together to make sure we can achieve business rules that seem fairly common (adjusting column width for example) on PDF's would be a huge boost to your capabilities. Right now the default export is very minimal, which I understand, but I have to make Column Header text really large to adjust column width until I have time to try this solution. 

Also, if I have row style based on rules that I apply when the grid is rendered, why do I have to reapply them when I export? For example, if the grid item meets one criteria, the background is green, otherwise it is default. When I export all rows are whatever color the top row in the grid is. I have to do the same business logic twice to get the colors of rows and text to export in the correct manner. This is counterproductive and I am not quite sure why Telerik didn't spend more time on something that seems quite important for the GridView's core functionality. Its very vanilla and hard to work with right now. Just my 2 cents. I will keep plugging away and try out your suggestion. Thank you!

0
Martin Ivanov
Telerik team
answered on 29 Aug 2017, 04:26 PM
Hello Russell,

Thank you for your sharing your thoughts on the exporting feature. We will take it into account and see if we can improve this.

About the exporting of the default styles, currently the export doesn't use the row styles because the document model works with different type of styles. The grid rows could be customized using the full power of the WPF styling mechanism. Each of those should be manually mapped in the export process to the document model style. This should be done with custom code. Because there is very big variety of different style settings that could be applied, the export mechanism can't know about each one and apply it to the document model style. Additionally, there are some settings that are not supported by the document model. This is why the gridview allows you to export only the raw data and if you need any custom style you can manually set this up.

Regards,
Martin Ivanov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
GridView
Asked by
Russell
Top achievements
Rank 1
Answers by
Martin
Telerik team
Russell
Top achievements
Rank 1
Martin Ivanov
Telerik team
Share this question
or