From ce4fc851b906497e524ca3fdf6641c1d821ee4d4 Mon Sep 17 00:00:00 2001 From: Uma-SF4267 Date: Thu, 19 Sep 2024 12:37:22 +0530 Subject: [PATCH 1/4] 896384: Added validatePassword action to get whether document is password protected. --- .../App_Start/FilterConfig.cs | 1 + .../App_Start/JsonMaxLengthFilterConfig.cs | 18 ++++++ .../Controllers/HomeController.cs | 61 +++++++++++++++++++ .../Views/Shared/_Layout.cshtml | 4 +- .../ASP.NET MVC Razor Examples/Web.config | 37 +++++++---- 5 files changed, 108 insertions(+), 13 deletions(-) create mode 100644 PDFViewer/ASP.NET MVC Razor Examples/App_Start/JsonMaxLengthFilterConfig.cs diff --git a/PDFViewer/ASP.NET MVC Razor Examples/App_Start/FilterConfig.cs b/PDFViewer/ASP.NET MVC Razor Examples/App_Start/FilterConfig.cs index 39e7832..501be26 100644 --- a/PDFViewer/ASP.NET MVC Razor Examples/App_Start/FilterConfig.cs +++ b/PDFViewer/ASP.NET MVC Razor Examples/App_Start/FilterConfig.cs @@ -8,6 +8,7 @@ public class FilterConfig public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); + filters.Add(new JsonMaxLengthAttribute()); } } } diff --git a/PDFViewer/ASP.NET MVC Razor Examples/App_Start/JsonMaxLengthFilterConfig.cs b/PDFViewer/ASP.NET MVC Razor Examples/App_Start/JsonMaxLengthFilterConfig.cs new file mode 100644 index 0000000..7755129 --- /dev/null +++ b/PDFViewer/ASP.NET MVC Razor Examples/App_Start/JsonMaxLengthFilterConfig.cs @@ -0,0 +1,18 @@ +using System.Web; +using System.Web.Mvc; + +namespace PDFViewerSample +{ + public class JsonMaxLengthAttribute : ActionFilterAttribute + { + public override void OnActionExecuting(ActionExecutingContext filterContext) + { + var jsonResult = filterContext.Result as JsonResult; + if (jsonResult != null) + { + jsonResult.MaxJsonLength = int.MaxValue; // Set maximum length + } + base.OnActionExecuting(filterContext); + } + } +} diff --git a/PDFViewer/ASP.NET MVC Razor Examples/Controllers/HomeController.cs b/PDFViewer/ASP.NET MVC Razor Examples/Controllers/HomeController.cs index cc93019..a069295 100644 --- a/PDFViewer/ASP.NET MVC Razor Examples/Controllers/HomeController.cs +++ b/PDFViewer/ASP.NET MVC Razor Examples/Controllers/HomeController.cs @@ -60,6 +60,57 @@ public ActionResult Load(jsonObjects jsonObject) return Content(JsonConvert.SerializeObject(jsonResult)); } + [System.Web.Mvc.HttpPost] + public ActionResult ValidatePassword(jsonObjects jsonObject) + { + PdfRenderer pdfviewer = new PdfRenderer(); + MemoryStream stream = new MemoryStream(); + var jsonData = JsonConverter(jsonObject); + object jsonResult = new object(); + if (jsonObject != null && jsonData.ContainsKey("document")) + { + if (bool.Parse(jsonData["isFileName"])) + { + string documentPath = GetDocumentPath(jsonData["document"]); + + if (!string.IsNullOrEmpty(documentPath)) + { + byte[] bytes = System.IO.File.ReadAllBytes(documentPath); + stream = new MemoryStream(bytes); + + } + else + { + string fileName = jsonData["document"].Split(new string[] { "://" }, StringSplitOptions.None)[0]; + if (fileName == "http" || fileName == "https") + { + var WebClient = new WebClient(); + byte[] pdfDoc = WebClient.DownloadData(jsonData["document"]); + stream = new MemoryStream(pdfDoc); + } + else + { + return this.Content(jsonData["document"] + " is not found"); + } + + } + } + else + { + byte[] bytes = Convert.FromBase64String(jsonData["document"]); + stream = new MemoryStream(bytes); + + } + } + string password = null; + if (jsonObject != null && jsonData.ContainsKey("password")) + { + password = jsonData["password"]; + } + var result = pdfviewer.Load(stream, password); + return Content(JsonConvert.SerializeObject(result)); + } + public Dictionary JsonConverter(jsonObjects results) { Dictionary resultObjects = new Dictionary(); @@ -133,6 +184,15 @@ public ActionResult RenderPdfPages(jsonObjects jsonObject) return Content(JsonConvert.SerializeObject(jsonResult)); } + [System.Web.Mvc.HttpPost] + public ActionResult RenderPdfTexts(jsonObjects jsonObject) + { + PdfRenderer pdfviewer = new PdfRenderer(); + var jsonData = JsonConverter(jsonObject); + object jsonResult = pdfviewer.GetDocumentText(jsonData); + return Content(JsonConvert.SerializeObject(jsonResult)); + } + [System.Web.Mvc.HttpPost] public ActionResult Unload(jsonObjects jsonObject) { @@ -279,5 +339,6 @@ public class jsonObjects public string isFormFieldAnnotationsExist { get; set; } public string documentLiveCount { get; set; } public string annotationDataFormat { get; set; } + public string organizePages { get; set; } } } \ No newline at end of file diff --git a/PDFViewer/ASP.NET MVC Razor Examples/Views/Shared/_Layout.cshtml b/PDFViewer/ASP.NET MVC Razor Examples/Views/Shared/_Layout.cshtml index 8f88499..615e6a3 100644 --- a/PDFViewer/ASP.NET MVC Razor Examples/Views/Shared/_Layout.cshtml +++ b/PDFViewer/ASP.NET MVC Razor Examples/Views/Shared/_Layout.cshtml @@ -5,8 +5,8 @@ @ViewBag.Title - My ASP.NET Application @Styles.Render("~/Content/css") - - + +