-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathIndex.razor
More file actions
177 lines (151 loc) · 5.43 KB
/
Index.razor
File metadata and controls
177 lines (151 loc) · 5.43 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
@page "/"
<label for="uploadFiles">Select pdfs:</label>
<div class="drag-drop-zone">
<InputFile id="uploadFiles" multiple OnChange="HandleSelection" accept="application/pdf" capture="false" />
Drop pdf files here or click here to choose
</div>
@if (selectedFiles != null)
{
<ul class="list-group mt-2">
@foreach (var file in selectedFiles)
{
<li class="list-group-item">
<div class="d-flex justify-content-between align-items-center">
<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
<h3>@file.Name</h3>
<span>Size: <strong>@file.Size bytes</strong></span>
</div>
</li>
}
</ul>
}
@if(files != null)
{
if(files.Count != 0){
<button class="btn btn-danger" disabled="@(loading)" @onclick="()=> Reset()">Remove all</button>
}
<ul class="list-group mt-2">
@foreach(var file in files)
{
int index = files.IndexOf(file);
<li class="list-group-item">
<div class="d-flex justify-content-between align-items-center">
<div class="d-flex">
@if (index != 0)
{
<button class="btn" disabled="@(loading)" @onclick="() => MoveUp(file)">
<svg class="bi bi-caret-up-fill" width="1em" height="1em" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path d="M7.247 4.86l-4.796 5.481c-.566.647-.106 1.659.753 1.659h9.592a1 1 0 00.753-1.659l-4.796-5.48a1 1 0 00-1.506 0z"/>
</svg>
</button>
}
else {
<div style="width: 42px; height: 40px; padding: 1px"></div>
}
@if (index != files.Count-1)
{
<button class="btn" disabled="@(loading)" @onclick="() => MoveDown(file)">
<svg class="bi bi-caret-down-fill" width="1em" height="1em" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path d="M7.247 11.14L2.451 5.658C1.885 5.013 2.345 4 3.204 4h9.592a1 1 0 01.753 1.659l-4.796 5.48a1 1 0 01-1.506 0z"/>
</svg>
</button>
}
</div>
<h3>@file.Name</h3>
<button disabled="@(loading)" @onclick="() => RemoveFile(file)" class="btn btn-danger">-</button>
</div>
</li>
}
</ul>
}
<button class="btn btn-primary mt-2" disabled="@(loading || files.Count == 0)" @onclick="@CreatePDFClicked">
@if (loading) {
<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
}
Merge PDFs
</button>
@code {
List<FileInMemory> files = new List<FileInMemory>();
List<IFileListEntry> selectedFiles;
Boolean loading = false;
async Task HandleSelection(IFileListEntry[] files)
{
selectedFiles = new List<IFileListEntry>();
selectedFiles.AddRange(files);
foreach (IFileListEntry ifile in files)
{
if (ifile.Type != "application/pdf")
{
selectedFiles.Remove(ifile);
continue;
}
await Task.Yield();
var ms = new MemoryStream();
await ifile.Data.CopyToAsync(ms);
FileInMemory file = new FileInMemory(ms, ifile.Size, ifile.Name);
this.files.Add(file);
selectedFiles.Remove(ifile);
}
}
void RemoveFile(FileInMemory file)
{
files.Remove(file);
}
void Reset()
{
files = new List<FileInMemory>();
}
void MoveUp(FileInMemory file)
{
int index = files.IndexOf(file);
files.Remove(file);
files.Insert(index - 1, file);
}
void MoveDown(FileInMemory file)
{
int index = files.IndexOf(file);
files.Remove(file);
files.Insert(index + 1, file);
}
}
@functions {
async Task CreatePDFClicked()
{
if (files.Count == 0) return;
loading = true;
await Task.Yield();
await CreatePDF();
loading = false;
}
async Task CreatePDF()
{
PdfDocument outPdf = new PdfDocument();
foreach (FileInMemory file in files)
{
await Task.Yield();
try
{
PdfDocument document = PdfReader.Open(file.Stream, PdfDocumentOpenMode.Import);
await CopyPages(document, outPdf);
}
catch
{
await JS.InvokeAsync<object>("alert", "Corrupt pdf: " + file.Name);
loading = false;
return;
}
}
await Task.Yield();
MemoryStream stream = new MemoryStream();
outPdf.Save(stream, true);
await JS.SaveAs("Merged.pdf", stream.ToArray());
}
async Task CopyPages(PdfDocument from, PdfDocument to)
{
for (int i = 0; i < from.PageCount; i++)
{
await Task.Yield();
to.AddPage(from.Pages[i]);
}
}
}