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

PDF export with auto sized columns

5 Answers 267 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Fredrik
Top achievements
Rank 2
Fredrik asked on 21 Feb 2015, 09:35 AM
Hello when I export to pdf all the columns has fixed sized based on the total size of the document.
insted I want my export document to have auto sized columns so that the text line of the document get as much space as it can.
please see attached files of the grid beeing exported and the export result in pdf

Code

                        var document = CreateDocument(grid);
                        document.LayoutMode = DocumentLayoutMode.Paged;
                        document.Measure(RadDocument.MAX_DOCUMENT_SIZE);
                        document.Arrange(new RectangleF(PointF.Empty, document.DesiredSize));
                        document.SectionDefaultPageOrientation = PageOrientation.Landscape;
                        document.SectionDefaultPageMargin = new Telerik.Windows.Documents.Layout.Padding(10, 10, 10, 10);
                        document.SectionDefaultPageSize = PaperTypeConverter.ToSize(PaperTypes.A3);
                        IDocumentFormatProvider provider = null;
                       
                     
                        provider = new PdfFormatProvider();
                       

                    
                        using (var stream = dialog.OpenFile())
                        {
                            provider.Export(document, stream);
                        }

5 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 24 Feb 2015, 09:29 AM
Hi Fredrik,

You can follow the approach illustrated in our "SpreadProcessing Integration" demo and use the AutoFitWidth method on a cells region. The "SpreadProcessing - Complete Solution to Create, Modify and Export Spreadsheets - Part I" blog post illustrates how you can use it. 

Furthermore, with our Q1 2015 release coming in a few days, we will provide built in integration and you will be able to export with a single ExportToPdf method. Using it, you will have the columns autosized automatically. 


Regards,
Maya
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Fredrik
Top achievements
Rank 2
answered on 24 Feb 2015, 09:43 AM
is this possible with the raddoucment or do i need to convert everything to workbook?
0
Maya
Telerik team
answered on 24 Feb 2015, 12:18 PM
Hello Fredrik,

If more appropriate, you can continue working with RadDocument and set the width of the columns to auto. More information can be found in:
1. Table - adjust column width to content
2. Guidelines for Printing with RadDocument.


Regards,
Maya
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Fredrik
Top achievements
Rank 2
answered on 24 Feb 2015, 01:25 PM

Hello i tryed the code from the post you listed.
now it auto fits the columns, but it wraps some coulms and ends up with alot of available space on the end o the document.
see attached


