|
1 | | -# How to print wpf treegrid with pdfviewercontrol? |
2 | | -This sample illustrates how to print wpf treegrid with pdfviewercontrol |
| 1 | +# How to print wpf treegrid with pdfviewercontrol |
| 2 | + |
| 3 | +This sample illustrates how to print [WPF TreeGrid](https://www.syncfusion.com/wpf-controls/treegrid) with [PdfViewerControl](https://help.syncfusion.com/wpf/pdf-viewer/printing-pdf-files). |
| 4 | + |
| 5 | +## Default Printing |
| 6 | + |
| 7 | +The printing feature can be achieved by exporting the `TreeGrid` to PDF and printing the exported PDF using the `PdfViewerControl`. |
| 8 | + |
| 9 | +``` c# |
| 10 | +var options = new TreeGridPdfExportingOptions(); |
| 11 | +FileStream fileStream = new FileStream("Sample.pdf", FileMode.Create); |
| 12 | +var document = treeGrid.ExportToPdf(options); |
| 13 | +MemoryStream stream = new MemoryStream(); |
| 14 | +document.Save(stream); |
| 15 | +PdfViewerControl pdfViewer = new PdfViewerControl(); |
| 16 | +pdfViewer.Load(stream); |
| 17 | +Window window = new Window(); |
| 18 | +window.Content = pdfViewer; |
| 19 | +window.Loaded += Window_Loaded; |
| 20 | +window.Show(); |
| 21 | + |
| 22 | +private void Window_Loaded(object sender, RoutedEventArgs e) |
| 23 | +{ |
| 24 | + var toolbar = pdfViewer.Template.FindName("PART_Toolbar", pdfViewer) as DocumentToolbar; |
| 25 | + // Get an instance of the open and save file buttons using its template name. |
| 26 | + Button openButton = (Button)toolbar.Template.FindName("PART_ButtonOpen", toolbar); |
| 27 | + Button saveButton = (Button)toolbar.Template.FindName("PART_ButtonSave", toolbar); |
| 28 | + |
| 29 | + // Set visibility of the button to collapsed. |
| 30 | + openButton.Visibility = System.Windows.Visibility.Collapsed; |
| 31 | + saveButton.Visibility = Visibility.Collapsed; |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +## Custom Printing |
| 36 | + |
| 37 | +You can print only the parent and expanded child nodes by overriding the `ExportNodesToPdf` method of the `TreeGridToPdfConverter` class. |
| 38 | + |
| 39 | +```c# |
| 40 | +var options = new TreeGridPdfExportingOptions(); |
| 41 | +options.AllowIndentColumn = true; |
| 42 | +options.FitAllColumnsInOnePage = true; |
| 43 | + |
| 44 | +var document = treeGrid.ExportToPdf(options, true); |
| 45 | + |
| 46 | +PdfViewerControl pdfViewer = new PdfViewerControl(); |
| 47 | +MemoryStream stream = new MemoryStream(); |
| 48 | +document.Save(stream); |
| 49 | +PdfLoadedDocument ldoc = new PdfLoadedDocument(stream); |
| 50 | +pdfViewer.Load(ldoc); |
| 51 | +// If you want to show the pdf viewer window, enable the following line. |
| 52 | +MainWindow pdfPage = new MainWindow(); |
| 53 | +pdfPage.Content = pdfViewer; |
| 54 | +pdfPage.Show(); |
| 55 | +pdfViewer.Print(true); |
| 56 | + |
| 57 | +public class TreeGridCustomPdfConverter : TreeGridToPdfConverter |
| 58 | +{ |
| 59 | + internal bool _excludeNonExpandedNodes; |
| 60 | + public TreeGridCustomPdfConverter(bool excludeNonExpandedNodes) :base() |
| 61 | + { |
| 62 | + _excludeNonExpandedNodes = excludeNonExpandedNodes; |
| 63 | + } |
| 64 | + /// <summary> |
| 65 | + /// ExportNodes to PDF |
| 66 | + /// </summary> |
| 67 | + /// <param name="treeGrid"></param> |
| 68 | + /// <param name="nodes"></param> |
| 69 | + /// <param name="pdfGrid"></param> |
| 70 | + /// <param name="pdfExportingOptions"></param> |
| 71 | + protected override void ExportNodesToPdf(SfTreeGrid treeGrid, TreeNodes nodes, PdfGrid pdfGrid, TreeGridPdfExportingOptions pdfExportingOptions) |
| 72 | + { |
| 73 | + if (!_excludeNonExpandedNodes) |
| 74 | + { |
| 75 | + base.ExportNodesToPdf(treeGrid, nodes, pdfGrid, pdfExportingOptions); |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + for (int i = 0; i < nodes.Count; i++) |
| 80 | + { |
| 81 | + TreeNode node = nodes[i]; |
| 82 | + ExportNodeToPdf(treeGrid, node, pdfGrid, pdfExportingOptions); |
| 83 | + if (node.IsExpanded && node.HasChildNodes) |
| 84 | + { |
| 85 | + node.PopulateChildNodes(); |
| 86 | + ExportNodesToPdf(treeGrid, node.ChildNodes, pdfGrid, pdfExportingOptions); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
0 commit comments