I have a grid, that I want to print, but is has to scale to fit the paper. I tried the printing example at http://www.telerik.com/community/code-library/wpf/gridview/radgridview-print-and-print-preview.aspx, but as my grid is too wide, it prints it on two pages.
So how can I scale the grid to the paper's width, so that the grid is not spread over multiple pages?
4 Answers, 1 is accepted
You can find the full code of the printing in the demo itself since this is not a built-in feature. For example in PrintExtensions.cs ToFixedDocument() extension method the grid is measured with infinity:
element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
element.Arrange(new Rect(new Point(0, 0), element.DesiredSize));
You can modify this to fit your needs. Kind regards,
the Telerik team
static FixedDocument ToFixedDocument(FrameworkElement element, PrintDialog dialog, PageOrientation orientation = PageOrientation.Portrait)...
// need to do this, otherwise 'Specified element is already the logical child of another element. Disconnect it first.' occurs
foreach (var col in (element as RadGridView).Columns)
col.Header = null;
dialog.PrintTicket.PageOrientation = orientation;
PrintCapabilities capabilities = dialog.PrintQueue.GetPrintCapabilities(dialog.PrintTicket);
Size pageSize = new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight);
Size extentSize = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
FixedDocument fixedDocument = new FixedDocument();
// Code to scale the grid to fit page width
// Get scale of the print wrt to screen of WPF visual
double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / element.ActualWidth, capabilities.PageImageableArea.ExtentHeight / element.ActualHeight);
// Transform the Visual to scale
element.LayoutTransform = new ScaleTransform(scale, scale);
// Get the size of the printer page
Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
// Update the layout of the visual to the printer page size.
element.Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz));
But at the line 'element.Measure(sz);' it crashed with the error: 'Layout measurement override of element 'Telerik.Windows.Controls.RadGridView' should not return NaN values as its DesiredSize.'
Do you know what is going on here?
Edit: I narrowed it down to the fact that ActualWidth of the grid seems to be 0??
Did you find a successful way to scale the grid to fit the page width?