-
Notifications
You must be signed in to change notification settings - Fork 0
/
Index.razor
273 lines (257 loc) · 10 KB
/
Index.razor
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
@page "/"
@using DevExpress.Drawing;
@using DevExpress.XtraRichEdit;
@using DevExpress.XtraRichEdit.API.Native;
@using Newtonsoft.Json;
@using DevExpress.Blazor
@inject IJSRuntime JS
<script>
window.downloadFileFromStream = async (fileName, contentStreamReference) => {
const arrayBuffer = await contentStreamReference.arrayBuffer();
const blob = new Blob([arrayBuffer]);
const url = URL.createObjectURL(blob);
const anchorElement = document.createElement('a');
anchorElement.href = url;
anchorElement.download = fileName ?? '';
anchorElement.click();
anchorElement.remove();
URL.revokeObjectURL(url);
}
</script>
<div class="container">
<div class="card mt-3">
<div class="card-header">
Word Processing Document API Font Finder
</div>
<div class="card-body px-0">
<DxFormLayout>
<DxFormLayoutItem ColSpanMd="12">
<Template>
<p class="ml-3 mr-3">
This sample detects and loads fonts used within a specified document. To detect fonts and display it in the “Fonts Used” table, select the desired document within the “Upload File” field.
</p>
</Template>
</DxFormLayoutItem>
<DxFormLayoutItem>
<Template>
<div class="input-group ml-3 mb-3">
<div class="input-group-prepend">
<span class="input-group-text">Upload File</span>
</div>
<div class="custom-file">
<InputFile OnChange="@LoadFile" class="custom-file-input"></InputFile>
<label class="custom-file-label">Choose File</label>
</div>
</div>
</Template>
</DxFormLayoutItem>
</DxFormLayout>
@if (isLoading)
{
<p class="ml-3 mb-3">Uploading...</p>
}
else
{
@if (fontNameList.Count > 0)
{
<div class="col">
<DxGrid Data="@fontNameList"
PagerVisible="false">
<DxGridDataColumn FieldName="@nameof(FontInfo.FontName)" Caption="Fonts Used within the Selected Document:" />
<DxGridDataColumn Width="300px">
<CellDisplayTemplate>
@{
var font = context.DataItem as FontInfo;
@if (font != null)
{
@if (font.IsLoaded)
{
<div>Font loaded</div>
}
else
{
<DxButton Text="Load from Google Fonts"
CssClass="btn-block"
Click="@((MouseEventArgs args) => LoadFont(font))" />
}
}
}
</CellDisplayTemplate>
</DxGridDataColumn>
</DxGrid>
<DxFormLayout>
<DxFormLayoutItem ColSpanMd="2">
<Template>
<DxButton Text="Download as PDF"
CssClass="btn-block mt-3"
Click="@((MouseEventArgs args) => DownloadAsPdf())" />
</Template>
</DxFormLayoutItem>
<DxFormLayoutItem ColSpanMd="7">
</DxFormLayoutItem>
<DxFormLayoutItem ColSpanMd="3">
<Template>
<DxButton Text="Load All Fonts from Google Fonts"
CssClass="btn-block mt-3"
Click="@((MouseEventArgs args) => LoadAllFonts())" />
</Template>
</DxFormLayoutItem>
</DxFormLayout>
</div>
}
}
</div>
</div>
</div>
@code {
bool isLoading;
RichEditDocumentServer server = new();
List<FontInfo> fontNameList = new List<FontInfo>();
async Task LoadFile(InputFileChangeEventArgs e)
{
isLoading = true;
await using MemoryStream fs = new();
await e.File.OpenReadStream().CopyToAsync(fs);
server.LoadDocument(fs);
GetDocumentFontList();
isLoading = false;
}
void LoadAllFonts()
{
foreach (var font in fontNameList)
LoadFont(font);
}
void LoadFont(FontInfo font)
{
byte[]? fontData = FontLoader.LoadFontFromGoogle(font.FontName);
if (fontData != null && fontData.Length != 0)
{
DXFontRepository.Instance.AddFont(fontData);
font.IsLoaded = true;
}
}
async Task DownloadAsPdf()
{
var outputStream = new MemoryStream();
server.ExportToPdf(outputStream);
outputStream.Position = 0;
using var streamRef = new DotNetStreamReference(outputStream);
await JS.InvokeVoidAsync("downloadFileFromStream", "result.pdf", streamRef);
}
void GetDocumentFontList()
{
DocumentFontsVisitor visitor = new DocumentFontsVisitor(fontNameList);
DocumentIterator iterator = new DocumentIterator(server.Document, true);
while (iterator.MoveNext())
iterator.Current.Accept(visitor);
}
public class FontInfo
{
public FontInfo(string fontName)
{
FontName = fontName;
}
public string FontName { get; private set; }
public bool IsLoaded { get; set; }
}
public class DocumentFontsVisitor : DocumentVisitorBase
{
List<FontInfo> fonts;
public DocumentFontsVisitor(List<FontInfo> fonts)
{
this.fonts = fonts;
}
public override void Visit(DocumentText text)
{
AddToList(text.TextProperties.FontName);
}
public override void Visit(DocumentParagraphEnd paragraphEnd)
{
AddToList(paragraphEnd.TextProperties.FontName);
}
void AddToList(string fontName)
{
if (fonts.Find(f => f.FontName == fontName) == null)
fonts.Add(new FontInfo(fontName));
}
}
public class FontLoader
{
const string apiKey = "your-api-key";
const string fontApiUrl = $"https://www.googleapis.com/webfonts/v1/webfonts/?key={apiKey}";
public static byte[]? LoadFontFromGoogle(string fontName)
{
string fontUrl = $"{fontApiUrl}&family={fontName}";
using (HttpClient client = new HttpClient())
{
try
{
return Task.Run(async () =>
{
HttpResponseMessage response = await client.GetAsync(fontUrl);
if (!response.IsSuccessStatusCode)
{
Console.WriteLine(response.StatusCode);
return null;
}
string content = await response.Content.ReadAsStringAsync();
MyFontList? webfontList = JsonConvert.DeserializeObject<MyFontList>(content);
if (webfontList is not null)
return LoadFontFile(webfontList.Items[0].Files.Regular);
return null;
}).GetAwaiter().GetResult();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
return null;
}
static byte[]? LoadFontFile(string fontUrl)
{
using (HttpClient client = new HttpClient())
{
try
{
return Task.Run(async () =>
{
HttpResponseMessage response = await client.GetAsync(fontUrl);
if (!response.IsSuccessStatusCode)
{
Console.WriteLine(response.StatusCode);
return null;
}
using (MemoryStream fileStream = new MemoryStream())
{
await response.Content.CopyToAsync(fileStream);
byte[] result = new byte[fileStream.Length];
fileStream.Position = 0;
fileStream.Read(result, 0, (int)fileStream.Length);
return result;
}
}).GetAwaiter().GetResult();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
return null;
}
private class MyFont
{
public string Family { get; set; }
public string Menu { get; set; }
public Files Files { get; set; }
}
private class MyFontList
{
public List<MyFont> Items { get; set; }
}
private class Files
{
public string Regular { get; set; }
}
}
}