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;
}