diff --git a/How to/Add Handwritten Signature Programmatically/PDFViewerSample/PDFViewerSample/PDFViewerSample.csproj b/How to/Add Handwritten Signature Programmatically/PDFViewerSample/PDFViewerSample/PDFViewerSample.csproj index 9f067fe..2baa1b9 100644 --- a/How to/Add Handwritten Signature Programmatically/PDFViewerSample/PDFViewerSample/PDFViewerSample.csproj +++ b/How to/Add Handwritten Signature Programmatically/PDFViewerSample/PDFViewerSample/PDFViewerSample.csproj @@ -9,6 +9,7 @@ + diff --git a/How to/Add Handwritten Signature Programmatically/PDFViewerSample/PDFViewerSample/Pages/Index.cshtml.cs b/How to/Add Handwritten Signature Programmatically/PDFViewerSample/PDFViewerSample/Pages/Index.cshtml.cs index ef356dd..50897ef 100644 --- a/How to/Add Handwritten Signature Programmatically/PDFViewerSample/PDFViewerSample/Pages/Index.cshtml.cs +++ b/How to/Add Handwritten Signature Programmatically/PDFViewerSample/PDFViewerSample/Pages/Index.cshtml.cs @@ -1,20 +1,294 @@ using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Caching.Memory; +using Syncfusion.EJ2.PdfViewer; +using Newtonsoft.Json; using Microsoft.AspNetCore.Mvc.RazorPages; +using System.Reflection; +using System.Net; namespace PDFViewerSample.Pages { + [IgnoreAntiforgeryToken(Order = 1001)] public class IndexModel : PageModel { - private readonly ILogger _logger; - public IndexModel(ILogger logger) + private readonly Microsoft.AspNetCore.Hosting.IHostingEnvironment _hostingEnvironment; + private IMemoryCache _cache; + + public IndexModel(Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment, IMemoryCache cache) + { + _hostingEnvironment = hostingEnvironment; + _cache = cache; + } + + [HttpPost("Load")] + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] + [Route("[controller]/Load")] +        //Post action for Loading the PDF documents   + public IActionResult OnPostLoad([FromBody] Dictionary jsonObject) + { + PdfRenderer pdfviewer = new PdfRenderer(_cache); + MemoryStream stream = new MemoryStream(); + object jsonResult = new object(); + if (jsonObject != null && jsonObject.ContainsKey("document")) + { + if (bool.Parse(jsonObject["isFileName"])) + { + string documentPath = GetDocumentPath(jsonObject["document"]); + if (!string.IsNullOrEmpty(documentPath)) + { + byte[] bytes = System.IO.File.ReadAllBytes(documentPath); + stream = new MemoryStream(bytes); + } + else + { + string fileName = jsonObject["document"].Split(new string[] { "://" }, StringSplitOptions.None)[0]; + if (fileName == "http" || fileName == "https") + { + WebClient WebClient = new WebClient(); + byte[] pdfDoc = WebClient.DownloadData(jsonObject["document"]); + stream = new MemoryStream(pdfDoc); + } + else + return this.Content(jsonObject["document"] + " is not found"); + } + } + else + { + byte[] bytes = Convert.FromBase64String(jsonObject["document"]); + stream = new MemoryStream(bytes); + } + } + jsonResult = pdfviewer.Load(stream, jsonObject); + return Content(JsonConvert.SerializeObject(jsonResult)); + } + + [AcceptVerbs("Post")] + [HttpPost("RenderPdfPages")] + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] + [Route("[controller]/RenderPdfPages")] + //Post action for processing the PDF documents. + public IActionResult OnPostRenderPdfPages([FromBody] Dictionary jsonObject) + { + PdfRenderer pdfviewer = new PdfRenderer(_cache); + object jsonResult = pdfviewer.GetPage(jsonObject); + return Content(JsonConvert.SerializeObject(jsonResult)); + } + + [AcceptVerbs("Post")] + [HttpPost("RenderPdfPages")] + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] + [Route("[controller]/RenderPdfPages")] +        //Post action for processing the PDF documents + public IActionResult RenderPdfTexts([FromBody] Dictionary jsonObject) + { + //Initialize the PDF Viewer object with memory cache object + PdfRenderer pdfviewer = new PdfRenderer(_cache); + object jsonResult = pdfviewer.GetDocumentText(jsonObject); + return Content(JsonConvert.SerializeObject(jsonResult)); + } + + [AcceptVerbs("Post")] + [HttpPost("ValidatePassword")] + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] + [Route("[controller]/ValidatePassword")] + public IActionResult ValidatePassword([FromBody] Dictionary jsonObject) + { + PdfRenderer pdfviewer = new PdfRenderer(_cache); + MemoryStream stream = new MemoryStream(); + object jsonResult = new object(); + if (jsonObject != null && jsonObject.ContainsKey("document")) + { + if (bool.Parse(jsonObject["isFileName"])) + { + string documentPath = GetDocumentPath(jsonObject["document"]); + if (!string.IsNullOrEmpty(documentPath)) + { + byte[] bytes = System.IO.File.ReadAllBytes(documentPath); + stream = new MemoryStream(bytes); + } + else + { + string fileName = jsonObject["document"].Split(new string[] { "://" }, StringSplitOptions.None)[0]; + + if (fileName == "http" || fileName == "https") + { + WebClient WebClient = new WebClient(); + byte[] pdfDoc = WebClient.DownloadData(jsonObject["document"]); + stream = new MemoryStream(pdfDoc); + } + + else + { + return this.Content(jsonObject["document"] + " is not found"); + } + } + } + else + { + byte[] bytes = Convert.FromBase64String(jsonObject["document"]); + stream = new MemoryStream(bytes); + } + } + string password = null; + if (jsonObject.ContainsKey("password")) + { + password = jsonObject["password"]; + } + var result = pdfviewer.Load(stream, password); + + return Content(JsonConvert.SerializeObject(result)); + } + + + [AcceptVerbs("Post")] + [HttpPost("Unload")] + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] + [Route("[controller]/Unload")] +        //Post action for unloading and disposing the PDF document resources  + public IActionResult OnPostUnload([FromBody] Dictionary jsonObject) { - _logger = logger; + PdfRenderer pdfviewer = new PdfRenderer(_cache); + pdfviewer.ClearCache(jsonObject); + return this.Content("Document cache is cleared"); } - public void OnGet() + [AcceptVerbs("Post")] + [HttpPost("RenderThumbnailImages")] + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] + [Route("[controller]/RenderThumbnailImages")] + //Post action for rendering the ThumbnailImages + public IActionResult OnPostRenderThumbnailImages([FromBody] Dictionary jsonObject) { + PdfRenderer pdfviewer = new PdfRenderer(_cache); + object result = pdfviewer.GetThumbnailImages(jsonObject); + return Content(JsonConvert.SerializeObject(result)); + } + + [AcceptVerbs("Post")] + [HttpPost("Bookmarks")] + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] + [Route("[controller]/Bookmarks")] + //Post action for processing the bookmarks from the PDF documents + public IActionResult OnPostBookmarks([FromBody] Dictionary jsonObject) + { + PdfRenderer pdfviewer = new PdfRenderer(_cache); + object jsonResult = pdfviewer.GetBookmarks(jsonObject); + return Content(JsonConvert.SerializeObject(jsonResult)); + } + + [AcceptVerbs("Post")] + [HttpPost("RenderAnnotationComments")] + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] + [Route("[controller]/RenderAnnotationComments")] + //Post action for rendering the annotation comments + public IActionResult OnPostRenderAnnotationComments([FromBody] Dictionary jsonObject) + { + PdfRenderer pdfviewer = new PdfRenderer(_cache); + object jsonResult = pdfviewer.GetAnnotationComments(jsonObject); + return Content(JsonConvert.SerializeObject(jsonResult)); + } + + [AcceptVerbs("Post")] + [HttpPost("ExportAnnotations")] + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] + [Route("[controller]/ExportAnnotations")] + //Post action for exporting the annotations + public IActionResult OnPostExportAnnotations([FromBody] Dictionary jsonObject) + { + PdfRenderer pdfviewer = new PdfRenderer(_cache); + string jsonResult = pdfviewer.ExportAnnotation(jsonObject); + return Content(jsonResult); + } + [AcceptVerbs("Post")] + [HttpPost("ImportAnnotations")] + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] + [Route("[controller]/ImportAnnotations")] + //Post action for importing the annotations + public IActionResult OnPostImportAnnotations([FromBody] Dictionary jsonObject) + { + PdfRenderer pdfviewer = new PdfRenderer(_cache); + string jsonResult = string.Empty; + object JsonResult; + if (jsonObject != null && jsonObject.ContainsKey("fileName")) + { + string documentPath = GetDocumentPath(jsonObject["fileName"]); + if (!string.IsNullOrEmpty(documentPath)) + { + jsonResult = System.IO.File.ReadAllText(documentPath); + } + else + { + return this.Content(jsonObject["document"] + " is not found"); + } + } + else + { + string extension = Path.GetExtension(jsonObject["importedData"]); + if (extension != ".xfdf") + { + JsonResult = pdfviewer.ImportAnnotation(jsonObject); + return Content(JsonConvert.SerializeObject(JsonResult)); + } + else + { + string documentPath = GetDocumentPath(jsonObject["importedData"]); + if (!string.IsNullOrEmpty(documentPath)) + { + byte[] bytes = System.IO.File.ReadAllBytes(documentPath); + jsonObject["importedData"] = Convert.ToBase64String(bytes); + JsonResult = pdfviewer.ImportAnnotation(jsonObject); + return Content(JsonConvert.SerializeObject(JsonResult)); + } + else + { + return this.Content(jsonObject["document"] + " is not found"); + } + } + } + return Content(jsonResult); + } + + [HttpPost("Download")] + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] + [Route("[controller]/Download")] + //Post action for downloading the PDF documents + public IActionResult OnPostDownload([FromBody] Dictionary jsonObject) + { + PdfRenderer pdfviewer = new PdfRenderer(_cache); + string documentBase = pdfviewer.GetDocumentAsBase64(jsonObject); + return Content(documentBase); + } + + [HttpPost("PrintImages")] + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] + [Route("[controller]/PrintImages")] + //Post action for printing the PDF documents + public IActionResult OnPostPrintImages([FromBody] Dictionary jsonObject) + { + PdfRenderer pdfviewer = new PdfRenderer(_cache); + object pageImage = pdfviewer.GetPrintImage(jsonObject); + return Content(JsonConvert.SerializeObject(pageImage)); + } + + //Gets the path of the PDF document + private string GetDocumentPath(string document) + { + string documentPath = string.Empty; + if (!System.IO.File.Exists(document)) + { + string basePath = _hostingEnvironment.WebRootPath; + string dataPath = string.Empty; + dataPath = basePath + "/"; + if (System.IO.File.Exists(dataPath + (document))) + documentPath = dataPath + document; + } + else + { + documentPath = document; + } + return documentPath; } } -} +} \ No newline at end of file