private RadDocument CreateDocumentPDF(RadGridView grid)
        {

            List<GridViewBoundColumnBase> columns = (from c in grid.Columns.OfType<GridViewBoundColumnBase>()

                                                     orderby c.DisplayIndex

                                                     select c).ToList();

 

            Table table = new Table();

 

            table.PreferredWidth = new TableWidthUnit(TableWidthUnitType.Auto);

            table.LayoutMode = TableLayoutMode.AutoFit;

 

            RadDocument document = new RadDocument();

            document.LayoutMode = DocumentLayoutMode.Paged;
            document.Measure(RadDocument.MAX_DOCUMENT_SIZE);
            document.Arrange(new RectangleF(PointF.Empty, document.DesiredSize));
            document.SectionDefaultPageOrientation = PageOrientation.Landscape;
            document.SectionDefaultPageMargin = new Telerik.Windows.Documents.Layout.Padding(10, 10, 10, 10);
            document.SectionDefaultPageSize = PaperTypeConverter.ToSize(PaperTypes.A3);
            document.ParagraphDefaultSpacingAfter = document.ParagraphDefaultSpacingBefore = 0;

 

 

            Telerik.Windows.Documents.Model.Section section = new Telerik.Windows.Documents.Model.Section();

            section.Blocks.Add(table);

            document.Sections.Add(section);

 

            if (grid.ShowColumnHeaders)
            {

                TableRow headerRow = new TableRow();

 

                if (grid.GroupDescriptors.Count() > 0)
                {

                    TableCell indentCell = new TableCell();

                    indentCell.PreferredWidth = new TableWidthUnit(grid.GroupDescriptors.Count() * 20);

                    //indentCell.Background = HeaderBackgroundPicker.SelectedColor;

                    headerRow.Cells.Add(indentCell);

                }

 

                for (int i = 0; i < columns.Count(); i++)
                {

                    TableCell cell = new TableCell();

                    //cell.Background = HeaderBackgroundPicker.SelectedColor;

                    AddCellValue(cell, columns[i].Header.ToString());

                    cell.PreferredWidth = new TableWidthUnit((float)columns[i].ActualWidth);

                    headerRow.Cells.Add(cell);

                }

 

                table.Rows.Add(headerRow);

            }

 

            if (grid.Items.Groups != null)
            {

                for (int i = 0; i < grid.Items.Groups.Count(); i++)
                {

                    AddGroupRow(table, grid.Items.Groups[i] as QueryableCollectionViewGroup, columns, grid);

                }

            }

            else
            {

                AddDataRows(table, grid.Items, columns, grid);

            }

 

            return document;

        }

        private void AddDataRows(Table table, IList items, IList<GridViewBoundColumnBase> columns, RadGridView grid)
        {

            for (int i = 0; i < items.Count; i++)
            {

                TableRow row = new TableRow();

 

                if (grid.GroupDescriptors.Count() > 0)
                {

                    TableCell indentCell = new TableCell();

                    indentCell.PreferredWidth = new TableWidthUnit(grid.GroupDescriptors.Count() * 20);

                    //indentCell.Background = RowBackgroundPicker.SelectedColor;

                    row.Cells.Add(indentCell);

                }

 

                for (int j = 0; j < columns.Count(); j++)
                {

                    TableCell cell = new TableCell();

 

                    object value = columns[j].GetValueForItem(items[i]);

 

                    AddCellValue(cell, value != null ? value.ToString() : string.Empty);

 

                    cell.PreferredWidth = new TableWidthUnit((float)columns[j].ActualWidth);

                    //cell.Background = RowBackgroundPicker.SelectedColor;

 

                    row.Cells.Add(cell);

                }

 

                table.Rows.Add(row);

            }

        }

 

        private void AddGroupRow(Table table, QueryableCollectionViewGroup group, IList<GridViewBoundColumnBase> columns, RadGridView grid)
        {

            TableRow row = new TableRow();

 

            int level = GetGroupLevel(group);

            if (level > 0)
            {

                TableCell cell = new TableCell();

                cell.PreferredWidth = new TableWidthUnit(level * 20);

                //cell.Background = GroupHeaderBackgroundPicker.SelectedColor;

                row.Cells.Add(cell);

            }

 

            TableCell aggregatesCell = new TableCell();

            //aggregatesCell.Background = GroupHeaderBackgroundPicker.SelectedColor;

            aggregatesCell.ColumnSpan = columns.Count() + (grid.GroupDescriptors.Count() > 0 ? 1 : 0) - (level > 0 ? 1 : 0);

 

            AddCellValue(aggregatesCell, group.Key != null ? group.Key.ToString() : string.Empty);

 

            foreach (AggregateResult result in group.AggregateResults)
            {

                AddCellValue(aggregatesCell, result.FormattedValue != null ? result.FormattedValue.ToString() : string.Empty);

            }

 

            row.Cells.Add(aggregatesCell);

 

            table.Rows.Add(row);

 

            if (group.HasSubgroups)
            {

                for (int i = 0; i < group.Subgroups.Count(); i++)
                {

                    AddGroupRow(table, group.Subgroups[i] as QueryableCollectionViewGroup, columns, grid);

                }

            }

            else
            {

                for (int i = 0; i < group.ItemCount; i++)
                {

                    AddDataRows(table, group.Items, columns, grid);

                }

            }

        }

 

        private void AddCellValue(TableCell cell, string value)
        {

            try
            {

                Telerik.Windows.Documents.Model.Paragraph paragraph = new Telerik.Windows.Documents.Model.Paragraph();

                cell.Blocks.Add(paragraph);

 

                Telerik.Windows.Documents.Model.Span span = new Telerik.Windows.Documents.Model.Span();

                span.Text = value;

 

                paragraph.Inlines.Add(span);

            }

            catch (Exception e)
            {

                string msg = e.Message;

            }

        }

        private int GetGroupLevel(IGroup group)
        {

            int level = 0;

 

            IGroup parent = group.ParentGroup;

 

            while (parent != null)
            {

                level++;

                parent = parent.ParentGroup;

            }

 

            return level;

        }

0
Petya
Telerik team
answered on 26 Feb 2015, 12:55 PM
Hello Fredrik,

The AutoFit functionality of tables in RadDocument works based on the longest single word in the table cell. If a particular cell contains a paragraph with several words that are split by spaces, this column might still be resized and not fit the whole text. The table layout logic is very complex and at this point we haven't scheduled a change in this regard.

The best I can suggest if you plan on using RadDocument is to use fixed sizes for the column, but base them on the width of the GridView's columns. 

Another alternative is to use the export for RadGridView Maya mentioned about. Q1 2015 is already live and available for download from your account, and you can learn more about the functionality at ExportFormat.Xlsx and ExportFormat.Pdf. This method for producing document will auto-fit the columns for you.

Regards,
Petya
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
GridView
Asked by
Fredrik
Top achievements
Rank 2
Answers by
Maya
Telerik team
Fredrik
Top achievements
Rank 2
Petya
Telerik team
Share this question
or