RadGridView Export PDF RowHeight and ColumnWidth

1 Answer 17 Views
GridView
Benedikt
Top achievements
Rank 3
Iron
Iron
Iron
Benedikt asked on 30 Sep 2021, 06:08 AM

Hey guys,

Some of my users want to print a GridView and write things in the printed Grid.
I know, dumb... 

The Export is currently done with this:

        public void PDFExport()
        {
            Telerik.Windows.Controls.GridView.IColumnFilterDescriptor tempFilter = ucEndtermintreueDaten.rgvEndtermintreueDaten.Columns["Ausblenden"].ColumnFilterDescriptor;
            tempFilter.DistinctFilter.AddDistinctValue(false);

            SaveFileDialog dialog = new SaveFileDialog();
            dialog.DefaultExt = ".pdf";
            dialog.Filter = string.Format("(*.{0})|*.{1}", "pdf", "pdf");
            dialog.FileName = "Endtermintreue " + MinDatum.ToString("dd.MM.yy") + "-" + MaxDatum.ToString("dd.MM.yy");

            var result = dialog.ShowDialog();
            if ((bool)result)
            {
                using (var stream = dialog.OpenFile())
                {
                    GridViewDocumentExportOptions options = new GridViewDocumentExportOptions()
                    {
                        ShowColumnHeaders = true,
                        AutoFitColumnsWidth = true
                    };
                    options.ExcludedColumns.Add(ucEndtermintreueDaten.rgvEndtermintreueDaten.Columns["Ausblenden"]);

                    ucEndtermintreueDaten.rgvEndtermintreueDaten.ExportToPdf(stream, options);
                }

                System.Diagnostics.Process.Start(dialog.FileName);
            }

            tempFilter.Clear();
        }

 

My problem is, that the rows are to small and the columns not wide enough to write something in it from hand if printed.
But I don't see any option to set the row height, and if I set AutoFitColumnsWidth to false it just cuts off the text in it.
Is it possible to set these options? 
Or maybe do I have to set the RowHeight and ColumnWidth in the element and revert it back to the original values after export?

Greetings Benedikt

1 Answer, 1 is accepted

Sort by
1
Accepted
Stenly
Telerik team
answered on 05 Oct 2021, 02:17 PM

Hello Benedikt,

To achieve this behavior, what I could suggest is to use the ExportToWorkbook method of the RadGridView control. Then you can change columns' widths and the rows' heights, of the created workbook. The following code snippet shows how to modify the columns and rows present in the workbook:

//Create Workbook class instance through the ExportToWorkbook method of the RadGridView
Workbook workbook = this.gridView.ExportToWorkbook();

//Change the width of the columns
for (int i = 0; i < workbook.ActiveWorksheet.UsedCellRange.ColumnCount; i++)
{
    workbook.ActiveWorksheet.Columns[i].SetWidth(new ColumnWidth(this.gridView.Columns[i].ActualWidth * 3, true));
}

//Change the height of the rows
for (int i = 0; i < workbook.ActiveWorksheet.UsedCellRange.RowCount; i++)
{
    var height = workbook.ActiveWorksheet.Rows[i].GetHeight().Value.Value;
    workbook.ActiveWorksheet.Rows[i].SetHeight(new RowHeight(height + 50, true));
}

After that, using PdfFormatProvider class to export the workbook to a PDF file. The workbook can also be exported to an excel file using the XlsxFormatProvider class. The following snippet shows how to use the PdfFormatProvider class:

//Export the Workbook to a Pdf file 
SaveFileDialog dialog = new SaveFileDialog();
dialog.DefaultExt = "pdf";
dialog.Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", "pdf", "PDF");
dialog.FilterIndex = 1;

if (dialog.ShowDialog() == true)
{
    //If you wish to export to xlsx format, change the PdfFormatProvider to XlsxFormatProvider
    var provider = new PdfFormatProvider();
    using (var output = dialog.OpenFile())
    {
        provider.Export(workbook, output);
    }
}

That said, I have prepared a sample project that implements this approach, so, could you give it a try?

Regards,
Stenly
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Benedikt
Top achievements
Rank 3
Iron
Iron
Iron
commented on 06 Oct 2021, 06:40 AM

Hi Stenly,

 

thanks for the answer and sample project.
This works very well :) 

I will play around with the Margins and Fontsize and have what I want.

 

Have a nice day,

Greetings
Benedikt

Tags
GridView
Asked by
Benedikt
Top achievements
Rank 3
Iron
Iron
Iron
Answers by
Stenly
Telerik team
Share this question
or