|
| 1 | +using Microsoft.AspNetCore.Hosting; |
| 2 | +using Microsoft.AspNetCore.Mvc; |
| 3 | +using Microsoft.Extensions.Caching.Memory; |
| 4 | +using Newtonsoft.Json; |
| 5 | +using Syncfusion.EJ2.PdfViewer; |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.IO; |
| 9 | +using System.Net; |
| 10 | + |
| 11 | +namespace PdfViewerWebService |
| 12 | +{ |
| 13 | + [Route("[controller]")] |
| 14 | + [ApiController] |
| 15 | + public class PdfViewerController : ControllerBase |
| 16 | + { |
| 17 | + private IWebHostEnvironment _hostingEnvironment; |
| 18 | + //Initialize the memory cache object |
| 19 | + public IMemoryCache _cache; |
| 20 | + public PdfViewerController(IWebHostEnvironment hostingEnvironment, IMemoryCache cache) |
| 21 | + { |
| 22 | + _hostingEnvironment = hostingEnvironment; |
| 23 | + _cache = cache; |
| 24 | + Console.WriteLine("PdfViewerController initialized"); |
| 25 | + } |
| 26 | + |
| 27 | + [HttpPost("Load")] |
| 28 | + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] |
| 29 | + [Route("[controller]/Load")] |
| 30 | + //Post action for Loading the PDF documents |
| 31 | + public IActionResult Load([FromBody] Dictionary<string, string> jsonObject) |
| 32 | + { |
| 33 | + Console.WriteLine("Load called"); |
| 34 | + //Initialize the PDF viewer object with memory cache object |
| 35 | + PdfRenderer pdfviewer = new PdfRenderer(_cache); |
| 36 | + MemoryStream stream = new MemoryStream(); |
| 37 | + object jsonResult = new object(); |
| 38 | + if (jsonObject != null && jsonObject.ContainsKey("document")) |
| 39 | + { |
| 40 | + if (bool.Parse(jsonObject["isFileName"])) |
| 41 | + { |
| 42 | + string documentPath = GetDocumentPath(jsonObject["document"]); |
| 43 | + if (!string.IsNullOrEmpty(documentPath)) |
| 44 | + { |
| 45 | + byte[] bytes = System.IO.File.ReadAllBytes(documentPath); |
| 46 | + stream = new MemoryStream(bytes); |
| 47 | + } |
| 48 | + else |
| 49 | + { |
| 50 | + string fileName = jsonObject["document"].Split(new string[] { "://" }, StringSplitOptions.None)[0]; |
| 51 | + |
| 52 | + if (fileName == "http" || fileName == "https") |
| 53 | + { |
| 54 | + WebClient WebClient = new WebClient(); |
| 55 | + byte[] pdfDoc = WebClient.DownloadData(jsonObject["document"]); |
| 56 | + stream = new MemoryStream(pdfDoc); |
| 57 | + } |
| 58 | + |
| 59 | + else |
| 60 | + { |
| 61 | + return this.Content(jsonObject["document"] + " is not found"); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + byte[] bytes = Convert.FromBase64String(jsonObject["document"]); |
| 68 | + stream = new MemoryStream(bytes); |
| 69 | + } |
| 70 | + } |
| 71 | + jsonResult = pdfviewer.Load(stream, jsonObject); |
| 72 | + return Content(JsonConvert.SerializeObject(jsonResult)); |
| 73 | + } |
| 74 | + |
| 75 | + [AcceptVerbs("Post")] |
| 76 | + [HttpPost("Bookmarks")] |
| 77 | + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] |
| 78 | + [Route("[controller]/Bookmarks")] |
| 79 | + //Post action for processing the bookmarks from the PDF documents |
| 80 | + public IActionResult Bookmarks([FromBody] Dictionary<string, string> jsonObject) |
| 81 | + { |
| 82 | + //Initialize the PDF Viewer object with memory cache object |
| 83 | + PdfRenderer pdfviewer = new PdfRenderer(_cache); |
| 84 | + var jsonResult = pdfviewer.GetBookmarks(jsonObject); |
| 85 | + return Content(JsonConvert.SerializeObject(jsonResult)); |
| 86 | + } |
| 87 | + |
| 88 | + [AcceptVerbs("Post")] |
| 89 | + [HttpPost("RenderPdfPages")] |
| 90 | + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] |
| 91 | + [Route("[controller]/RenderPdfPages")] |
| 92 | + //Post action for processing the PDF documents |
| 93 | + public IActionResult RenderPdfPages([FromBody] Dictionary<string, string> jsonObject) |
| 94 | + { |
| 95 | + //Initialize the PDF Viewer object with memory cache object |
| 96 | + PdfRenderer pdfviewer = new PdfRenderer(_cache); |
| 97 | + object jsonResult = pdfviewer.GetPage(jsonObject); |
| 98 | + return Content(JsonConvert.SerializeObject(jsonResult)); |
| 99 | + } |
| 100 | + |
| 101 | + [AcceptVerbs("Post")] |
| 102 | + [HttpPost("RenderPdfTexts")] |
| 103 | + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] |
| 104 | + [Route("[controller]/RenderPdfTexts")] |
| 105 | + //Post action for processing the PDF texts |
| 106 | + public IActionResult RenderPdfTexts([FromBody] Dictionary<string, string> jsonObject) |
| 107 | + { |
| 108 | + //Initialize the PDF Viewer object with memory cache object |
| 109 | + PdfRenderer pdfviewer = new PdfRenderer(_cache); |
| 110 | + object jsonResult = pdfviewer.GetDocumentText(jsonObject); |
| 111 | + return Content(JsonConvert.SerializeObject(jsonResult)); |
| 112 | + } |
| 113 | + |
| 114 | + [AcceptVerbs("Post")] |
| 115 | + [HttpPost("RenderThumbnailImages")] |
| 116 | + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] |
| 117 | + [Route("[controller]/RenderThumbnailImages")] |
| 118 | + //Post action for rendering the ThumbnailImages |
| 119 | + public IActionResult RenderThumbnailImages([FromBody] Dictionary<string, string> jsonObject) |
| 120 | + { |
| 121 | + //Initialize the PDF Viewer object with memory cache object |
| 122 | + PdfRenderer pdfviewer = new PdfRenderer(_cache); |
| 123 | + object result = pdfviewer.GetThumbnailImages(jsonObject); |
| 124 | + return Content(JsonConvert.SerializeObject(result)); |
| 125 | + } |
| 126 | + [AcceptVerbs("Post")] |
| 127 | + [HttpPost("RenderAnnotationComments")] |
| 128 | + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] |
| 129 | + [Route("[controller]/RenderAnnotationComments")] |
| 130 | + //Post action for rendering the annotations |
| 131 | + public IActionResult RenderAnnotationComments([FromBody] Dictionary<string, string> jsonObject) |
| 132 | + { |
| 133 | + //Initialize the PDF Viewer object with memory cache object |
| 134 | + PdfRenderer pdfviewer = new PdfRenderer(_cache); |
| 135 | + object jsonResult = pdfviewer.GetAnnotationComments(jsonObject); |
| 136 | + return Content(JsonConvert.SerializeObject(jsonResult)); |
| 137 | + } |
| 138 | + [AcceptVerbs("Post")] |
| 139 | + [HttpPost("ExportAnnotations")] |
| 140 | + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] |
| 141 | + [Route("[controller]/ExportAnnotations")] |
| 142 | + //Post action to export annotations |
| 143 | + public IActionResult ExportAnnotations([FromBody] Dictionary<string, string> jsonObject) |
| 144 | + { |
| 145 | + PdfRenderer pdfviewer = new PdfRenderer(_cache); |
| 146 | + string jsonResult = pdfviewer.ExportAnnotation(jsonObject); |
| 147 | + return Content(jsonResult); |
| 148 | + } |
| 149 | + [AcceptVerbs("Post")] |
| 150 | + [HttpPost("ImportAnnotations")] |
| 151 | + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] |
| 152 | + [Route("[controller]/ImportAnnotations")] |
| 153 | + //Post action to import annotations |
| 154 | + public IActionResult ImportAnnotations([FromBody] Dictionary<string, string> jsonObject) |
| 155 | + { |
| 156 | + PdfRenderer pdfviewer = new PdfRenderer(_cache); |
| 157 | + string jsonResult = string.Empty; |
| 158 | + object JsonResult; |
| 159 | + if (jsonObject != null && jsonObject.ContainsKey("fileName")) |
| 160 | + { |
| 161 | + string documentPath = GetDocumentPath(jsonObject["fileName"]); |
| 162 | + if (!string.IsNullOrEmpty(documentPath)) |
| 163 | + { |
| 164 | + jsonResult = System.IO.File.ReadAllText(documentPath); |
| 165 | + } |
| 166 | + else |
| 167 | + { |
| 168 | + return this.Content(jsonObject["document"] + " is not found"); |
| 169 | + } |
| 170 | + } |
| 171 | + else |
| 172 | + { |
| 173 | + string extension = Path.GetExtension(jsonObject["importedData"]); |
| 174 | + if (extension != ".xfdf") |
| 175 | + { |
| 176 | + JsonResult = pdfviewer.ImportAnnotation(jsonObject); |
| 177 | + return Content(JsonConvert.SerializeObject(JsonResult)); |
| 178 | + } |
| 179 | + else |
| 180 | + { |
| 181 | + string documentPath = GetDocumentPath(jsonObject["importedData"]); |
| 182 | + if (!string.IsNullOrEmpty(documentPath)) |
| 183 | + { |
| 184 | + byte[] bytes = System.IO.File.ReadAllBytes(documentPath); |
| 185 | + jsonObject["importedData"] = Convert.ToBase64String(bytes); |
| 186 | + JsonResult = pdfviewer.ImportAnnotation(jsonObject); |
| 187 | + return Content(JsonConvert.SerializeObject(JsonResult)); |
| 188 | + } |
| 189 | + else |
| 190 | + { |
| 191 | + return this.Content(jsonObject["document"] + " is not found"); |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + return Content(jsonResult); |
| 196 | + } |
| 197 | + |
| 198 | + [AcceptVerbs("Post")] |
| 199 | + [HttpPost("ExportFormFields")] |
| 200 | + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] |
| 201 | + [Route("[controller]/ExportFormFields")] |
| 202 | + public IActionResult ExportFormFields([FromBody] Dictionary<string, string> jsonObject) |
| 203 | + |
| 204 | + { |
| 205 | + PdfRenderer pdfviewer = new PdfRenderer(_cache); |
| 206 | + string jsonResult = pdfviewer.ExportFormFields(jsonObject); |
| 207 | + return Content(jsonResult); |
| 208 | + } |
| 209 | + |
| 210 | + [AcceptVerbs("Post")] |
| 211 | + [HttpPost("ImportFormFields")] |
| 212 | + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] |
| 213 | + [Route("[controller]/ImportFormFields")] |
| 214 | + public IActionResult ImportFormFields([FromBody] Dictionary<string, string> jsonObject) |
| 215 | + { |
| 216 | + PdfRenderer pdfviewer = new PdfRenderer(_cache); |
| 217 | + jsonObject["data"] = GetDocumentPath(jsonObject["data"]); |
| 218 | + object jsonResult = pdfviewer.ImportFormFields(jsonObject); |
| 219 | + return Content(JsonConvert.SerializeObject(jsonResult)); |
| 220 | + } |
| 221 | + |
| 222 | + [AcceptVerbs("Post")] |
| 223 | + [HttpPost("Unload")] |
| 224 | + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] |
| 225 | + [Route("[controller]/Unload")] |
| 226 | + //Post action for unloading and disposing the PDF document resources |
| 227 | + public IActionResult Unload([FromBody] Dictionary<string, string> jsonObject) |
| 228 | + { |
| 229 | + //Initialize the PDF Viewer object with memory cache object |
| 230 | + PdfRenderer pdfviewer = new PdfRenderer(_cache); |
| 231 | + pdfviewer.ClearCache(jsonObject); |
| 232 | + return this.Content("Document cache is cleared"); |
| 233 | + } |
| 234 | + |
| 235 | + |
| 236 | + [HttpPost("Download")] |
| 237 | + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] |
| 238 | + [Route("[controller]/Download")] |
| 239 | + //Post action for downloading the PDF documents |
| 240 | + public IActionResult Download([FromBody] Dictionary<string, string> jsonObject) |
| 241 | + { |
| 242 | + //Initialize the PDF Viewer object with memory cache object |
| 243 | + PdfRenderer pdfviewer = new PdfRenderer(_cache); |
| 244 | + string documentBase = pdfviewer.GetDocumentAsBase64(jsonObject); |
| 245 | + return Content(documentBase); |
| 246 | + } |
| 247 | + |
| 248 | + [HttpPost("PrintImages")] |
| 249 | + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] |
| 250 | + [Route("[controller]/PrintImages")] |
| 251 | + //Post action for printing the PDF documents |
| 252 | + public IActionResult PrintImages([FromBody] Dictionary<string, string> jsonObject) |
| 253 | + { |
| 254 | + //Initialize the PDF Viewer object with memory cache object |
| 255 | + PdfRenderer pdfviewer = new PdfRenderer(_cache); |
| 256 | + object pageImage = pdfviewer.GetPrintImage(jsonObject); |
| 257 | + return Content(JsonConvert.SerializeObject(pageImage)); |
| 258 | + } |
| 259 | + |
| 260 | + //Gets the path of the PDF document |
| 261 | + private string GetDocumentPath(string document) |
| 262 | + { |
| 263 | + string documentPath = string.Empty; |
| 264 | + if (!System.IO.File.Exists(document)) |
| 265 | + { |
| 266 | + var path = _hostingEnvironment.ContentRootPath; |
| 267 | + if (System.IO.File.Exists(path + "/Data/" + document)) |
| 268 | + documentPath = path + "/Data/" + document; |
| 269 | + } |
| 270 | + else |
| 271 | + { |
| 272 | + documentPath = document; |
| 273 | + } |
| 274 | + Console.WriteLine(documentPath); |
| 275 | + return documentPath; |
| 276 | + } |
| 277 | + // GET api/values |
| 278 | + [HttpGet] |
| 279 | + public IEnumerable<string> Get() |
| 280 | + { |
| 281 | + return new string[] { "value1", "value2" }; |
| 282 | + } |
| 283 | + |
| 284 | + // GET api/values/5 |
| 285 | + [HttpGet("{id}")] |
| 286 | + public string Get(int id) |
| 287 | + { |
| 288 | + return "value"; |
| 289 | + } |
| 290 | + } |
| 291 | +} |
0 commit comments