-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm.cs
100 lines (81 loc) · 3.23 KB
/
Form.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
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using Aspose.Words;
namespace WordDocumentPrinterBySection
{
public partial class Form : System.Windows.Forms.Form
{
private string _fileName;
private string _printerName;
public string FileName
{
get => _fileName;
set
{
_fileName = value;
txtFileToPrint.Text = openFileDialog.FileName;
pgrPrint.Value = 0;
btnPrint.Enabled = !String.IsNullOrEmpty(_fileName.Trim());
lblStatus.Text = String.Empty;
}
}
public string PrinterName
{
get => _printerName;
set
{
_printerName = value;
txtOutputPrinter.Text = _printerName;
txtStapling.Text = PrinterHelper.GetCurrentStapling(_printerName);
}
}
public Form() {
InitializeComponent();
}
private void Form_Load(object sender, EventArgs e) {
PrinterName = printDialog.PrinterSettings.PrinterName;
}
private void btnFileToPrint_Click(object sender, EventArgs e) {
openFileDialog.ShowDialog();
}
private void openFileDialog_FileOk(object sender, CancelEventArgs e) {
FileName = openFileDialog.FileName;
if (openFileDialog.InitialDirectory == null)
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
}
private void btnPrint_Click(object sender, EventArgs e) {
Cursor = Cursors.WaitCursor;
lblStatus.Font = new System.Drawing.Font(lblStatus.Font, FontStyle.Regular);
lblStatus.ForeColor = Color.Black;
lblStatus.Text = FormMessages.WordDocumentLoadingMessage;
var printerSettings = new PrinterSettings {
PrinterName = PrinterName
};
var document = new Document(FileName);
pgrPrint.Value = 0;
pgrPrint.Maximum = document.Sections.Count;
var sections = document.Sections;
foreach (Section section in sections) {
lblStatus.Text = String.Format(FormMessages.SectionPrintingMessage, pgrPrint.Value + 1, pgrPrint.Maximum);
var tempDocument = new Document();
var tempSection = new Section(tempDocument);
tempSection.AppendContent(section);
tempDocument.Sections.Clear();
tempDocument.Sections.Add(tempSection);
tempDocument.Print(printerSettings);
++pgrPrint.Value;
}
Cursor = Cursors.Default;
lblStatus.Text = FormMessages.PrintingStartCompletedMessage.ToUpperInvariant();
lblStatus.Font = new System.Drawing.Font(lblStatus.Font, FontStyle.Bold);
lblStatus.ForeColor = Color.DarkGreen;
}
private void btnOutputPrinter_Click(object sender, EventArgs e) {
printDialog.ShowDialog();
PrinterName = printDialog.PrinterSettings.PrinterName;
}
}
}