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