11using Microsoft . AspNetCore . Mvc ;
2+ using Microsoft . Extensions . Caching . Memory ;
3+ using Syncfusion . EJ2 . PdfViewer ;
4+ using Newtonsoft . Json ;
25using Microsoft . AspNetCore . Mvc . RazorPages ;
6+ using System . Reflection ;
7+ using System . Net ;
38
49namespace PDFViewerSample . Pages
510{
11+ [ IgnoreAntiforgeryToken ( Order = 1001 ) ]
612 public class IndexModel : PageModel
713 {
8- private readonly ILogger < IndexModel > _logger ;
914
10- public IndexModel ( ILogger < IndexModel > logger )
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+ public IActionResult RenderPdfTexts ( [ FromBody ] Dictionary < string , string > jsonObject )
85+ {
86+ //Initialize the PDF Viewer object with memory cache object
87+ PdfRenderer pdfviewer = new PdfRenderer ( _cache ) ;
88+ object jsonResult = pdfviewer . GetDocumentText ( jsonObject ) ;
89+ return Content ( JsonConvert . SerializeObject ( jsonResult ) ) ;
90+ }
91+
92+ public IActionResult ValidatePassword ( [ FromBody ] Dictionary < string , string > jsonObject )
93+ {
94+ PdfRenderer pdfviewer = new PdfRenderer ( _cache ) ;
95+ MemoryStream stream = new MemoryStream ( ) ;
96+ object jsonResult = new object ( ) ;
97+ if ( jsonObject != null && jsonObject . ContainsKey ( "document" ) )
98+ {
99+ if ( bool . Parse ( jsonObject [ "isFileName" ] ) )
100+ {
101+ string documentPath = GetDocumentPath ( jsonObject [ "document" ] ) ;
102+ if ( ! string . IsNullOrEmpty ( documentPath ) )
103+ {
104+ byte [ ] bytes = System . IO . File . ReadAllBytes ( documentPath ) ;
105+ stream = new MemoryStream ( bytes ) ;
106+ }
107+ else
108+ {
109+ string fileName = jsonObject [ "document" ] . Split ( new string [ ] { "://" } , StringSplitOptions . None ) [ 0 ] ;
110+
111+ if ( fileName == "http" || fileName == "https" )
112+ {
113+ WebClient WebClient = new WebClient ( ) ;
114+ byte [ ] pdfDoc = WebClient . DownloadData ( jsonObject [ "document" ] ) ;
115+ stream = new MemoryStream ( pdfDoc ) ;
116+ }
117+
118+ else
119+ {
120+ return this . Content ( jsonObject [ "document" ] + " is not found" ) ;
121+ }
122+ }
123+ }
124+ else
125+ {
126+ byte [ ] bytes = Convert . FromBase64String ( jsonObject [ "document" ] ) ;
127+ stream = new MemoryStream ( bytes ) ;
128+ }
129+ }
130+ string password = null ;
131+ if ( jsonObject . ContainsKey ( "password" ) )
132+ {
133+ password = jsonObject [ "password" ] ;
134+ }
135+ var result = pdfviewer . Load ( stream , password ) ;
136+
137+ return Content ( JsonConvert . SerializeObject ( result ) ) ;
138+ }
139+
140+
141+ //Post action for unloading and disposing the PDF document resources
142+ public IActionResult OnPostUnload ( [ FromBody ] jsonObjects responseData )
11143 {
12- _logger = logger ;
144+ PdfRenderer pdfviewer = new PdfRenderer ( _cache ) ;
145+ var jsonObject = JsonConverterstring ( responseData ) ;
146+ pdfviewer . ClearCache ( jsonObject ) ;
147+ return this . Content ( "Document cache is cleared" ) ;
13148 }
14149
15- public void OnGet ( )
150+ //Post action for rendering the ThumbnailImages
151+ public IActionResult OnPostRenderThumbnailImages ( [ FromBody ] jsonObjects responseData )
16152 {
153+ PdfRenderer pdfviewer = new PdfRenderer ( _cache ) ;
154+ var jsonObject = JsonConverterstring ( responseData ) ;
155+ object result = pdfviewer . GetThumbnailImages ( jsonObject ) ;
156+ return Content ( JsonConvert . SerializeObject ( result ) ) ;
157+ }
158+
159+ //Post action for processing the bookmarks from the PDF documents
160+ public IActionResult OnPostBookmarks ( [ FromBody ] jsonObjects responseData )
161+ {
162+ PdfRenderer pdfviewer = new PdfRenderer ( _cache ) ;
163+ var jsonObject = JsonConverterstring ( responseData ) ;
164+ object jsonResult = pdfviewer . GetBookmarks ( jsonObject ) ;
165+ return Content ( JsonConvert . SerializeObject ( jsonResult ) ) ;
166+ }
167+
168+ //Post action for rendering the annotation comments
169+ public IActionResult OnPostRenderAnnotationComments ( [ FromBody ] jsonObjects responseData )
170+ {
171+ PdfRenderer pdfviewer = new PdfRenderer ( _cache ) ;
172+ var jsonObject = JsonConverterstring ( responseData ) ;
173+ object jsonResult = pdfviewer . GetAnnotationComments ( jsonObject ) ;
174+ return Content ( JsonConvert . SerializeObject ( jsonResult ) ) ;
175+ }
176+
177+ //Post action for exporting the annotations
178+ public IActionResult OnPostExportAnnotations ( [ FromBody ] jsonObjects responseData )
179+ {
180+ PdfRenderer pdfviewer = new PdfRenderer ( _cache ) ;
181+ var jsonObject = JsonConverterstring ( responseData ) ;
182+ string jsonResult = pdfviewer . ExportAnnotation ( jsonObject ) ;
183+ return Content ( jsonResult ) ;
184+ }
17185
186+ //Post action for importing the annotations
187+ public IActionResult OnPostImportAnnotations ( [ FromBody ] jsonObjects responseData )
188+ {
189+ PdfRenderer pdfviewer = new PdfRenderer ( _cache ) ;
190+ var jsonObject = JsonConverterstring ( responseData ) ;
191+ string jsonResult = string . Empty ;
192+ object JsonResult ;
193+ if ( jsonObject != null && jsonObject . ContainsKey ( "fileName" ) )
194+ {
195+ string documentPath = GetDocumentPath ( jsonObject [ "fileName" ] ) ;
196+ if ( ! string . IsNullOrEmpty ( documentPath ) )
197+ {
198+ jsonResult = System . IO . File . ReadAllText ( documentPath ) ;
199+ }
200+ else
201+ {
202+ return this . Content ( jsonObject [ "document" ] + " is not found" ) ;
203+ }
204+ }
205+ else
206+ {
207+ string extension = Path . GetExtension ( jsonObject [ "importedData" ] ) ;
208+ if ( extension != ".xfdf" )
209+ {
210+ JsonResult = pdfviewer . ImportAnnotation ( jsonObject ) ;
211+ return Content ( JsonConvert . SerializeObject ( JsonResult ) ) ;
212+ }
213+ else
214+ {
215+ string documentPath = GetDocumentPath ( jsonObject [ "importedData" ] ) ;
216+ if ( ! string . IsNullOrEmpty ( documentPath ) )
217+ {
218+ byte [ ] bytes = System . IO . File . ReadAllBytes ( documentPath ) ;
219+ jsonObject [ "importedData" ] = Convert . ToBase64String ( bytes ) ;
220+ JsonResult = pdfviewer . ImportAnnotation ( jsonObject ) ;
221+ return Content ( JsonConvert . SerializeObject ( JsonResult ) ) ;
222+ }
223+ else
224+ {
225+ return this . Content ( jsonObject [ "document" ] + " is not found" ) ;
226+ }
227+ }
228+ }
229+ return Content ( jsonResult ) ;
18230 }
231+
232+ //Post action for downloading the PDF documents
233+ public IActionResult OnPostDownload ( [ FromBody ] jsonObjects responseData )
234+ {
235+ PdfRenderer pdfviewer = new PdfRenderer ( _cache ) ;
236+ var jsonObject = JsonConverterstring ( responseData ) ;
237+ string documentBase = pdfviewer . GetDocumentAsBase64 ( jsonObject ) ;
238+ return Content ( documentBase ) ;
239+ }
240+
241+ //Post action for printing the PDF documents
242+ public IActionResult OnPostPrintImages ( [ FromBody ] jsonObjects responseData )
243+ {
244+ PdfRenderer pdfviewer = new PdfRenderer ( _cache ) ;
245+ var jsonObject = JsonConverterstring ( responseData ) ;
246+ object pageImage = pdfviewer . GetPrintImage ( jsonObject ) ;
247+ return Content ( JsonConvert . SerializeObject ( pageImage ) ) ;
248+ }
249+
250+ //Gets the path of the PDF document
251+ private string GetDocumentPath ( string document )
252+ {
253+ string documentPath = string . Empty ;
254+ if ( ! System . IO . File . Exists ( document ) )
255+ {
256+ string basePath = _hostingEnvironment . WebRootPath ;
257+ string dataPath = string . Empty ;
258+ dataPath = basePath + "/" ;
259+ if ( System . IO . File . Exists ( dataPath + ( document ) ) )
260+ documentPath = dataPath + document ;
261+ }
262+ else
263+ {
264+ documentPath = document ;
265+ }
266+ return documentPath ;
267+ }
268+ }
269+
270+ public class jsonObjects
271+ {
272+ public string document { get ; set ; }
273+ public string password { get ; set ; }
274+ public string zoomFactor { get ; set ; }
275+ public string isFileName { get ; set ; }
276+ public string xCoordinate { get ; set ; }
277+ public string yCoordinate { get ; set ; }
278+ public string pageNumber { get ; set ; }
279+ public string documentId { get ; set ; }
280+ public string hashId { get ; set ; }
281+ public string sizeX { get ; set ; }
282+ public string sizeY { get ; set ; }
283+ public string startPage { get ; set ; }
284+ public string endPage { get ; set ; }
285+ public string stampAnnotations { get ; set ; }
286+ public string textMarkupAnnotations { get ; set ; }
287+ public string stickyNotesAnnotation { get ; set ; }
288+ public string shapeAnnotations { get ; set ; }
289+ public string measureShapeAnnotations { get ; set ; }
290+ public string action { get ; set ; }
291+ public string pageStartIndex { get ; set ; }
292+ public string pageEndIndex { get ; set ; }
293+ public string fileName { get ; set ; }
294+ public string elementId { get ; set ; }
295+ public string pdfAnnotation { get ; set ; }
296+ public string importPageList { get ; set ; }
297+ public string uniqueId { get ; set ; }
298+ public string data { get ; set ; }
299+ public string viewPortWidth { get ; set ; }
300+ public string viewPortHeight { get ; set ; }
301+ public string tilecount { get ; set ; }
302+ public bool isCompletePageSizeNotReceived { get ; set ; }
303+ public string freeTextAnnotation { get ; set ; }
304+ public string signatureData { get ; set ; }
305+ public string fieldsData { get ; set ; }
306+ public string FormDesigner { get ; set ; }
307+ public string inkSignatureData { get ; set ; }
308+ public bool hideEmptyDigitalSignatureFields { get ; set ; }
309+ public bool showDigitalSignatureAppearance { get ; set ; }
310+ public bool digitalSignaturePresent { get ; set ; }
311+ public string tileXCount { get ; set ; }
312+ public string tileYCount { get ; set ; }
313+ public string digitalSignaturePageList { get ; set ; }
314+ public string annotationCollection { get ; set ; }
315+ public string annotationsPageList { get ; set ; }
316+ public string formFieldsPageList { get ; set ; }
317+ public bool isAnnotationsExist { get ; set ; }
318+ public bool isFormFieldAnnotationsExist { get ; set ; }
319+ public string documentLiveCount { get ; set ; }
320+ public string annotationDataFormat { get ; set ; }
321+ public string importedData { get ; set ; }
19322 }
20- }
323+ }
0 commit comments