1
+ using Newtonsoft . Json ;
2
+ using Syncfusion . EJ2 . PdfViewer ;
3
+ using System ;
4
+ using System . Collections . Generic ;
5
+ using System . IO ;
6
+ using System . Linq ;
7
+ using System . Net ;
8
+ using System . Net . Http ;
9
+ using System . Reflection ;
10
+ using System . Web ;
11
+ using System . Web . Caching ;
12
+ using System . Web . Mvc ;
13
+
14
+ namespace GettingStartedMVC . Controllers
15
+ {
16
+ public class HomeController : Controller
17
+ {
18
+ [ System . Web . Mvc . HttpPost ]
19
+ public ActionResult Load ( jsonObjects jsonObject )
20
+ {
21
+ PdfRenderer pdfviewer = new PdfRenderer ( ) ;
22
+ MemoryStream stream = new MemoryStream ( ) ;
23
+ var jsonData = JsonConverter ( jsonObject ) ;
24
+ object jsonResult = new object ( ) ;
25
+ if ( jsonObject != null && jsonData . ContainsKey ( "document" ) )
26
+ {
27
+ if ( bool . Parse ( jsonData [ "isFileName" ] ) )
28
+ {
29
+ string documentPath = GetDocumentPath ( jsonData [ "document" ] ) ;
30
+
31
+ if ( ! string . IsNullOrEmpty ( documentPath ) )
32
+ {
33
+ byte [ ] bytes = System . IO . File . ReadAllBytes ( documentPath ) ;
34
+ stream = new MemoryStream ( bytes ) ;
35
+ }
36
+ else
37
+ {
38
+ string fileName = jsonData [ "document" ] . Split ( new string [ ] { "://" } , StringSplitOptions . None ) [ 0 ] ;
39
+ if ( fileName == "http" || fileName == "https" )
40
+ {
41
+ var WebClient = new WebClient ( ) ;
42
+ byte [ ] pdfDoc = WebClient . DownloadData ( jsonData [ "document" ] ) ;
43
+ stream = new MemoryStream ( pdfDoc ) ;
44
+ }
45
+ else
46
+ {
47
+ return this . Content ( jsonData [ "document" ] + " is not found" ) ;
48
+ }
49
+ }
50
+ }
51
+ else
52
+ {
53
+ byte [ ] bytes = Convert . FromBase64String ( jsonData [ "document" ] ) ;
54
+ stream = new MemoryStream ( bytes ) ;
55
+
56
+ }
57
+ }
58
+ jsonResult = pdfviewer . Load ( stream , jsonData ) ;
59
+ return Content ( JsonConvert . SerializeObject ( jsonResult ) ) ;
60
+ }
61
+
62
+ public Dictionary < string , string > JsonConverter ( jsonObjects results )
63
+ {
64
+ Dictionary < string , object > resultObjects = new Dictionary < string , object > ( ) ;
65
+ resultObjects = results . GetType ( ) . GetProperties ( BindingFlags . Instance | BindingFlags . Public )
66
+ . ToDictionary ( prop => prop . Name , prop => prop . GetValue ( results , null ) ) ;
67
+ var emptyObjects = ( from kv in resultObjects
68
+ where kv . Value != null
69
+ select kv ) . ToDictionary ( kv => kv . Key , kv => kv . Value ) ;
70
+ Dictionary < string , string > jsonResult = emptyObjects . ToDictionary ( k => k . Key , k => k . Value . ToString ( ) ) ;
71
+ return jsonResult ;
72
+ }
73
+
74
+ [ System . Web . Mvc . HttpPost ]
75
+ public ActionResult ExportAnnotations ( jsonObjects jsonObject )
76
+ {
77
+ PdfRenderer pdfviewer = new PdfRenderer ( ) ;
78
+ var jsonData = JsonConverter ( jsonObject ) ;
79
+ string jsonResult = pdfviewer . ExportAnnotation ( jsonData ) ;
80
+ return Content ( ( jsonResult ) ) ;
81
+ }
82
+
83
+ [ System . Web . Mvc . HttpPost ]
84
+ public ActionResult ImportAnnotations ( jsonObjects jsonObject1 )
85
+ {
86
+ PdfRenderer pdfviewer = new PdfRenderer ( ) ;
87
+ var jsonObject = JsonConverter ( jsonObject1 ) ;
88
+ string jsonResult = string . Empty ;
89
+ object JsonResult ;
90
+ if ( jsonObject != null && jsonObject . ContainsKey ( "fileName" ) )
91
+ {
92
+ string documentPath = GetDocumentPath ( jsonObject [ "fileName" ] ) ;
93
+ if ( ! string . IsNullOrEmpty ( documentPath ) )
94
+ {
95
+ jsonResult = System . IO . File . ReadAllText ( documentPath ) ;
96
+ }
97
+ else
98
+ {
99
+ return this . Content ( jsonObject [ "document" ] + " is not found" ) ;
100
+ }
101
+ }
102
+ else
103
+ {
104
+ string extension = Path . GetExtension ( jsonObject [ "importedData" ] ) ;
105
+ if ( extension != ".xfdf" )
106
+ {
107
+ JsonResult = pdfviewer . ImportAnnotation ( jsonObject ) ;
108
+ return Content ( JsonConvert . SerializeObject ( JsonResult ) ) ;
109
+ }
110
+ else
111
+ {
112
+ string documentPath = GetDocumentPath ( jsonObject [ "importedData" ] ) ;
113
+ if ( ! string . IsNullOrEmpty ( documentPath ) )
114
+ {
115
+ byte [ ] bytes = System . IO . File . ReadAllBytes ( documentPath ) ;
116
+ jsonObject [ "importedData" ] = Convert . ToBase64String ( bytes ) ;
117
+ JsonResult = pdfviewer . ImportAnnotation ( jsonObject ) ;
118
+ return Content ( JsonConvert . SerializeObject ( JsonResult ) ) ;
119
+ }
120
+ else
121
+ {
122
+ return this . Content ( jsonObject [ "document" ] + " is not found" ) ;
123
+ }
124
+ }
125
+ }
126
+ return Content ( jsonResult ) ;
127
+ }
128
+
129
+
130
+ [ System . Web . Mvc . HttpPost ]
131
+ public ActionResult ImportFormFields ( jsonObjects jsonObject )
132
+ {
133
+ PdfRenderer pdfviewer = new PdfRenderer ( ) ;
134
+ var jsonData = JsonConverter ( jsonObject ) ;
135
+ object jsonResult = pdfviewer . ImportFormFields ( jsonData ) ;
136
+ return Content ( JsonConvert . SerializeObject ( jsonResult ) ) ;
137
+ }
138
+
139
+ [ System . Web . Mvc . HttpPost ]
140
+ public ActionResult ExportFormFields ( jsonObjects jsonObject )
141
+ {
142
+ PdfRenderer pdfviewer = new PdfRenderer ( ) ;
143
+ var jsonData = JsonConverter ( jsonObject ) ;
144
+ string jsonResult = pdfviewer . ExportFormFields ( jsonData ) ;
145
+ return Content ( jsonResult ) ;
146
+ }
147
+
148
+ [ System . Web . Mvc . HttpPost ]
149
+ public ActionResult RenderPdfPages ( jsonObjects jsonObject )
150
+ {
151
+ PdfRenderer pdfviewer = new PdfRenderer ( ) ;
152
+ var jsonData = JsonConverter ( jsonObject ) ;
153
+ object jsonResult = pdfviewer . GetPage ( jsonData ) ;
154
+ return Content ( JsonConvert . SerializeObject ( jsonResult ) ) ;
155
+ }
156
+
157
+ [ System . Web . Mvc . HttpPost ]
158
+ public ActionResult Unload ( jsonObjects jsonObject )
159
+ {
160
+ PdfRenderer pdfviewer = new PdfRenderer ( ) ;
161
+ var jsonData = JsonConverter ( jsonObject ) ;
162
+ pdfviewer . ClearCache ( jsonData ) ;
163
+ return this . Content ( "Document cache is cleared" ) ;
164
+ }
165
+
166
+ [ System . Web . Mvc . HttpPost ]
167
+ public ActionResult RenderThumbnailImages ( jsonObjects jsonObject )
168
+ {
169
+ PdfRenderer pdfviewer = new PdfRenderer ( ) ;
170
+ var jsonData = JsonConverter ( jsonObject ) ;
171
+ object result = pdfviewer . GetThumbnailImages ( jsonData ) ;
172
+ return Content ( JsonConvert . SerializeObject ( result ) ) ;
173
+ }
174
+
175
+ [ System . Web . Mvc . HttpPost ]
176
+ public ActionResult Bookmarks ( jsonObjects jsonObject )
177
+ {
178
+ PdfRenderer pdfviewer = new PdfRenderer ( ) ;
179
+ var jsonData = JsonConverter ( jsonObject ) ;
180
+ object jsonResult = pdfviewer . GetBookmarks ( jsonData ) ;
181
+ return Content ( JsonConvert . SerializeObject ( jsonResult ) ) ;
182
+ }
183
+
184
+ [ System . Web . Mvc . HttpPost ]
185
+ public ActionResult RenderAnnotationComments ( jsonObjects jsonObject )
186
+ {
187
+ PdfRenderer pdfviewer = new PdfRenderer ( ) ;
188
+ var jsonData = JsonConverter ( jsonObject ) ;
189
+ object jsonResult = pdfviewer . GetAnnotationComments ( jsonData ) ;
190
+ return Content ( JsonConvert . SerializeObject ( jsonResult ) ) ;
191
+ }
192
+
193
+ [ System . Web . Mvc . HttpPost ]
194
+ public ActionResult Download ( jsonObjects jsonObject )
195
+ {
196
+ PdfRenderer pdfviewer = new PdfRenderer ( ) ;
197
+ var jsonData = JsonConverter ( jsonObject ) ;
198
+ string documentBase = pdfviewer . GetDocumentAsBase64 ( jsonData ) ;
199
+ return Content ( documentBase ) ;
200
+ }
201
+
202
+ [ System . Web . Mvc . HttpPost ]
203
+ public ActionResult PrintImages ( jsonObjects jsonObject )
204
+ {
205
+ PdfRenderer pdfviewer = new PdfRenderer ( ) ;
206
+ var jsonData = JsonConverter ( jsonObject ) ;
207
+ object pageImage = pdfviewer . GetPrintImage ( jsonData ) ;
208
+ return Content ( JsonConvert . SerializeObject ( pageImage ) ) ;
209
+ }
210
+
211
+ private HttpResponseMessage GetPlainText ( string pageImage )
212
+ {
213
+ var responseText = new HttpResponseMessage ( HttpStatusCode . OK ) ;
214
+ responseText . Content = new StringContent ( pageImage , System . Text . Encoding . UTF8 , "text/plain" ) ;
215
+ return responseText ;
216
+ }
217
+
218
+ private string GetDocumentPath ( string document )
219
+ {
220
+ string documentPath = string . Empty ;
221
+ if ( ! System . IO . File . Exists ( document ) )
222
+ {
223
+ var path = HttpContext . Request . PhysicalApplicationPath ;
224
+ if ( System . IO . File . Exists ( path + "App_Data\\ " + document ) )
225
+ documentPath = path + "App_Data\\ " + document ;
226
+ }
227
+ else
228
+ {
229
+ documentPath = document ;
230
+ }
231
+ return documentPath ;
232
+ }
233
+
234
+ public ActionResult Index ( )
235
+ {
236
+ return View ( ) ;
237
+ }
238
+
239
+ public ActionResult About ( )
240
+ {
241
+ ViewBag . Message = "Your application description page." ;
242
+ return View ( ) ;
243
+ }
244
+
245
+ public ActionResult Contact ( )
246
+ {
247
+ ViewBag . Message = "Your contact page." ;
248
+ return View ( ) ;
249
+ }
250
+ }
251
+
252
+ public class jsonObjects
253
+ {
254
+ public string document { get ; set ; }
255
+ public string password { get ; set ; }
256
+ public bool isClientsideLoading { get ; set ; }
257
+ public string organizePages { get ; set ; }
258
+ public string zoomFactor { get ; set ; }
259
+ public string isFileName { get ; set ; }
260
+ public string xCoordinate { get ; set ; }
261
+ public string yCoordinate { get ; set ; }
262
+ public string pageNumber { get ; set ; }
263
+ public string documentId { get ; set ; }
264
+ public string hashId { get ; set ; }
265
+ public string sizeX { get ; set ; }
266
+ public string sizeY { get ; set ; }
267
+ public string startPage { get ; set ; }
268
+ public string endPage { get ; set ; }
269
+ public string stampAnnotations { get ; set ; }
270
+ public string textMarkupAnnotations { get ; set ; }
271
+ public string stickyNotesAnnotation { get ; set ; }
272
+ public string shapeAnnotations { get ; set ; }
273
+ public string measureShapeAnnotations { get ; set ; }
274
+ public string action { get ; set ; }
275
+ public string pageStartIndex { get ; set ; }
276
+ public string pageEndIndex { get ; set ; }
277
+ public string fileName { get ; set ; }
278
+ public string elementId { get ; set ; }
279
+ public string pdfAnnotation { get ; set ; }
280
+ public string importPageList { get ; set ; }
281
+ public string uniqueId { get ; set ; }
282
+ public string data { get ; set ; }
283
+ public string viewPortWidth { get ; set ; }
284
+ public string viewportHeight { get ; set ; }
285
+ public string tilecount { get ; set ; }
286
+ public bool isCompletePageSizeNotReceived { get ; set ; }
287
+ public string freeTextAnnotation { get ; set ; }
288
+ public string signatureData { get ; set ; }
289
+ public string fieldsData { get ; set ; }
290
+ public string formDesigner { get ; set ; }
291
+ public bool isSignatureEdited { get ; set ; }
292
+ public string inkSignatureData { get ; set ; }
293
+ public bool hideEmptyDigitalSignatureFields { get ; set ; }
294
+ public bool showDigitalSignatureAppearance { get ; set ; }
295
+ public bool digitalSignaturePresent { get ; set ; }
296
+ public string tileXCount { get ; set ; }
297
+ public string tileYCount { get ; set ; }
298
+ public string digitalSignaturePageList { get ; set ; }
299
+ public string annotationCollection { get ; set ; }
300
+ public string annotationsPageList { get ; set ; }
301
+ public string formFieldsPageList { get ; set ; }
302
+ public bool isAnnotationsExist { get ; set ; }
303
+ public bool isFormFieldAnnotationsExist { get ; set ; }
304
+ public string documentLiveCount { get ; set ; }
305
+ public string annotationDataFormat { get ; set ; }
306
+
307
+ public string importedData { get ; set ; }
308
+ }
309
+ }
0 commit comments