-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
172 lines (152 loc) · 5.9 KB
/
Form1.cs
File metadata and controls
172 lines (152 loc) · 5.9 KB
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
using DynamicWebTWAIN.RestClient;
using DynamicWebTWAIN.Service;
using Dynamsoft.DocumentViewer;
using Microsoft.Web.WebView2.Core;
namespace WinFormsApp
{
public class WinFormsWebViewBridge : IWebViewBridge
{
private Microsoft.Web.WebView2.WinForms.WebView2 _webView;
public WinFormsWebViewBridge(Microsoft.Web.WebView2.WinForms.WebView2 webView)
{
_webView = webView;
}
public async Task<string?> ExecuteJavaScriptAsync(string script) // Change return type to string? to allow null
{
if (_webView is Control control && control.InvokeRequired)
{
string? result = null; // Change type to string? to allow null
control.Invoke(new Action(async () =>
{
result = await _webView.ExecuteScriptAsync(script);
}));
return result; // Ensure a value is returned after invocation
}
else
{
return await _webView.ExecuteScriptAsync(script);
}
}
public void RegisterCallback(Func<string, bool> callback)
{
_webView.CoreWebView2.WebMessageReceived += (sender, args) =>
{
callback?.Invoke(args.TryGetWebMessageAsString());
};
}
public async Task LoadUrlAsync(Uri url)
{
_webView.Source = url;
await Task.CompletedTask;
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private JSInterop _jsInterop;
private ServiceManager _serviceManager;
private IReadOnlyList<Scanner> _scanners;
private string productKey = "DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9";
private async void Window_Loaded(object sender, EventArgs e)
{
try
{
await webView.EnsureCoreWebView2Async();
JSInteropOptions options = new JSInteropOptions();
// @"C:\code\dev\DWT\REST\.NET\DynamicWebTWAIN.RestClient\RestService\DynamicWebTWAINService\contentFiles\any\any\dynamsoft.dwt.service.windows"
_serviceManager = new ServiceManager(); // "dynamsoft.dwt.service"
_serviceManager.CreateService();
// because we load ddv page in the service, so we should make sure the service is running, so we need to set a long timeout
// or manual create a websocket connection in js, recommend this way.
options.ProductKey = productKey;
_jsInterop = new JSInterop(options,
new WinFormsWebViewBridge(webView),
_serviceManager.Service.BaseAddress);
await _jsInterop.EnsureInitializedAsync();
_scanners = await _jsInterop.DWTClient.ScannerControlClient.ScannerManager.GetScanners(DynamicWebTWAIN.RestClient.EnumDeviceTypeMask.DT_TWAINSCANNER | EnumDeviceTypeMask.DT_WIASCANNER);
foreach (var scanner in _scanners)
{
if (scanner.Type == EnumDeviceTypeMask.DT_WIASCANNER)
{
cbxSources.Items.Add("WIA-" + scanner.Name);
}
else
{
cbxSources.Items.Add(scanner.Name);
}
}
cbxSources.SelectedIndex = 0;
}
catch (Exception ex)
{
if (this.InvokeRequired)
{
this.Invoke(new Action(() => MessageBox.Show(ex.Message)));
}
else
{
MessageBox.Show(ex.Message);
}
}
}
private void Window_Closing(object sender, FormClosingEventArgs e)
{
_jsInterop.DWTClient?.Dispose();
_serviceManager?.Dispose();
}
private async void btnSaveAsPdf_Click(object sender, EventArgs e)
{
try
{
var count = await _jsInterop.GetPageCount();
if (count == 0)
{
MessageBox.Show("There is no images in the buffer.");
return;
}
var pdf = await _jsInterop.SaveAsPdf(PageOption.All, PdfPageType.PageDefault, SaveAnnotationMode.Annotation, "");
string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "WinFormsWebViewAppOutput.pdf");
File.WriteAllBytes(filePath, pdf);
MessageBox.Show(filePath);
}
catch (Exception ex)
{
if (this.InvokeRequired)
{
this.Invoke(new Action(() => MessageBox.Show(ex.Message)));
}
else
{
MessageBox.Show(ex.Message);
}
}
}
private async void btnScanToView_Click(object sender, EventArgs e)
{
try
{
CreateScanJobOptions options = new CreateScanJobOptions();
options.Device = _scanners[cbxSources.SelectedIndex].Device;
options.Config = new ScannerConfiguration();
//options.Config.XferCount = 7;
options.Config.IfFeederEnabled = false;
options.Config.IfDuplexEnabled = false;
await _jsInterop.ScanImageToView(options);
}
catch (Exception ex)
{
if (this.InvokeRequired)
{
this.Invoke(new Action(() => MessageBox.Show(ex.Message)));
}
else
{
MessageBox.Show(ex.Message);
}
}
}
}
}