1
+ using Microsoft . AspNetCore . Mvc ;
2
+ using Microsoft . Extensions . Caching . Memory ;
3
+ using Syncfusion . EJ2 . PdfViewer ;
4
+ using Newtonsoft . Json ;
5
+ using Microsoft . AspNetCore . Mvc . RazorPages ;
6
+ using System . Reflection ;
7
+ using System . Net ;
8
+
9
+ namespace PDFViewerSample . Pages
10
+ {
11
+ [ IgnoreAntiforgeryToken ( Order = 1001 ) ]
12
+ public class IndexModel : PageModel
13
+ {
14
+
15
+ private readonly Microsoft . AspNetCore . Hosting . IHostingEnvironment _hostingEnvironment ;
16
+ private IMemoryCache _cache ;
17
+
18
+ public IndexModel ( Microsoft . AspNetCore . Hosting . IHostingEnvironment hostingEnvironment , IMemoryCache cache )
19
+ {
20
+ _hostingEnvironment = hostingEnvironment ;
21
+ _cache = cache ;
22
+ }
23
+
24
+ public IActionResult OnPostLoad ( [ FromBody ] jsonObjects responseData )
25
+ {
26
+ PdfRenderer pdfviewer = new PdfRenderer ( _cache ) ;
27
+ MemoryStream stream = new MemoryStream ( ) ;
28
+ var jsonObject = JsonConverterstring ( responseData ) ;
29
+ object jsonResult = new object ( ) ;
30
+ if ( jsonObject != null && jsonObject . ContainsKey ( "document" ) )
31
+ {
32
+ if ( bool . Parse ( jsonObject [ "isFileName" ] ) )
33
+ {
34
+ string documentPath = GetDocumentPath ( jsonObject [ "document" ] ) ;
35
+ if ( ! string . IsNullOrEmpty ( documentPath ) )
36
+ {
37
+ byte [ ] bytes = System . IO . File . ReadAllBytes ( documentPath ) ;
38
+ stream = new MemoryStream ( bytes ) ;
39
+ }
40
+ else
41
+ {
42
+ string fileName = jsonObject [ "document" ] . Split ( new string [ ] { "://" } , StringSplitOptions . None ) [ 0 ] ;
43
+ if ( fileName == "http" || fileName == "https" )
44
+ {
45
+ WebClient WebClient = new WebClient ( ) ;
46
+ byte [ ] pdfDoc = WebClient . DownloadData ( jsonObject [ "document" ] ) ;
47
+ stream = new MemoryStream ( pdfDoc ) ;
48
+ }
49
+ else
50
+ return this . Content ( jsonObject [ "document" ] + " is not found" ) ;
51
+ }
52
+ }
53
+ else
54
+ {
55
+ byte [ ] bytes = Convert . FromBase64String ( jsonObject [ "document" ] ) ;
56
+ stream = new MemoryStream ( bytes ) ;
57
+ }
58
+ }
59
+ jsonResult = pdfviewer . Load ( stream , jsonObject ) ;
60
+ return Content ( JsonConvert . SerializeObject ( jsonResult ) ) ;
61
+ }
62
+
63
+ public Dictionary < string , string > JsonConverterstring ( jsonObjects results )
64
+ {
65
+ Dictionary < string , object > resultObjects = new Dictionary < string , object > ( ) ;
66
+ resultObjects = results . GetType ( ) . GetProperties ( BindingFlags . Instance | BindingFlags . Public )
67
+ . ToDictionary ( prop => prop . Name , prop => prop . GetValue ( results , null ) ) ;
68
+ var emptyObjects = ( from kv in resultObjects
69
+ where kv . Value != null
70
+ select kv ) . ToDictionary ( kv => kv . Key , kv => kv . Value ) ;
71
+ Dictionary < string , string > jsonResult = emptyObjects . ToDictionary ( k => k . Key , k => k . Value . ToString ( ) ) ;
72
+ return jsonResult ;
73
+ }
74
+
75
+ //Post action for processing the PDF documents.
76
+ public IActionResult OnPostRenderPdfPages ( [ FromBody ] jsonObjects responseData )
77
+ {
78
+ PdfRenderer pdfviewer = new PdfRenderer ( _cache ) ;
79
+ var jsonObject = JsonConverterstring ( responseData ) ;
80
+ object jsonResult = pdfviewer . GetPage ( jsonObject ) ;
81
+ return Content ( JsonConvert . SerializeObject ( jsonResult ) ) ;
82
+ }
83
+
84
+ //Post action for unloading and disposing the PDF document resources
85
+ public IActionResult OnPostUnload ( [ FromBody ] jsonObjects responseData )
86
+ {
87
+ PdfRenderer pdfviewer = new PdfRenderer ( _cache ) ;
88
+ var jsonObject = JsonConverterstring ( responseData ) ;
89
+ pdfviewer . ClearCache ( jsonObject ) ;
90
+ return this . Content ( "Document cache is cleared" ) ;
91
+ }
92
+
93
+ //Post action for rendering the ThumbnailImages
94
+ public IActionResult OnPostRenderThumbnailImages ( [ FromBody ] jsonObjects responseData )
95
+ {
96
+ PdfRenderer pdfviewer = new PdfRenderer ( _cache ) ;
97
+ var jsonObject = JsonConverterstring ( responseData ) ;
98
+ object result = pdfviewer . GetThumbnailImages ( jsonObject ) ;
99
+ return Content ( JsonConvert . SerializeObject ( result ) ) ;
100
+ }
101
+
102
+ //Post action for processing the bookmarks from the PDF documents
103
+ public IActionResult OnPostBookmarks ( [ FromBody ] jsonObjects responseData )
104
+ {
105
+ PdfRenderer pdfviewer = new PdfRenderer ( _cache ) ;
106
+ var jsonObject = JsonConverterstring ( responseData ) ;
107
+ object jsonResult = pdfviewer . GetBookmarks ( jsonObject ) ;
108
+ return Content ( JsonConvert . SerializeObject ( jsonResult ) ) ;
109
+ }
110
+
111
+ //Post action for rendering the annotation comments
112
+ public IActionResult OnPostRenderAnnotationComments ( [ FromBody ] jsonObjects responseData )
113
+ {
114
+ PdfRenderer pdfviewer = new PdfRenderer ( _cache ) ;
115
+ var jsonObject = JsonConverterstring ( responseData ) ;
116
+ object jsonResult = pdfviewer . GetAnnotationComments ( jsonObject ) ;
117
+ return Content ( JsonConvert . SerializeObject ( jsonResult ) ) ;
118
+ }
119
+
120
+ //Post action for exporting the annotations
121
+ public IActionResult OnPostExportAnnotations ( [ FromBody ] jsonObjects responseData )
122
+ {
123
+ PdfRenderer pdfviewer = new PdfRenderer ( _cache ) ;
124
+ var jsonObject = JsonConverterstring ( responseData ) ;
125
+ string jsonResult = pdfviewer . ExportAnnotation ( jsonObject ) ;
126
+ return Content ( jsonResult ) ;
127
+ }
128
+
129
+ //Post action for importing the annotations
130
+ public IActionResult OnPostImportAnnotations ( [ FromBody ] jsonObjects responseData )
131
+ {
132
+ PdfRenderer pdfviewer = new PdfRenderer ( _cache ) ;
133
+ var jsonObject = JsonConverterstring ( responseData ) ;
134
+ string jsonResult = string . Empty ;
135
+ object JsonResult ;
136
+ if ( jsonObject != null && jsonObject . ContainsKey ( "fileName" ) )
137
+ {
138
+ string documentPath = GetDocumentPath ( jsonObject [ "fileName" ] ) ;
139
+ if ( ! string . IsNullOrEmpty ( documentPath ) )
140
+ {
141
+ jsonResult = System . IO . File . ReadAllText ( documentPath ) ;
142
+ }
143
+ else
144
+ {
145
+ return this . Content ( jsonObject [ "document" ] + " is not found" ) ;
146
+ }
147
+ }
148
+ else
149
+ {
150
+ string extension = Path . GetExtension ( jsonObject [ "importedData" ] ) ;
151
+ if ( extension != ".xfdf" )
152
+ {
153
+ JsonResult = pdfviewer . ImportAnnotation ( jsonObject ) ;
154
+ return Content ( JsonConvert . SerializeObject ( JsonResult ) ) ;
155
+ }
156
+ else
157
+ {
158
+ string documentPath = GetDocumentPath ( jsonObject [ "importedData" ] ) ;
159
+ if ( ! string . IsNullOrEmpty ( documentPath ) )
160
+ {
161
+ byte [ ] bytes = System . IO . File . ReadAllBytes ( documentPath ) ;
162
+ jsonObject [ "importedData" ] = Convert . ToBase64String ( bytes ) ;
163
+ JsonResult = pdfviewer . ImportAnnotation ( jsonObject ) ;
164
+ return Content ( JsonConvert . SerializeObject ( JsonResult ) ) ;
165
+ }
166
+ else
167
+ {
168
+ return this . Content ( jsonObject [ "document" ] + " is not found" ) ;
169
+ }
170
+ }
171
+ }
172
+ return Content ( jsonResult ) ;
173
+ }
174
+
175
+ //Post action for downloading the PDF documents
176
+ public IActionResult OnPostDownload ( [ FromBody ] jsonObjects responseData )
177
+ {
178
+ PdfRenderer pdfviewer = new PdfRenderer ( _cache ) ;
179
+ var jsonObject = JsonConverterstring ( responseData ) ;
180
+ string documentBase = pdfviewer . GetDocumentAsBase64 ( jsonObject ) ;
181
+ return Content ( documentBase ) ;
182
+ }
183
+
184
+ //Post action for printing the PDF documents
185
+ public IActionResult OnPostPrintImages ( [ FromBody ] jsonObjects responseData )
186
+ {
187
+ PdfRenderer pdfviewer = new PdfRenderer ( _cache ) ;
188
+ var jsonObject = JsonConverterstring ( responseData ) ;
189
+ object pageImage = pdfviewer . GetPrintImage ( jsonObject ) ;
190
+ return Content ( JsonConvert . SerializeObject ( pageImage ) ) ;
191
+ }
192
+
193
+ //Gets the path of the PDF document
194
+ private string GetDocumentPath ( string document )
195
+ {
196
+ string documentPath = string . Empty ;
197
+ if ( ! System . IO . File . Exists ( document ) )
198
+ {
199
+ string basePath = _hostingEnvironment . WebRootPath ;
200
+ string dataPath = string . Empty ;
201
+ dataPath = basePath + "/" ;
202
+ if ( System . IO . File . Exists ( dataPath + ( document ) ) )
203
+ documentPath = dataPath + document ;
204
+ }
205
+ else
206
+ {
207
+ documentPath = document ;
208
+ }
209
+ return documentPath ;
210
+ }
211
+ }
212
+
213
+ public class jsonObjects
214
+ {
215
+ public string document { get ; set ; }
216
+ public string password { get ; set ; }
217
+ public string zoomFactor { get ; set ; }
218
+ public string isFileName { get ; set ; }
219
+ public string xCoordinate { get ; set ; }
220
+ public string yCoordinate { get ; set ; }
221
+ public string pageNumber { get ; set ; }
222
+ public string documentId { get ; set ; }
223
+ public string hashId { get ; set ; }
224
+ public string sizeX { get ; set ; }
225
+ public string sizeY { get ; set ; }
226
+ public string startPage { get ; set ; }
227
+ public string endPage { get ; set ; }
228
+ public string stampAnnotations { get ; set ; }
229
+ public string textMarkupAnnotations { get ; set ; }
230
+ public string stickyNotesAnnotation { get ; set ; }
231
+ public string shapeAnnotations { get ; set ; }
232
+ public string measureShapeAnnotations { get ; set ; }
233
+ public string action { get ; set ; }
234
+ public string pageStartIndex { get ; set ; }
235
+ public string pageEndIndex { get ; set ; }
236
+ public string fileName { get ; set ; }
237
+ public string elementId { get ; set ; }
238
+ public string pdfAnnotation { get ; set ; }
239
+ public string importPageList { get ; set ; }
240
+ public string uniqueId { get ; set ; }
241
+ public string data { get ; set ; }
242
+ public string viewPortWidth { get ; set ; }
243
+ public string viewPortHeight { get ; set ; }
244
+ public string tilecount { get ; set ; }
245
+ public bool isCompletePageSizeNotReceived { get ; set ; }
246
+ public string freeTextAnnotation { get ; set ; }
247
+ public string signatureData { get ; set ; }
248
+ public string fieldsData { get ; set ; }
249
+ public string formDesigner { get ; set ; }
250
+ public string inkSignatureData { get ; set ; }
251
+ public bool hideEmptyDigitalSignatureFields { get ; set ; }
252
+ public bool showDigitalSignatureAppearance { get ; set ; }
253
+ public bool digitalSignaturePresent { get ; set ; }
254
+ public string tileXCount { get ; set ; }
255
+ public string tileYCount { get ; set ; }
256
+ public string digitalSignaturePageList { get ; set ; }
257
+ public string annotationCollection { get ; set ; }
258
+ public string annotationsPageList { get ; set ; }
259
+ public string formFieldsPageList { get ; set ; }
260
+ public bool isAnnotationsExist { get ; set ; }
261
+ public bool isFormFieldAnnotationsExist { get ; set ; }
262
+ public string documentLiveCount { get ; set ; }
263
+ public string annotationDataFormat { get ; set ; }
264
+ public string importedData { get ; set ; }
265
+ }
266
+ }
0 commit comments