Skip to content

Commit 04ac54d

Browse files
author
saravanan.ayyanar
committed
how_to_print_wpf_treegrid_with_pdfviewercontrol
1 parent 040000e commit 04ac54d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+3788
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0"?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
5+
</startup>
6+
</configuration>
4.19 KB
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<Application x:Class="PrintingDemo.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
StartupUri="MainWindow.xaml">
5+
<Application.Resources />
6+
</Application>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#region Copyright Syncfusion Inc. 2001 - 2018
2+
// Copyright Syncfusion Inc. 2001 - 2018. All rights reserved.
3+
// Use of this code is subject to the terms of our license.
4+
// A copy of the current license can be obtained at any time by e-mailing
5+
// licensing@syncfusion.com. Any infringement will be prosecuted under
6+
// applicable laws.
7+
#endregion
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Configuration;
11+
using System.Data;
12+
using System.Linq;
13+
using System.Threading.Tasks;
14+
using System.Windows;
15+
16+
namespace PrintingDemo
17+
{
18+
/// <summary>
19+
/// Interaction logic for App.xaml
20+
/// </summary>
21+
public partial class App : Application
22+
{
23+
public App()
24+
{
25+
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense(Syncfusion.Licensing.DemoCommon.FindLicenseKey());
26+
}
27+
}
28+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#region Copyright Syncfusion Inc. 2001-2018.
2+
// Copyright Syncfusion Inc. 2001-2018. All rights reserved.
3+
// Use of this code is subject to the terms of our license.
4+
// A copy of the current license can be obtained at any time by e-mailing
5+
// licensing@syncfusion.com. Any infringement will be prosecuted under
6+
// applicable laws.
7+
#endregion
8+
using Microsoft.Win32;
9+
using Syncfusion.Pdf;
10+
using Syncfusion.Pdf.Graphics;
11+
using Syncfusion.Pdf.Grid;
12+
using Syncfusion.Pdf.Parsing;
13+
using Syncfusion.UI.Xaml.TreeGrid;
14+
using Syncfusion.UI.Xaml.TreeGrid.Converter;
15+
using PrintingDemo.Helpers;
16+
using Syncfusion.Windows.PdfViewer;
17+
using System;
18+
using System.Collections.Generic;
19+
using System.IO;
20+
using System.Linq;
21+
using System.Text;
22+
using System.Threading.Tasks;
23+
using System.Windows;
24+
using System.Windows.Input;
25+
26+
namespace PrintingDemo
27+
{
28+
public static class Commands
29+
{
30+
static Commands()
31+
{
32+
CommandManager.RegisterClassCommandBinding(typeof(SfTreeGrid), new CommandBinding(PrintTreeGrid, OnExecutePrintTreeGrid));
33+
}
34+
35+
#region ExportToExcel Command
36+
37+
public static RoutedCommand PrintTreeGrid = new RoutedCommand("PrintTreeGrid", typeof(SfTreeGrid));
38+
39+
private static void OnExecutePrintTreeGrid(object sender, ExecutedRoutedEventArgs args)
40+
{
41+
var treeGrid = args.Source as SfTreeGrid;
42+
if (treeGrid == null) return;
43+
try
44+
{
45+
var options = new TreeGridPdfExportingOptions();
46+
options.AllowIndentColumn = true;
47+
options.FitAllColumnsInOnePage = true;
48+
49+
var document = treeGrid.ExportToPdf(options, true);
50+
51+
PdfViewerControl pdfViewer = new PdfViewerControl();
52+
MemoryStream stream = new MemoryStream();
53+
document.Save(stream);
54+
PdfLoadedDocument ldoc = new PdfLoadedDocument(stream);
55+
pdfViewer.Load(ldoc);
56+
// if you want to show the pdf viewer window. Please enable the below line,
57+
//MainWindow pdfPage = new MainWindow();
58+
//pdfPage.Content = pdfViewer;
59+
//pdfPage.Show();
60+
pdfViewer.Print(true);
61+
62+
}
63+
catch (Exception)
64+
{
65+
66+
}
67+
}
68+
69+
#endregion
70+
71+
}
72+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Syncfusion.Pdf.Grid;
2+
using Syncfusion.UI.Xaml.TreeGrid;
3+
using Syncfusion.UI.Xaml.TreeGrid.Converter;
4+
5+
namespace PrintingDemo.Helpers
6+
{
7+
public class TreeGridCustomPdfConverter : TreeGridToPdfConverter
8+
{
9+
internal bool _excludeNonExpandedNodes;
10+
public TreeGridCustomPdfConverter(bool excludeNonExpandedNodes) :base()
11+
{
12+
_excludeNonExpandedNodes = excludeNonExpandedNodes;
13+
}
14+
/// <summary>
15+
/// ExportNodes to PDF
16+
/// </summary>
17+
/// <param name="treeGrid"></param>
18+
/// <param name="nodes"></param>
19+
/// <param name="pdfGrid"></param>
20+
/// <param name="pdfExportingOptions"></param>
21+
protected override void ExportNodesToPdf(SfTreeGrid treeGrid, TreeNodes nodes, PdfGrid pdfGrid, TreeGridPdfExportingOptions pdfExportingOptions)
22+
{
23+
if (!_excludeNonExpandedNodes)
24+
{
25+
base.ExportNodesToPdf(treeGrid, nodes, pdfGrid, pdfExportingOptions);
26+
}
27+
else
28+
{
29+
for (int i = 0; i < nodes.Count; i++)
30+
{
31+
TreeNode node = nodes[i];
32+
ExportNodeToPdf(treeGrid, node, pdfGrid, pdfExportingOptions);
33+
if (node.IsExpanded && node.HasChildNodes)
34+
{
35+
node.PopulateChildNodes();
36+
ExportNodesToPdf(treeGrid, node.ChildNodes, pdfGrid, pdfExportingOptions);
37+
}
38+
}
39+
}
40+
}
41+
}
42+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Syncfusion.Pdf;
2+
using Syncfusion.UI.Xaml.TreeGrid;
3+
using Syncfusion.UI.Xaml.TreeGrid.Converter;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace PrintingDemo.Helpers
11+
{
12+
public static class TreeGridCustomPdfExportExtension
13+
{
14+
/// <summary>
15+
/// Export to PDF by excluding the non expanded nodes
16+
/// </summary>
17+
/// <param name="treeGrid"></param>
18+
/// <param name="exportingOptions"></param>
19+
/// <param name="excludeNonExpandedNodes"></param>
20+
/// <returns></returns>
21+
public static PdfDocument ExportToPdf(this SfTreeGrid treeGrid, TreeGridPdfExportingOptions exportingOptions, bool excludeNonExpandedNodes)
22+
{
23+
if (excludeNonExpandedNodes)
24+
{
25+
TreeGridCustomPdfConverter converter = new TreeGridCustomPdfConverter(excludeNonExpandedNodes);
26+
var pdfDocument = converter.ExportToPdf(treeGrid, exportingOptions);
27+
converter = null;
28+
return pdfDocument;
29+
}
30+
else
31+
return treeGrid.ExportToPdf(exportingOptions);
32+
}
33+
}
34+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<syncfusion:ChromelessWindow x:Class="PrintingDemo.MainWindow"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
5+
xmlns:local="clr-namespace:PrintingDemo"
6+
xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
7+
syncfusion:LayoutControl.SetMetroMode="True"
8+
Icon="App.ico">
9+
<syncfusion:ChromelessWindow.DataContext>
10+
<local:EmployeeDetailsRepository />
11+
</syncfusion:ChromelessWindow.DataContext>
12+
13+
<Grid>
14+
15+
<syncfusion:LayoutControl HeaderDescriptionText="This sample showcases how to print the TreeGrid content."
16+
HeaderText="Printing Demo"
17+
LayoutMode="Metro"
18+
UserOptionsVisibility="Visible">
19+
<syncfusion:LayoutControl.GridView>
20+
<syncfusion:SfTreeGrid Name="treeGrid"
21+
AutoExpandMode="RootNodesExpanded"
22+
AutoGenerateColumns="False"
23+
ChildPropertyName="ReportsTo"
24+
ItemsSource="{Binding EmployeeDetails}"
25+
ParentPropertyName="ID"
26+
SelfRelationRootValue="-1">
27+
28+
<syncfusion:SfTreeGrid.Columns>
29+
<syncfusion:TreeGridTextColumn HeaderText="First Name" MappingName="FirstName" />
30+
<syncfusion:TreeGridTextColumn HeaderText="Last Name" MappingName="LastName" />
31+
<syncfusion:TreeGridTextColumn HeaderText="Employee ID"
32+
MappingName="ID"
33+
TextAlignment="Left" />
34+
<syncfusion:TreeGridTextColumn MappingName="Title" />
35+
<syncfusion:TreeGridTextColumn MappingName="City" />
36+
<syncfusion:TreeGridCurrencyColumn CurrencyDecimalDigits="0"
37+
CurrencySymbol="$"
38+
MappingName="Salary" />
39+
<syncfusion:TreeGridTextColumn HeaderText="Reports To" MappingName="ReportsTo" />
40+
</syncfusion:SfTreeGrid.Columns>
41+
42+
</syncfusion:SfTreeGrid>
43+
44+
</syncfusion:LayoutControl.GridView>
45+
<syncfusion:LayoutControl.UserOptionsView>
46+
<syncfusion:UserOptions Grid.Column="1" HeaderText="Options">
47+
<StackPanel Margin="5">
48+
<Button Width="150"
49+
Margin="6"
50+
Command="local:Commands.PrintTreeGrid"
51+
CommandTarget="{Binding GridView,
52+
RelativeSource={RelativeSource Mode=FindAncestor,
53+
AncestorType={x:Type syncfusion:LayoutControl}}}"
54+
Content="Print TreeGrid" />
55+
</StackPanel>
56+
</syncfusion:UserOptions>
57+
</syncfusion:LayoutControl.UserOptionsView>
58+
</syncfusion:LayoutControl>
59+
</Grid>
60+
</syncfusion:ChromelessWindow>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#region Copyright Syncfusion Inc. 2001 - 2018
2+
// Copyright Syncfusion Inc. 2001 - 2018. All rights reserved.
3+
// Use of this code is subject to the terms of our license.
4+
// A copy of the current license can be obtained at any time by e-mailing
5+
// licensing@syncfusion.com. Any infringement will be prosecuted under
6+
// applicable laws.
7+
#endregion
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Linq;
11+
using System.Text;
12+
using System.Threading.Tasks;
13+
using System.Windows;
14+
using System.Windows.Controls;
15+
using System.Windows.Data;
16+
using System.Windows.Documents;
17+
using System.Windows.Input;
18+
using System.Windows.Media;
19+
using System.Windows.Media.Imaging;
20+
using System.Windows.Navigation;
21+
using System.Windows.Shapes;
22+
using Syncfusion.Windows.Shared;
23+
using Syncfusion.UI.Xaml.TreeGrid.Converter;
24+
using Syncfusion.Pdf.Grid;
25+
using Syncfusion.Pdf.Graphics;
26+
using Syncfusion.Pdf;
27+
using Microsoft.Win32;
28+
using System.IO;
29+
using System.Diagnostics;
30+
31+
namespace PrintingDemo
32+
{
33+
/// <summary>
34+
/// Interaction logic for MainWindow.xaml
35+
/// </summary>
36+
public partial class MainWindow : ChromelessWindow
37+
{
38+
public MainWindow()
39+
{
40+
InitializeComponent();
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)