Hi,
I am having some issues printing a grid view. The code I use to print is modified from the downloadable example. The issue is that the page orientation is always portrait, and the grid appears rotated 90 degrees on the page no top margin (see attached image, I printed to pdf). Instead, I would like the page in landscape mode, with proper margins. It also appears that if the grid view is wider than the page, new pages are not added, instead the values are cut off.
So, my questions are:
1. How can I change the page orientation when printing the grid view (programmatically)?
2. How can I add margins?
3. How can I print grid views that are wider than a page on multiple pages?
Here is my code:
In my context menu:
my click handler (note, the grid was created completely in code and in the XAML pages constructor, was added to a stack panel as a child.
And my code to print:
I am having some issues printing a grid view. The code I use to print is modified from the downloadable example. The issue is that the page orientation is always portrait, and the grid appears rotated 90 degrees on the page no top margin (see attached image, I printed to pdf). Instead, I would like the page in landscape mode, with proper margins. It also appears that if the grid view is wider than the page, new pages are not added, instead the values are cut off.
So, my questions are:
1. How can I change the page orientation when printing the grid view (programmatically)?
2. How can I add margins?
3. How can I print grid views that are wider than a page on multiple pages?
Here is my code:
In my context menu:
//print
MenuItem mprintGridItem =
new
MenuItem();
mprintGridItem.Header =
"Print Grid"
;
mprintGridItem.Click += TelerikGridPrint;
MainGrid.ContextMenu.Items.Add(mprintGridItem);
my click handler (note, the grid was created completely in code and in the XAML pages constructor, was added to a stack panel as a child.
private
void
TelerikGridPrint(
object
sender,
object
args)
{
try
{
TelerikGridDocumentCreatorModel.Print(telerikResults.GridControl);
}
catch
(Exception ex)
{
Logger.LogError(
"TelerikGridPrint: "
+ ex);
}
}
And my code to print:
public
static
class
TelerikGridDocumentCreatorModel
// : DependencyObject, INotifyPropertyChanged
{
private
static
Color HeaderBackground = Color.FromArgb(255, 127, 127, 127);
private
static
Color RowBackground = Color.FromArgb(255, 251, 247, 255);
private
static
Color GroupHeaderBackground = Color.FromArgb(255, 216, 216, 216);
public
static
void
Print(
object
parameter)
{
RadGridView grid = (RadGridView)parameter;
RadRichTextBox rtb =
new
RadRichTextBox() { Height = 0 };
rtb.Name =
"RadRichTextBox1"
;
Grid parent = grid.ParentOfType<Grid>();
if
(parent !=
null
&& parent.FindName(rtb.Name) ==
null
)
{
parent.Children.Add(rtb);
rtb.ApplyTemplate();
}
rtb.Dispatcher.BeginInvoke((Action)(() =>
{
rtb.Document = CreateDocument(grid);
}));
PrintSettings pSettings =
new
PrintSettings();
pSettings.DocumentName =
"MyDocument"
;
pSettings.PrintMode = PrintMode.Native;
pSettings.PrintScaling = PrintScaling.None;
rtb.Print(pSettings);
}
private
static
RadDocument CreateDocument(RadGridView grid)
{
List<GridViewBoundColumnBase> columns = (from c
in
grid.Columns.OfType<GridViewBoundColumnBase>()
orderby c.DisplayIndex
select c).ToList();
Telerik.Windows.Documents.Model.Table table =
new
Telerik.Windows.Documents.Model.Table();
RadDocument document =
new
RadDocument();
Telerik.Windows.Documents.Model.Section section =
new
Telerik.Windows.Documents.Model.Section();
Telerik.Windows.Documents.Model.Paragraph paragraph1 =
new
Telerik.Windows.Documents.Model.Paragraph();
Telerik.Windows.Documents.Model.Paragraph paragraph2 =
new
Telerik.Windows.Documents.Model.Paragraph();
section.Blocks.Add(paragraph1);
section.Blocks.Add(table);
section.Blocks.Add(paragraph2);
document.Sections.Add(section);
if
(grid.ShowColumnHeaders)
{
Telerik.Windows.Documents.Model.TableRow headerRow =
new
Telerik.Windows.Documents.Model.TableRow();
if
(grid.GroupDescriptors.Count > 0)
{
Telerik.Windows.Documents.Model.TableCell indentCell =
new
Telerik.Windows.Documents.Model.TableCell();
indentCell.PreferredWidth =
new
TableWidthUnit(grid.GroupDescriptors.Count * 20);
indentCell.Background = HeaderBackground;
headerRow.Cells.Add(indentCell);
}
for
(
int
i = 0; i < columns.Count; i++)
{
Telerik.Windows.Documents.Model.TableCell cell =
new
Telerik.Windows.Documents.Model.TableCell();
cell.Background = HeaderBackground;
AddCellValue(cell, columns[i].UniqueName);
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
static
void
AddDataRows(Telerik.Windows.Documents.Model.Table table, IList items, IList<GridViewBoundColumnBase> columns, RadGridView grid)
{
for
(
int
i = 0; i < items.Count; i++)
{
Telerik.Windows.Documents.Model.TableRow row =
new
Telerik.Windows.Documents.Model.TableRow();
if
(grid.GroupDescriptors.Count > 0)
{
Telerik.Windows.Documents.Model.TableCell indentCell =
new
Telerik.Windows.Documents.Model.TableCell();
indentCell.PreferredWidth =
new
TableWidthUnit(grid.GroupDescriptors.Count * 20);
indentCell.Background = RowBackground;
row.Cells.Add(indentCell);
}
for
(
int
j = 0; j < columns.Count; j++)
{
Telerik.Windows.Documents.Model.TableCell cell =
new
Telerik.Windows.Documents.Model.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 = RowBackground;
row.Cells.Add(cell);
}
table.Rows.Add(row);
}
}
private
static
void
AddGroupRow(Telerik.Windows.Documents.Model.Table table, QueryableCollectionViewGroup group, IList<GridViewBoundColumnBase> columns, RadGridView grid)
{
Telerik.Windows.Documents.Model.TableRow row =
new
Telerik.Windows.Documents.Model.TableRow();
int
level = GetGroupLevel(group);
if
(level > 0)
{
Telerik.Windows.Documents.Model.TableCell cell =
new
Telerik.Windows.Documents.Model.TableCell();
cell.PreferredWidth =
new
TableWidthUnit(level * 20);
cell.Background = GroupHeaderBackground;
row.Cells.Add(cell);
}
Telerik.Windows.Documents.Model.TableCell aggregatesCell =
new
Telerik.Windows.Documents.Model.TableCell();
aggregatesCell.Background = GroupHeaderBackground;
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)
{
foreach
(var g
in
group.Subgroups)
{
AddGroupRow(table, g
as
QueryableCollectionViewGroup, columns, grid);
}
}
else
{
AddDataRows(table, group.Items, columns, grid);
}
}
private
static
void
AddCellValue(Telerik.Windows.Documents.Model.TableCell cell,
string
value)
{
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);
}
private
static
int
GetGroupLevel(IGroup group)
{
int
level = 0;
IGroup parent = group.ParentGroup;
while
(parent !=
null
)
{
level++;
parent = parent.ParentGroup;
}
return
level;
}