-
Notifications
You must be signed in to change notification settings - Fork 4
/
PrintPage.xaml.cs
124 lines (112 loc) · 4.83 KB
/
PrintPage.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
namespace Mvvm.Services
{
public partial class PrintPage : Page
{
static int _pageNumber = 0;
Grid _printArea;
private PageNumbering _pageNumbering;
public PrintPage(FrameworkElement content, FrameworkElement header = null, FrameworkElement footer = null, PageNumbering pageNumbering = PageNumbering.None)
{
_printArea = new Grid();
//// _printArea.Background = new SolidColorBrush(Color.FromArgb(128, 128, 128, 128)); // TEMP, just for testing
_printArea.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
_printArea.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
_printArea.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
_printArea.Children.Add(new Canvas() { Width = 10000 }); // Horizontally stretches the Grid.
Content = _printArea;
PageNumbering = pageNumbering;
AddContent(content);
Header = header;
Footer = footer;
}
public static int StartPageNumber
{ set { _pageNumber = value - 1; } }
public void AddContent(FrameworkElement content)
{
Grid.SetRow(content, 1);
_printArea.Children.Add(content);
if (PageNumbering != PageNumbering.None)
{
_pageNumber += 1;
var pageNumberText = new TextBlock() { Text = _pageNumber.ToString() };
switch (PageNumbering)
{
case PageNumbering.None:
break;
case PageNumbering.TopLeft:
Grid.SetRow(pageNumberText, 0);
pageNumberText.Margin = new Thickness(0, 0, 0, 20);
_printArea.Children.Add(pageNumberText);
break;
case PageNumbering.TopMiddle:
Grid.SetRow(pageNumberText, 0);
pageNumberText.Margin = new Thickness(0, 0, 0, 20);
pageNumberText.HorizontalAlignment = HorizontalAlignment.Stretch;
pageNumberText.HorizontalTextAlignment = TextAlignment.Center;
_printArea.Children.Add(pageNumberText);
break;
case PageNumbering.TopRight:
Grid.SetRow(pageNumberText, 0);
Grid.SetColumn(pageNumberText, 1);
pageNumberText.Margin = new Thickness(0, 0, 0, 20);
pageNumberText.HorizontalAlignment = HorizontalAlignment.Stretch;
pageNumberText.HorizontalTextAlignment = TextAlignment.Right;
_printArea.Children.Add(pageNumberText);
break;
case PageNumbering.BottomLeft:
Grid.SetRow(pageNumberText, 2);
pageNumberText.Margin = new Thickness(0, 20, 0, 0);
_printArea.Children.Add(pageNumberText);
break;
case PageNumbering.BottomMidle:
Grid.SetRow(pageNumberText, 2);
pageNumberText.Margin = new Thickness(0, 20, 0, 0);
pageNumberText.HorizontalAlignment = HorizontalAlignment.Stretch;
pageNumberText.HorizontalTextAlignment = TextAlignment.Center;
_printArea.Children.Add(pageNumberText);
break;
case PageNumbering.BottomRight:
Grid.SetRow(pageNumberText, 2);
pageNumberText.Margin = new Thickness(0, 20, 0, 0);
_printArea.Children.Add(pageNumberText);
break;
default:
break;
}
}
}
public FrameworkElement Header
{
set
{
if (value != null)
{
var header = value.DeepClone();
Grid.SetRow(header, 0);
_printArea.Children.Add(header);
}
}
}
public FrameworkElement Footer
{
set
{
if (value != null)
{
var footer = value.DeepClone();
Grid.SetRow(footer, 2);
_printArea.Children.Add(footer);
}
}
}
public PageNumbering PageNumbering
{
get { return _pageNumbering; }
set { _pageNumbering = value; }
}
}
}