-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFormScan.cs
213 lines (182 loc) · 7.06 KB
/
FormScan.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NTwain;
using NTwain.Data;
using System.Reflection;
using System.Drawing.Imaging;
using tsText = iTextSharp.text;
using tsPdf = iTextSharp.text.pdf;
namespace DSA_lims
{
public partial class FormScan : Form
{
DSASettings mSettings = null;
TwainSession session = null;
DataSource dataSource = null;
List<Image> images = new List<Image>();
public byte[] PdfData = null;
public string DocumentName = String.Empty;
public FormScan(DSASettings s)
{
InitializeComponent();
mSettings = s;
}
private void FormScan_Load(object sender, EventArgs e)
{
var appId = TWIdentity.CreateFromAssembly(DataGroups.Image, Assembly.GetExecutingAssembly());
session = new TwainSession(appId);
session.TransferReady += Session_TransferReady;
session.DataTransferred += Session_DataTransferred;
session.TransferError += Session_TransferError;
session.SourceDisabled += Session_SourceDisabled;
session.Open();
foreach (DataSource ds in session)
cboxScanner.Items.Add(ds.Name);
cboxScanner.Text = mSettings.ScannerName;
cbDuplex.Checked = mSettings.ScannerDuplex;
cboxFlipType.Text = mSettings.ScannerFlipType;
// FIXME: Allow BW pixel type
//cboxPixelType.Text = mSettings.ScannerPixelType;
cboxPixelType.Text = "Color";
cboxPixelType.Enabled = false;
}
private void btnScan_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(cboxScanner.Text))
{
MessageBox.Show("You must set a scanner");
return;
}
if (String.IsNullOrEmpty(cboxFlipType.Text))
{
MessageBox.Show("You must set a orientation");
return;
}
if (String.IsNullOrEmpty(cboxPixelType.Text))
{
MessageBox.Show("You must set a color");
return;
}
if (String.IsNullOrEmpty(tbFileName.Text.Trim()))
{
MessageBox.Show("You must set a document name");
return;
}
try
{
DocumentName = tbFileName.Text.Trim();
btnPreview.Enabled = false;
btnStore.Enabled = false;
PdfData = null;
images.Clear();
if (dataSource != null && dataSource.IsOpen)
dataSource.Close();
dataSource = session.FirstOrDefault(x => x.Name == cboxScanner.Text);
dataSource.Open();
dataSource.Capabilities.CapFeederEnabled.SetValue(BoolType.True);
dataSource.Capabilities.CapDuplexEnabled.SetValue(cbDuplex.Checked ? BoolType.True : BoolType.False);
dataSource.Capabilities.ICapSupportedSizes.SetValue(SupportedSize.None);
dataSource.Capabilities.ICapFlipRotation.SetValue(cboxFlipType.Text == "Fanfold" ? FlipRotation.Fanfold : FlipRotation.Book);
dataSource.Capabilities.ICapPixelType.SetValue(cboxPixelType.Text == "Black and White" ? PixelType.BlackWhite : PixelType.RGB);
dataSource.Capabilities.ICapImageFileFormat.SetValue(FileFormat.Png);
dataSource.Enable(SourceEnableMode.NoUI, true, this.Handle);
mSettings.ScannerName = cboxScanner.Text;
mSettings.ScannerDuplex = cbDuplex.Checked;
mSettings.ScannerFlipType = cboxFlipType.Text;
mSettings.ScannerPixelType = cboxPixelType.Text;
}
catch(Exception ex)
{
Common.Log.Error("Scanning of document failed on scanner " + cboxScanner.Text, ex);
}
}
private void Session_SourceDisabled(object sender, EventArgs e)
{
try
{
var document = new tsText.Document(tsText.PageSize.A4);
var output = new MemoryStream();
var writer = tsPdf.PdfWriter.GetInstance(document, output);
document.Open();
foreach (Image img in images)
{
var pdfImg = tsText.Image.GetInstance(img, ImageFormat.Jpeg);
pdfImg.ScaleToFit(document.PageSize.Width, document.PageSize.Height);
pdfImg.SpacingBefore = 0f;
pdfImg.SpacingAfter = 0f;
pdfImg.Alignment = tsText.Element.ALIGN_LEFT;
pdfImg.SetAbsolutePosition(0, 0);
document.Add(pdfImg);
document.NewPage();
}
document.Close();
PdfData = output.ToArray();
btnPreview.Invoke((MethodInvoker)delegate
{
btnPreview.Enabled = true;
});
btnStore.Invoke((MethodInvoker)delegate
{
btnStore.Enabled = true;
});
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Session_TransferError(object sender, TransferErrorEventArgs e)
{
MessageBox.Show(e.Exception.Message);
}
private void Session_DataTransferred(object sender, DataTransferredEventArgs e)
{
Image img = Image.FromStream(e.GetNativeImageStream());
images.Add(img);
}
private void Session_TransferReady(object sender, TransferReadyEventArgs e)
{
}
private void btnCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void FormScan_FormClosing(object sender, FormClosingEventArgs e)
{
if (dataSource != null && dataSource.IsOpen)
dataSource.Close();
if (session != null)
session.Close();
}
private void btnPreview_Click(object sender, EventArgs e)
{
if (PdfData != null)
{
try
{
string p = Path.GetTempFileName() + ".pdf";
File.WriteAllBytes(p, PdfData);
System.Diagnostics.Process.Start(p);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void btnStore_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
Close();
}
}
}