or
Table table = new Table();//Payment Details Title **Span spanPaymentDetails = new Span("Payment Details");spanPaymentDetails.StyleName = "styleTableTitle14";Paragraph paragraphPaymentDetails = new Paragraph();paragraphPaymentDetails.Inlines.Add(spanPaymentDetails);TableCell cellPaymentDetails = new TableCell();cellPaymentDetails.Background = Colors.LightGray;cellPaymentDetails.Blocks.Add(paragraphPaymentDetails);//BACSSpan spanBACS = new Span("BACS:");spanBACS.StyleName = "styleTableContent14";spanBACS.FontStyle = FontStyles.Italic;Paragraph paragraphBACS = new Paragraph();paragraphBACS.Inlines.Add(spanBACS);TableCell cellBACS = new TableCell();cellBACS.Background = Colors.LightGray;cellBACS.Blocks.Add(paragraphBACS);//BankSpan spanBankTitle = new Span("Bank: ");spanBankTitle.StyleName = "styleTableContent14";spanBankTitle.FontStyle = FontStyles.Italic;Span spanBankDetail = new Span("HSBC");spanBankDetail.StyleName = "styleTableContent14";Paragraph paragraphBank = new Paragraph();paragraphBank.Inlines.Add(spanBankTitle);paragraphBank.Inlines.Add(spanBankDetail);TableCell cellBank = new TableCell();cellBank.Background = Colors.Transparent;cellBank.Blocks.Add(paragraphBank);//Account NameSpan spanAccountNameTitle = new Span("Account Name: ");spanAccountNameTitle.StyleName = "styleTableContent14";spanAccountNameTitle.FontStyle = FontStyles.Italic;Span spanAccountNameDetail = new Span("My Company Name");spanAccountNameDetail.StyleName = "styleTableContent14";Paragraph paragraphAccountName = new Paragraph();paragraphAccountName.Inlines.Add(spanAccountNameTitle);paragraphAccountName.Inlines.Add(spanAccountNameDetail);TableCell cellAccountName = new TableCell();cellAccountName.Background = Colors.LightGray;cellAccountName.Blocks.Add(paragraphAccountName);//Sort CodeSpan spanSortCodeTitle = new Span("Sort Code: ");spanSortCodeTitle.StyleName = "styleTableContent14";spanSortCodeTitle.FontStyle = FontStyles.Italic;Span spanSortCodeDetail = new Span("22-11-33");spanSortCodeDetail.StyleName = "styleTableContent14";Paragraph paragraphSortCode = new Paragraph();paragraphSortCode.Inlines.Add(spanSortCodeTitle);paragraphSortCode.Inlines.Add(spanSortCodeDetail);TableCell cellSortCode = new TableCell();cellSortCode.Background = Colors.LightGray;cellSortCode.Blocks.Add(paragraphSortCode);//Account No.Span spanAccountNoTitle = new Span("Account No. ");spanAccountNoTitle.StyleName = "styleTableContent14";spanAccountNoTitle.FontStyle = FontStyles.Italic;Span spanAccountNoDetail = new Span("xxxx xxxx");spanAccountNoDetail.StyleName = "styleTableContent14";Paragraph paragraphAccountNo = new Paragraph();paragraphAccountNo.Inlines.Add(spanAccountNoTitle);paragraphAccountNo.Inlines.Add(spanAccountNoDetail);TableCell cellAccountNo = new TableCell();cellAccountNo.Background = Colors.LightGray;cellAccountNo.Blocks.Add(paragraphAccountNo);//ChequeSpan spanCheque = new Span("Cheque:");spanCheque.StyleName = "styleTableContent14";Paragraph paragraphCheque = new Paragraph();paragraphCheque.Inlines.Add(spanCheque);TableCell cellCheque = new TableCell();cellCheque.Background = Colors.LightGray;cellCheque.Blocks.Add(paragraphCheque);//Cheque Made Payable ToSpan spanChequeMadePayableTo = new Span("Made Payable To:");spanChequeMadePayableTo.StyleName = "styleTableContent14";Paragraph paragraphChequeMadePayableTo = new Paragraph();paragraphChequeMadePayableTo.Inlines.Add(spanChequeMadePayableTo);TableCell cellChequeMadePayableTo = new TableCell();cellChequeMadePayableTo.Background = Colors.LightGray;cellChequeMadePayableTo.Blocks.Add(paragraphChequeMadePayableTo);//Cheque DetailsSpan spanChequeDetail = new Span("My Company Name");spanChequeDetail.StyleName = "styleTableContent14";Paragraph paragraphChequeDetail = new Paragraph();paragraphChequeDetail.Inlines.Add(spanChequeDetail);TableCell cellChequeDetail = new TableCell();cellChequeDetail.Background = Colors.LightGray;cellChequeDetail.Blocks.Add(paragraphChequeDetail);//Add Cells To Table RowTableRow rowTitle = new TableRow();rowTitle.Cells.Add(cellPaymentDetails);table.AddRow(rowTitle);TableRow rowDetails = new TableRow();rowDetails.Cells.Add(cellBACS);rowDetails.Cells.Add(cellBank);rowDetails.Cells.Add(cellAccountName);rowDetails.Cells.Add(cellCheque);rowDetails.Cells.Add(cellChequeMadePayableTo);table.AddRow(rowDetails);TableRow rowDetails2 = new TableRow();rowDetails2.Cells.Add(new TableCell());rowDetails2.Cells.Add(cellSortCode);rowDetails2.Cells.Add(cellAccountNo);rowDetails2.Cells.Add(new TableCell());rowDetails2.Cells.Add(cellChequeDetail);table.AddRow(rowDetails2);//Change Cell Paddingtable.CellPadding = new Padding(5, 3, 5, 3);//Create Document & Apply StylesRadDocument document = new RadDocument();RadDocumentStyles.AddStylesToDocumentRepository(document);Section section = new Section();//Add Empty ParagraphParagraph paragraph = new Paragraph();paragraph.StyleName = "paragraphMailMergeTable";section.Blocks.Add(paragraph);//Add Tablesection.Blocks.Add(table);//Add Empty ParagraphParagraph paragraph1 = new Paragraph();paragraph1.StyleName = "paragraphMailMergeTable";section.Blocks.Add(paragraph1);//Add Section To Documentdocument.Sections.Add(section);document.MeasureAndArrangeInDefaultSize();return new DocumentFragment(document);Kindly find the Below Code, this is the PrintExtensions Class that Prints an image of the Gridview data,
however on large amount of data, it takes maybe half an hour without doing anything and in the same time it consumes a lot of CPU memory.
but in small amount of data it works very fine.
Any solution?
This is the Build of the rad controls I am using (RadControls for WPF, v.2012.2.1001.40)
Best Regards
public static class PrintExtensions { static FixedDocument ToFixedDocument(FrameworkElement element, PrintDialog dialog) { dialog.PrintTicket.PageOrientation = PageOrientation.Landscape; 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(); element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); element.Arrange(new Rect(new Point(0, 0), element.DesiredSize)); for (double y = 0; y < element.DesiredSize.Height; y += extentSize.Height) { for (double x = 0; x < element.DesiredSize.Width; x += extentSize.Width) { VisualBrush brush = new VisualBrush(element); brush.Stretch = Stretch.None; brush.AlignmentX = AlignmentX.Left; brush.AlignmentY = AlignmentY.Top; brush.ViewboxUnits = BrushMappingMode.Absolute; brush.TileMode = TileMode.None; brush.Viewbox = new Rect(x, y, extentSize.Width, extentSize.Height); PageContent pageContent = new PageContent(); FixedPage page = new FixedPage(); ((IAddChild)pageContent).AddChild(page); fixedDocument.Pages.Add(pageContent); page.Width = pageSize.Width; page.Height = pageSize.Height; Canvas canvas = new Canvas(); FixedPage.SetLeft(canvas, capabilities.PageImageableArea.OriginWidth); FixedPage.SetTop(canvas, capabilities.PageImageableArea.OriginHeight); canvas.Width = extentSize.Width; canvas.Height = extentSize.Height; canvas.Background = brush; page.Children.Add(canvas); } } return fixedDocument; } static GridViewDataControl ToPrintFriendlyGrid(GridViewDataControl source) { var grid = new RadGridView() { ItemsSource = source.ItemsSource, RowIndicatorVisibility = Visibility.Collapsed, ShowGroupPanel = false, CanUserFreezeColumns = false, IsFilteringAllowed = false, EnableColumnVirtualization=false, AutoExpandGroups = true, AutoGenerateColumns = false, Margin=new Thickness(10,30,10,10), ShowColumnFooters=true, EnableRowVirtualization=true }; double widthsum = 0; foreach (var column in source.Columns.OfType<GridViewDataColumn>()) { widthsum += column.Width.DisplayValue; } foreach (var column in source.Columns.OfType<GridViewDataColumn>()) { GridViewDataColumn newColumn = new GridViewDataColumn(); newColumn.Width = column.ActualWidth; newColumn.DisplayIndex = column.DisplayIndex; //newColumn.DataMemberBinding = new System.Windows.Data.Binding(column.UniqueName); newColumn.DataMemberBinding = column.DataMemberBinding; // Better to just copy the references to get all the custom formatting newColumn.DataFormatString = column.DataFormatString; newColumn.TextAlignment = column.TextAlignment; newColumn.Header = column.Header; newColumn.Footer = column.Footer; newColumn.Width = 1030/ widthsum * column.Width.DisplayValue; if (column.DataType.Name == "Decimal") { //newColumn.TextAlignment = TextAlignment.Right; SumFunction sum = new SumFunction { ResultFormatString = "Total : {0}", Caption = "" }; sum.ResultFormatString = "{0:n}"; newColumn.AggregateFunctions.Add(sum); newColumn.FooterTextAlignment = TextAlignment.Right; } else { newColumn.TextWrapping = TextWrapping.Wrap; } grid.Columns.Add(newColumn); } StyleManager.SetTheme(grid, StyleManager.GetTheme(grid)); grid.SortDescriptors.AddRange(source.SortDescriptors); grid.GroupDescriptors.AddRange(source.GroupDescriptors); grid.FilterDescriptors.AddRange(source.FilterDescriptors); return grid; } public enum ZoomType { Full, Width, Height, TwoWide }; private static double constrain(double val, double val_min, double val_max) { if (val < val_min) return val_min; else if (val > val_max) return val_max; else return val; } public static void PrintPreview(this GridViewDataControl source,string title) { Window window = new Window(); window.Title = "Print Preview"; if (!string.IsNullOrWhiteSpace(source.ToolTip as string)) window.Title += " of " + source.ToolTip; window.Width = SystemParameters.PrimaryScreenWidth * 0.92; window.Height = SystemParameters.WorkArea.Height; window.Left = constrain(SystemParameters.VirtualScreenWidth - SystemParameters.PrimaryScreenWidth, 0, SystemParameters.VirtualScreenWidth - 11); window.Top = constrain(0, 0, SystemParameters.VirtualScreenHeight - 25); DocumentViewer viewer = new DocumentViewer(); viewer.Document = ToFixedDocument(ToPrintFriendlyGrid(source), new PrintDialog()); Zoom(viewer, ZoomType.Full); window.Content = viewer; window.Show(); } public static void Zoom(DocumentViewer viewer, ZoomType zoom) { switch (zoom) { case ZoomType.Height: viewer.FitToHeight(); break; case ZoomType.Width: viewer.FitToWidth(); break; case ZoomType.TwoWide: viewer.FitToMaxPagesAcross(2); break; case ZoomType.Full: break; } } public static void Print(this GridViewDataControl source, bool showDialog, string title) { var dialog = new PrintDialog(); var dialogResult = showDialog ? dialog.ShowDialog() : true; if (dialogResult == true) { var viewer = new DocumentViewer(); Grid maingrid = new Grid(); ColumnDefinition gridCol1 = new ColumnDefinition(); maingrid.ColumnDefinitions.Add(gridCol1); RowDefinition gridRow1 = new RowDefinition(); RowDefinition gridRow2 = new RowDefinition(); maingrid.RowDefinitions.Add(gridRow1); maingrid.RowDefinitions.Add(gridRow2); TextBlock txtBlock1 = new TextBlock(); txtBlock1.Text = title; txtBlock1.Margin = new Thickness(2, 2, 2, 2); txtBlock1.TextAlignment = TextAlignment.Center; Grid.SetRow(txtBlock1, 0); Grid.SetRow(ToPrintFriendlyGrid(source), 1); maingrid.Children.Add(txtBlock1); maingrid.Children.Add(ToPrintFriendlyGrid(source)); viewer.Document = ToFixedDocument(maingrid, dialog); dialog.PrintDocument(viewer.Document.DocumentPaginator, ""); } } }