Skip to content

Commit 91da711

Browse files
authored
Merge pull request #2 from SyncfusionExamples/ES-975464
ES-975464 - Resolve the ReadMe file length issue in this sample repository
2 parents 9ee7b00 + a08c6de commit 91da711

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

PrintPreview.png

49.4 KB
Loading

README.md

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,92 @@
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) (SfTreeGrid) with [PdfViewerControl](https://help.syncfusion.com/wpf/pdf-viewer/printing-pdf-files).
4+
5+
The printing feature can be achieved by exporting the **TreeGrid** to PDF and printing the exported PDF using the **PdfViewerControl**.
6+
7+
``` c#
8+
var options = new TreeGridPdfExportingOptions();
9+
FileStream fileStream = new FileStream("Sample.pdf", FileMode.Create);
10+
var document = treeGrid.ExportToPdf(options);
11+
MemoryStream stream = new MemoryStream();
12+
document.Save(stream);
13+
PdfViewerControl pdfViewer = new PdfViewerControl();
14+
pdfViewer.Load(stream);
15+
Window window = new Window();
16+
window.Content = pdfViewer;
17+
window.Loaded += Window_Loaded;
18+
window.Show();
19+
20+
private void Window_Loaded(object sender, RoutedEventArgs e)
21+
{
22+
var toolbar = pdfViewer.Template.FindName("PART_Toolbar", pdfViewer) as DocumentToolbar;
23+
// Get an instance of the open and save file buttons using its template name.
24+
Button openButton = (Button)toolbar.Template.FindName("PART_ButtonOpen", toolbar);
25+
Button saveButton = (Button)toolbar.Template.FindName("PART_ButtonSave", toolbar);
26+
27+
// Set visibility of the button to collapsed.
28+
openButton.Visibility = System.Windows.Visibility.Collapsed;
29+
saveButton.Visibility = Visibility.Collapsed;
30+
}
31+
```
32+
33+
![Printing TreeGrid with PdfViewerControl](PrintPreview.png)
34+
35+
### Print Parent and Expanded Child Nodes:
36+
37+
You can print only the parent and expanded child nodes by overriding the [ExportNodesToPdf](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.TreeGrid.Converter.TreeGridToPdfConverter.html#Syncfusion_UI_Xaml_TreeGrid_Converter_TreeGridToPdfConverter_ExportNodesToPdf_Syncfusion_UI_Xaml_TreeGrid_SfTreeGrid_Syncfusion_UI_Xaml_TreeGrid_TreeNodes_Syncfusion_Pdf_Grid_PdfGrid_Syncfusion_UI_Xaml_TreeGrid_Converter_TreeGridPdfExportingOptions_) method of the [TreeGridToPdfConverter](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.TreeGrid.Converter.TreeGridToPdfConverter.html) class.
38+
39+
``` csharp
40+
var options = new TreeGridPdfExportingOptions();
41+
options.AllowIndentColumn = true;
42+
options.FitAllColumnsInOnePage = true;
43+
var document = treeGrid.ExportToPdf(options, true);
44+
PdfViewerControl pdfViewer = new PdfViewerControl();
45+
MemoryStream stream = new MemoryStream();
46+
document.Save(stream);
47+
PdfLoadedDocument ldoc = new PdfLoadedDocument(stream);
48+
pdfViewer.Load(ldoc);
49+
// If you want to show the pdf viewer window, enable the following line.
50+
MainWindow pdfPage = new MainWindow();
51+
pdfPage.Content = pdfViewer;
52+
pdfPage.Show();
53+
pdfViewer.Print(true);
54+
55+
public class TreeGridCustomPdfConverter : TreeGridToPdfConverter
56+
{
57+
internal bool _excludeNonExpandedNodes;
58+
59+
public TreeGridCustomPdfConverter(bool excludeNonExpandedNodes) :base()
60+
{
61+
_excludeNonExpandedNodes = excludeNonExpandedNodes;
62+
}
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

Comments
 (0)