HI,
I am fairly new to WPF and I am trying to print preview a grid having more than 10 columns . All the columns do not fit on a page for printing. So I would like to continue printing rest of the columns on page 2 . Or if anyother approach , so that the user can see the data for all the columns of the grid.. Here is the sample code. Please let me know.
public static void PrintPreview(Telerik.Windows.Controls.RadGridView grid)
{
if (grid != null)
{
var dialog = new PrintDialog();
var capabilities = dialog.PrintQueue.GetPrintCapabilities(dialog.PrintTicket);
var pageSize = new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight);
var extentSize = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
FixedDocument fixedDoc = (PaginateGridInToFixedDocument(ToPrintFriendlyGrid(grid, extentSize), dialog));
BusinessObjectGridPrintPreviewView previewWindow = new BusinessObjectGridPrintPreviewView();
previewWindow.RadGridView = grid;
previewWindow.Document = fixedDoc;
previewWindow.ShowDialog();
}
}
public static FixedDocument PaginateGridInToFixedDocument(GridViewDataControl element, PrintDialog dialog)
{
//var capabilities = dialog.PrintQueue.GetPrintCapabilities(dialog.PrintTicket);
//var pageSize = new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight);
//var extentSize = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
var capabilities = dialog.PrintQueue.GetPrintCapabilities(dialog.PrintTicket);
Size pageSize = new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight);
Size extentSize = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
var fixedDocument = new FixedDocument();
var totalHeight = 0.0;
var totalWidth = 0.0;
element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
element.Arrange(new Rect(new Point(0, 0), element.DesiredSize));
totalHeight = element.DesiredSize.Height;
//totalWidth = element.DesiredSize.Width;
List<
double
> widthBreakList = new List<
double
>();
double widthBreakTotal = 0;
if (element.GroupDescriptors.Count > 0)
{
widthBreakTotal = widthBreakTotal + (25 * element.GroupDescriptors.Count);
}
double lastBreakLocation = 0;
widthBreakList.Add(0);
foreach (Telerik.Windows.Controls.GridViewColumn column in element.Columns)
{
if (widthBreakTotal + column.ActualWidth >= (lastBreakLocation + extentSize.Width))
{
widthBreakList.Add(widthBreakTotal);
lastBreakLocation = widthBreakTotal;
}
else
{
widthBreakTotal += column.ActualWidth;
}
}
totalWidth = widthBreakTotal;
widthBreakList.Add(totalWidth);
var yOffset = 0d;
var xOffset = 0d;
var xOffsetNext = 0d;
for (int i = 0; i < (widthBreakList.Count - 1); i++)
{
xOffset = widthBreakList[i];
xOffsetNext = widthBreakList[i + 1];
yOffset = 0d;
while (yOffset <
totalHeight
)
{
var
brush
=
new
VisualBrush(element)
{
Stretch
= Stretch.None,
AlignmentX
= AlignmentX.Left,
AlignmentY
= AlignmentY.Top,
ViewboxUnits
=
BrushMappingMode
.Absolute,
TileMode
= TileMode.None,
Viewbox
=
new
Rect(xOffset, yOffset, xOffsetNext, (extentSize.Height - (extentSize.Height % element.RowHeight)))
};
var
pageContent
=
new
PageContent();
var
page
=
new
FixedPage();
((IAddChild)pageContent).AddChild(page);
fixedDocument.Pages.Add(pageContent);
page.Width
=
pageSize
.Width;
page.Height
=
pageSize
.Height;
var
canvas
=
new
Canvas();
FixedPage.SetLeft(canvas, capabilities.PageImageableArea.OriginWidth);
FixedPage.SetTop(canvas, capabilities.PageImageableArea.OriginHeight);
canvas.Width
=
xOffsetNext
- xOffset;
canvas.Height = (extentSize.Height - (extentSize.Height % element.RowHeight));
canvas.Background
=
brush
;
page.Children.Add(canvas);
yOffset += extentSize.Height - (extentSize.Height % element.RowHeight);
}
}
return fixedDocument;
}
/// <summary>
/// Convert(Re-create) RadGridView into a print-friendly format
/// </
summary
>
/// <
param
name
=
"source"
></
param
>
/// <
returns
></
returns
>
public static GridViewDataControl ToPrintFriendlyGrid(GridViewDataControl source, Size extentSize)
{
var grid = new RadGridView()
{
ItemsSource = source.ItemsSource,
RowIndicatorVisibility = Visibility.Collapsed,
ShowGroupPanel = false,
CanUserFreezeColumns = false,
IsFilteringAllowed = false,
AutoExpandGroups = true,
AutoGenerateColumns = false,
ColumnWidth = GridViewLength.SizeToHeader
};
grid.ShowColumnFooters = source.ShowColumnFooters;
grid.ShowGroupFooters = source.ShowGroupFooters;
grid.ShowColumnHeaders = source.ShowColumnHeaders;
grid.Width = source.ActualWidth;
if (source.DataContext is BusinessObjectGridViewModel)
{
GridColumnCreator columnCreator = new GridColumnCreator(source.DataContext as BusinessObjectGridViewModel, grid);
columnCreator.CreateColumns();
grid.ColumnWidth = GridViewLength.Auto;
foreach (Telerik.Windows.Controls.GridViewColumn column in grid.Columns)
{
column.Width = GridViewLength.Auto;
column.MaxWidth = extentSize.Width;
}
}
else
{
foreach (var column in source.Columns.OfType<
GridViewDataColumn
>())
{
var newColumn = new GridViewDataColumn();
newColumn.Header = column.Header;
newColumn.DataMemberBinding = new System.Windows.Data.Binding(column.UniqueName);
newColumn.Width = column.Width;
grid.Columns.Add(newColumn);
}
grid.SortDescriptors.AddRange(source.SortDescriptors);
grid.GroupDescriptors.AddRange(source.GroupDescriptors);
grid.FilterDescriptors.AddRange(source.FilterDescriptors);
}
grid.DataContext = source.DataContext;
StyleManager.SetTheme(grid, StyleManager.GetTheme(grid));
return grid;
}