-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathPdfReader.razor.cs
More file actions
385 lines (331 loc) · 11.7 KB
/
PdfReader.razor.cs
File metadata and controls
385 lines (331 loc) · 11.7 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
using Microsoft.AspNetCore.Components;
using System.Web;
using UAParser;
namespace BootstrapBlazor.Components;
/// <summary>
/// Blazor Pdf Reader PDF阅读器 组件
/// </summary>
public partial class PdfReader : IAsyncDisposable
{
[Inject]
[NotNull]
private IJSRuntime? JSRuntime { get; set; }
[NotNull]
private IJSObjectReference? Module { get; set; }
/// <summary>
/// UI界面元素的引用对象
/// </summary>
private ElementReference Element { get; set; }
/// <summary>
/// 获得/设置 用于渲染的文件流,为空则用 FileName 参数读取文件
/// </summary>
[Parameter]
public Stream? Stream { get; set; }
private byte[]? streamCache { get; set; }
/// <summary>
/// 获得/设置 PDF文件URL, 默认 http 开头自动使用流模式读取
/// </summary>
[Parameter]
public string? FileName { get; set; }
string? fileNameCache { get; set; }
/// <summary>
/// 获得/设置 使用流化模式,可跨域读取文件. 默认为 false
/// </summary>
[Parameter]
public bool StreamMode { get; set; }
/// <summary>
/// 获得/设置 宽 单位(px|%) 默认 100%
/// </summary>
[Parameter]
public string Width { get; set; } = "100%";
/// <summary>
/// 获得/设置 高 单位(px|%) 默认 500px
/// </summary>
[Parameter]
public string Height { get; set; } = "700px";
/// <summary>
/// 获得/设置 组件外观 Css Style
/// </summary>
[Parameter]
public string? StyleString { get; set; }
/// <summary>
/// 获得/设置 页码
/// </summary>
[Parameter]
public int Page { get; set; } = 1;
/// <summary>
/// 获得/设置 显示导航窗格
/// </summary>
[Parameter]
public bool Navpanes { get; set; } = true;
/// <summary>
/// 获得/设置 显示工具栏
/// </summary>
[Parameter]
public bool Toolbar { get; set; } = true;
/// <summary>
/// 获得/设置 显示状态栏
/// </summary>
[Parameter]
public bool Statusbar { get; set; } = true;
/// <summary>
/// [已过时,使用 Zoom 代替] 获得/设置 视图模式,
/// </summary>
[Parameter]
[Obsolete]
public string? View { get; set; }
/// <summary>
/// 获得/设置 页面模式
/// </summary>
[Parameter]
public EnumPageMode? Pagemode { get; set; } = EnumPageMode.Thumbs;
/// <summary>
/// 获得/设置 查询关键字
/// </summary>
[Parameter]
public string? Search { get; set; }
/// <summary>
/// 获得/设置 缩放模式 默认为 自动
/// </summary>
[Parameter]
public EnumZoomMode? Zoom { get; set; } = EnumZoomMode.Auto;
/// <summary>
/// 获得/设置 浏览器路径
/// </summary>
[Parameter]
public string ViewerBase { get; set; } = "/_content/BootstrapBlazor.PdfReader/web/viewer.html";
/// <summary>
/// 获得/设置 禁用复制/打印/下载
/// </summary>
[Parameter]
public bool ReadOnly { get; set; }
/// <summary>
/// 获得/设置 水印内容
/// </summary>
[Parameter]
public string? Watermark { get; set; }
/// <summary>
/// 获得/设置 水印内容仅在全屏演示状态显示
/// </summary>
[Parameter]
public bool WatermarkDemoModeOnly { get; set; }
/// <summary>
/// Debug
/// </summary>
[Parameter]
public bool Debug { get; set; }
/// <summary>
/// 获得/设置 'http' 开头自动使用流模式读取
/// </summary>
[Parameter]
public bool AutoStreamMode { get; set; } = true;
/// <summary>
/// 获得/设置 读取本地文件路径
/// </summary>
[Parameter]
public string? LocalFileName { get; set; }
string? localFileNameCache { get; set; }
/// <summary>
/// 获得/设置 兼容模式,兼容旧版浏览器 默认为 false
/// </summary>
[Parameter]
public bool CompatibilityMode { get; set; }
/// <summary>
/// 获得/设置 兼容模式,兼容旧版不支持es5的浏览器 默认为 false
/// <para>Compatible with older browsers that do not support ES5</para>
/// </summary>
[Parameter]
public bool CompatibilityNoneES5 { get; set; }
string? ErrorMessage { get; set; }
private string? Url { get; set; }
private string? UrlDebug { get; set; }
private UAParser.ClientInfo? ClientInfo { get; set; }
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="firstRender"></param>
/// <returns></returns>
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
Module = await JSRuntime.InvokeAsync<IJSObjectReference>("import", "./_content/BootstrapBlazor.PdfReader/app.js" + "?v=" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);
var userAgent = await Module!.InvokeAsync<string>("getUserAgent");
var parser = Parser.GetDefault();
ClientInfo = parser.Parse(userAgent);
if (!WatermarkDemoModeOnly)
{
WatermarkDemoModeOnly = await Module.InvokeAsync<bool>("getGlobalWatermark");
}
await Refresh();
}
}
/// <summary>
/// 刷新组件
/// </summary>
/// <returns></returns>
public virtual async Task Refresh() => await Refresh(null, null, null, null);
/// <summary>
/// 跳转页码
/// </summary>
/// <param name="page">页码</param>
/// <returns></returns>
public virtual async Task NavigateToPage(int page) => await Refresh(page: page);
/// <summary>
/// 刷新组件
/// </summary>
/// <param name="page">页码</param>
/// <returns></returns>
public virtual async Task Refresh(int page) => await Refresh(page: page);
/// <summary>
/// 刷新组件
/// </summary>
/// <param name="search">查询关键字</param>
/// <param name="page">页码</param>
/// <param name="pagemode">页面模式</param>
/// <param name="zoom">缩放模式</param>
/// <param name="readOnly">禁用复制/打印/下载</param>
/// <param name="watermark">水印内容</param>
/// <param name="compatibilityMode">兼容模式,兼容旧版浏览器</param>
/// <returns></returns>
public virtual async Task Refresh(string? search = null, int? page = null, EnumPageMode? pagemode = null, EnumZoomMode? zoom = null, bool? readOnly = null, string? watermark = null, bool? compatibilityMode = null)
{
ErrorMessage = null;
try
{
Search = search ?? Search;
Page = page ?? Page;
Pagemode = pagemode ?? Pagemode;
Zoom = zoom ?? Zoom;
ReadOnly = readOnly ?? ReadOnly;
Watermark = watermark ?? Watermark;
CompatibilityMode = compatibilityMode ?? CompatibilityMode;
if (CompatibilityNoneES5 || (ClientInfo != null && ClientInfo.UA.Family.StartsWith("Chrome") == true && Convert.ToInt32(ClientInfo.UA.Major) < 97))
{
CompatibilityMode = true;
ViewerBase = "/_content/BootstrapBlazor.PdfReader/compat/web/viewer.html";
}
else if (CompatibilityMode || (ClientInfo != null && ClientInfo.UA.Family.StartsWith("Chrome") == true && Convert.ToInt32(ClientInfo.UA.Major) < 109))
{
ViewerBase = "/_content/BootstrapBlazor.PdfReader/2.6.347/web/viewer.html";
}
else if (ReadOnly || readOnly != null)
{
ViewerBase = ReadOnly ? "/_content/BootstrapBlazor.PdfReader/web/viewerlimit.html" : "/_content/BootstrapBlazor.PdfReader/web/viewer.html";
}
if (Stream != null)
{
await ShowPdf(Stream);
}
else if (!string.IsNullOrEmpty(LocalFileName) && File.Exists(LocalFileName))
{
var streamLocal = new FileStream(LocalFileName, FileMode.Open, FileAccess.Read);
if (streamLocal != null)
{
await ShowPdf(streamLocal, fileNameCache != localFileNameCache, true);
localFileNameCache = LocalFileName;
}
else
{
ErrorMessage = "No data";
}
}
else if (!string.IsNullOrEmpty(FileName) && (StreamMode || (AutoStreamMode && FileName.StartsWith("http"))))
{
var client = new HttpClient();
var stream = await client.GetStreamAsync(FileName);
if (stream != null)
{
await ShowPdf(stream, fileNameCache != FileName);
fileNameCache = FileName;
}
else
{
ErrorMessage = "No data";
}
}
else
{
Url = GenUrl();
}
}
catch (Exception e)
{
ErrorMessage = e.Message;
}
StateHasChanged();
}
private string GenUrl(bool filemode = true) => $"{ViewerBase}?file={(filemode ? HttpUtility.UrlEncode(FileName) : "(1)")}#page={Page}&navpanes={(Navpanes ? 0 : 1)}&toolbar={(Toolbar ? 0 : 1)}&statusbar={(Statusbar ? 0 : 1)}&pagemode={(Pagemode ?? EnumPageMode.Thumbs).ToString().ToLower()}&search={Search}" + (Zoom != null ? $"&zoom={Zoom.GetEnumName()}" : "") + (Watermark != null ? $"&wm={Watermark}" : "") + (WatermarkDemoModeOnly ? $"&wmonlydemo=true" : "");
/// <summary>
/// 打开 LocalFileName
/// </summary>
/// <param name="localFileName"></param>
/// <returns></returns>
public virtual async Task ShowPdf(string localFileName)
{
LocalFileName = localFileName;
await Refresh();
}
/// <summary>
/// 打开 stream
/// </summary>
/// <param name="stream"></param>
/// <param name="forceLoad">default true</param>
/// <param name="islocalFile"></param>
/// <returns></returns>
public virtual async Task ShowPdf(Stream stream, bool forceLoad = true, bool islocalFile = false)
{
if (Module == null)
{
Stream = stream;
}
else if (islocalFile)
{
if (forceLoad)
{
streamCache = new byte[stream.Length];
stream.Read(streamCache, 0, (int)stream.Length);
}
if (streamCache == null)
{
streamCache = new byte[stream.Length];
stream.Read(streamCache, 0, (int)stream.Length);
}
if (streamCache != null)
{
Url = null;
var url = GenUrl(false);
UrlDebug = url;
using var streamRef = new DotNetStreamReference(new MemoryStream(streamCache));
await Module!.InvokeVoidAsync("showPdf", url, Element, streamRef);
}
}
else
{
Url = null;
var url = GenUrl(false);
UrlDebug = url;
using var streamRef = new DotNetStreamReference(stream);
await Module!.InvokeVoidAsync("showPdf", url, Element, streamRef);
}
}
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <returns></returns>
public async ValueTask DisposeAsync()
{
if (Module is not null)
{
try
{
await Module.DisposeAsync();
}
catch { }
}
GC.SuppressFinalize(this);
}
}