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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Packaging;
using System.Printing;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Documents.Serialization;
using System.Windows.Media;
using System.Windows.Xps;
using System.Windows.Xps.Packaging;
using System.Windows.Xps.Serialization;
namespace gsview
{
/* Class for handling async print progress callback */
public class gsPrintEventArgs : EventArgs
{
private String m_status;
private bool m_completed;
private int m_page;
public String Status
{
get { return m_status; }
}
public bool Completed
{
get { return m_completed; }
}
public int Page
{
get { return m_page; }
}
public gsPrintEventArgs(String status, bool completed, int page)
{
m_completed = completed;
m_status = status;
m_page = page;
}
}
public class gsprint
{
private XpsDocumentWriter m_docWriter = null;
internal delegate void AsyncPrintCallBack(object printObject, gsPrintEventArgs info);
internal event AsyncPrintCallBack PrintUpdate;
private bool m_busy;
public bool IsBusy()
{
return m_busy;
}
public gsprint()
{
m_busy = false;
}
/* Show std. print dialog */
public PrintDialog GetPrintDialog()
{
PrintDialog dlg = new PrintDialog();
/* Current page and page ranges is going to require a little work */
dlg.PageRangeSelection = PageRangeSelection.AllPages;
//dlg.UserPageRangeEnabled = true;
//dlg.CurrentPageEnabled = true;
dlg.SelectedPagesEnabled = false;
m_busy = false;
if (dlg.ShowDialog() == true)
return dlg;
return null;
}
/* Main print entry point */
public void Print(PrintQueue queu, FixedDocumentSequence fixdoc)
{
XpsDocumentWriter docwrite = GetDocWriter(queu);
m_busy = true;
docwrite.WritingPrintTicketRequired +=
new WritingPrintTicketRequiredEventHandler(PrintTicket);
PrintPages(docwrite, fixdoc);
}
/* Send it */
private void PrintPages(XpsDocumentWriter xpsdw, FixedDocumentSequence fixdoc)
{
m_docWriter = xpsdw;
xpsdw.WritingCompleted +=
new WritingCompletedEventHandler(AsyncCompleted);
xpsdw.WritingProgressChanged +=
new WritingProgressChangedEventHandler(AsyncProgress);
xpsdw.WriteAsync(fixdoc);
}
public void CancelAsync()
{
/* ick. This does not work in windows 8. causes crash */
/* https://connect.microsoft.com/VisualStudio/feedback/details/778145/xpsdocumentwriter-cancelasync-cause-crash-in-win8 */
m_docWriter.CancelAsync();
}
/* Done */
private void AsyncCompleted(object sender, WritingCompletedEventArgs e)
{
string status = null;
if (e.Cancelled)
status = "Print Canceled";
else if (e.Error != null)
status = "Print Error";
else
status = "Print Completed";
if (PrintUpdate != null)
{
gsPrintEventArgs info = new gsPrintEventArgs(status, true, 0);
PrintUpdate(this, info);
}
m_busy = false;
}
/* Do this update with each fixed document (page) that is handled */
private void AsyncProgress(object sender, WritingProgressChangedEventArgs e)
{
if (PrintUpdate != null)
{
String progress = "Page " + e.Number;
gsPrintEventArgs info = new gsPrintEventArgs(progress, false, e.Number);
PrintUpdate(this, info);
}
}
/* Print ticket handling. You can customize for PrintTicketLevel at
FixedDocumentSequencePrintTicket, FixedDocumentPrintTicket,
or FixedPagePrintTicket. We may want to play around with this some */
private void PrintTicket(Object sender, WritingPrintTicketRequiredEventArgs e)
{
if (e.CurrentPrintTicketLevel ==
PrintTicketLevel.FixedDocumentSequencePrintTicket)
{
PrintTicket pts = new PrintTicket();
pts.PageOrientation = PageOrientation.Portrait;
e.CurrentPrintTicket = pts;
}
}
/* Create the document write */
private XpsDocumentWriter GetDocWriter(PrintQueue pq)
{
XpsDocumentWriter xpsdw = PrintQueue.CreateXpsDocumentWriter(pq);
return xpsdw;
}
}
}
|