diff --git a/ASP.NET Core/PdfViewerWebService_5.0/Controllers/PdfViewerController.cs b/ASP.NET Core/PdfViewerWebService_5.0/Controllers/PdfViewerController.cs index 0209532..627fbf1 100644 --- a/ASP.NET Core/PdfViewerWebService_5.0/Controllers/PdfViewerController.cs +++ b/ASP.NET Core/PdfViewerWebService_5.0/Controllers/PdfViewerController.cs @@ -7,6 +7,13 @@ using System.Collections.Generic; using System.IO; using System.Net; +using System.Drawing; +using Syncfusion.Pdf.Parsing; +using Syncfusion.Pdf; +using Syncfusion.Pdf.Graphics; +using Syncfusion.Pdf.Interactive; +using Syncfusion.Pdf.Redaction; +using System.Linq; namespace PdfViewerService2.Controllers { @@ -318,6 +325,172 @@ public IActionResult ImportFormFields([FromBody] Dictionary json return Content(JsonConvert.SerializeObject(pageImage)); } + [HttpPost("Redaction")] + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] + [Route("[controller]/Redaction")] + public IActionResult Redaction([FromBody] Dictionary jsonObject) + { + string RedactionText = "Redacted"; + var finalbase64 = string.Empty; + if (jsonObject != null && jsonObject.ContainsKey("base64String")) + { + string base64 = jsonObject["base64String"]; + string base64String = base64.Split(new string[] { "data:application/pdf;base64," }, StringSplitOptions.None)[1]; + if (base64String != null || base64String != string.Empty) + { + byte[] byteArray = Convert.FromBase64String(base64String); + Console.WriteLine("redaction"); + PdfLoadedDocument loadedDocument = new PdfLoadedDocument(byteArray); + foreach (PdfLoadedPage loadedPage in loadedDocument.Pages) + { + List removeItems = new List(); + foreach (PdfLoadedAnnotation annotation in loadedPage.Annotations) + { + if (annotation is PdfLoadedRectangleAnnotation) + { + if (annotation.Author == "Redaction") + { + // Add the annotation to the removeItems list + removeItems.Add(annotation); + // Create a new redaction with the annotation bounds and color + PdfRedaction redaction = new PdfRedaction(annotation.Bounds, annotation.Color); + // Add the redaction to the page + loadedPage.AddRedaction(redaction); + annotation.Flatten = true; + } + if (annotation.Author == "Text") + { + // Add the annotation to the removeItems list + removeItems.Add(annotation); + // Create a new redaction with the annotation bounds and color + PdfRedaction redaction = new PdfRedaction(annotation.Bounds); + //Set the font family and font size + PdfStandardFont font = new PdfStandardFont(PdfFontFamily.Courier, 8); + //Create the appearance like repeated text in the redaction area + CreateRedactionAppearance(redaction.Appearance.Graphics, PdfTextAlignment.Left, true, new SizeF(annotation.Bounds.Width, annotation.Bounds.Height), RedactionText, font, PdfBrushes.Red); + // Add the redaction to the page + loadedPage.AddRedaction(redaction); + annotation.Flatten = true; + } + //Apply the pattern for the Redaction + if (annotation.Author == "Pattern") + { + // Add the annotation to the removeItems list + removeItems.Add(annotation); + // Create a new redaction with the annotation bounds and color + PdfRedaction redaction = new PdfRedaction(annotation.Bounds); + Syncfusion.Drawing.RectangleF rect = new Syncfusion.Drawing.RectangleF(0, 0, 8, 8); + PdfTilingBrush tillingBrush = new PdfTilingBrush(rect); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Gray, new Syncfusion.Drawing.RectangleF(0, 0, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.White, new Syncfusion.Drawing.RectangleF(2, 0, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(4, 0, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.DarkGray, new Syncfusion.Drawing.RectangleF(6, 0, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.White, new Syncfusion.Drawing.RectangleF(0, 2, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(2, 2, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Black, new Syncfusion.Drawing.RectangleF(4, 2, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(6, 2, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(0, 4, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.DarkGray, new Syncfusion.Drawing.RectangleF(2, 4, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(4, 4, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.White, new Syncfusion.Drawing.RectangleF(6, 4, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Black, new Syncfusion.Drawing.RectangleF(0, 6, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(2, 6, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Black, new Syncfusion.Drawing.RectangleF(4, 6, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.DarkGray, new Syncfusion.Drawing.RectangleF(6, 6, 2, 2)); + rect = new Syncfusion.Drawing.RectangleF(0, 0, 16, 14); + PdfTilingBrush tillingBrushNew = new PdfTilingBrush(rect); + tillingBrushNew.Graphics.DrawRectangle(tillingBrush, rect); + //Set the pattern for the redaction area + redaction.Appearance.Graphics.DrawRectangle(tillingBrushNew, new Syncfusion.Drawing.RectangleF(0, 0, annotation.Bounds.Width, annotation.Bounds.Height)); + // Add the redaction to the page + loadedPage.AddRedaction(redaction); + annotation.Flatten = true; + } + } + else if (annotation is PdfLoadedRubberStampAnnotation) + { + if (annotation.Author == "Image") + { + Stream[] images = PdfLoadedRubberStampAnnotationExtension.GetImages(annotation as PdfLoadedRubberStampAnnotation); + // Create a new redaction with the annotation bounds and color + PdfRedaction redaction = new PdfRedaction(annotation.Bounds); + images[0].Position = 0; + PdfImage image = new PdfBitmap(images[0]); + //Apply the image to redaction area + redaction.Appearance.Graphics.DrawImage(image, new Syncfusion.Drawing.RectangleF(0, 0, annotation.Bounds.Width, annotation.Bounds.Height)); + // Add the redaction to the page + loadedPage.AddRedaction(redaction); + annotation.Flatten = true; + } + } + } + foreach (PdfLoadedAnnotation annotation1 in removeItems) + { + loadedPage.Annotations.Remove(annotation1); + } + } + loadedDocument.Redact(); + MemoryStream stream = new MemoryStream(); + loadedDocument.Save(stream); + stream.Position = 0; + loadedDocument.Close(true); + byteArray = stream.ToArray(); + finalbase64 = "data:application/pdf;base64," + Convert.ToBase64String(byteArray); + stream.Dispose(); + return Content(finalbase64); + } + } + + return Content("data:application/pdf;base64," + ""); + } + + //The Method used for apply the text in the full area of redaction rectangle + private static void CreateRedactionAppearance(PdfGraphics graphics, PdfTextAlignment alignment, bool repeat, SizeF size, string overlayText, PdfFont font, PdfBrush textcolor) + { + float col = 0, row; + if (font == null) font = new PdfStandardFont(PdfFontFamily.Helvetica, 10); + int textAlignment = Convert.ToInt32(alignment); + float y = 0, x = 0, diff = 0; + Syncfusion.Drawing.RectangleF rect; + Syncfusion.Drawing.SizeF textsize = font.MeasureString(overlayText); + + if (repeat) + { + col = size.Width / textsize.Width; + row = (float)Math.Floor(size.Height / font.Size); + diff = Math.Abs(size.Width - (float)(Math.Floor(col) * textsize.Width)); + if (textAlignment == 1) + x = diff / 2; + if (textAlignment == 2) + x = diff; + for (int i = 1; i < col; i++) + { + for (int j = 0; j < row; j++) + { + rect = new Syncfusion.Drawing.RectangleF(x, y, 0, 0); + graphics.DrawString(overlayText, font, textcolor, rect); + y = y + font.Size; + } + x = x + textsize.Width; + y = 0; + } + } + else + { + diff = Math.Abs(size.Width - textsize.Width); + if (textAlignment == 1) + { + x = diff / 2; + } + if (textAlignment == 2) + { + x = diff; + } + rect = new Syncfusion.Drawing.RectangleF(x, 0, 0, 0); + graphics.DrawString(overlayText, font, textcolor, rect); + } + } + //Gets the path of the PDF document private string GetDocumentPath(string document) { diff --git a/ASP.NET Core/PdfViewerWebService_5.0/PdfViewerWebService_5.0.csproj b/ASP.NET Core/PdfViewerWebService_5.0/PdfViewerWebService_5.0.csproj index 240dfed..ca15a70 100644 --- a/ASP.NET Core/PdfViewerWebService_5.0/PdfViewerWebService_5.0.csproj +++ b/ASP.NET Core/PdfViewerWebService_5.0/PdfViewerWebService_5.0.csproj @@ -8,7 +8,11 @@ - + + + + + diff --git a/ASP.NET Core/PdfViewerWebService_6.0 - Minimal API/PdfViewerWebService_6.0.csproj b/ASP.NET Core/PdfViewerWebService_6.0 - Minimal API/PdfViewerWebService_6.0.csproj index c879298..405a858 100644 --- a/ASP.NET Core/PdfViewerWebService_6.0 - Minimal API/PdfViewerWebService_6.0.csproj +++ b/ASP.NET Core/PdfViewerWebService_6.0 - Minimal API/PdfViewerWebService_6.0.csproj @@ -9,7 +9,11 @@ - + + + + + diff --git a/ASP.NET Core/PdfViewerWebService_6.0 - Minimal API/Program.cs b/ASP.NET Core/PdfViewerWebService_6.0 - Minimal API/Program.cs index 951e528..1922896 100644 --- a/ASP.NET Core/PdfViewerWebService_6.0 - Minimal API/Program.cs +++ b/ASP.NET Core/PdfViewerWebService_6.0 - Minimal API/Program.cs @@ -4,6 +4,12 @@ using Newtonsoft.Json.Serialization; using Syncfusion.EJ2.PdfViewer; using System.Net; +using System.Drawing; +using Syncfusion.Pdf.Parsing; +using Syncfusion.Pdf; +using Syncfusion.Pdf.Graphics; +using Syncfusion.Pdf.Interactive; +using Syncfusion.Pdf.Redaction; var builder = WebApplication.CreateBuilder(args); var MyAllowSpecificOrigins = "MyPolicy"; @@ -271,6 +277,171 @@ object pageImage = pdfviewer.GetPrintImage(jsonObject); return Results.Content(JsonConvert.SerializeObject(pageImage)); }); + +app.MapPost("pdfviewer/Redaction", (Dictionary args) => +{ + Dictionary jsonObject = args.ToDictionary(k => k.Key, k => k.Value?.ToString()); + string RedactionText = "Redacted"; + var finalbase64 = string.Empty; + if (jsonObject != null && jsonObject.ContainsKey("base64String")) + { + string base64 = jsonObject["base64String"]; + string base64String = base64.Split(new string[] { "data:application/pdf;base64," }, StringSplitOptions.None)[1]; + if (base64String != null || base64String != string.Empty) + { + byte[] byteArray = Convert.FromBase64String(base64String); + Console.WriteLine("redaction"); + PdfLoadedDocument loadedDocument = new PdfLoadedDocument(byteArray); + foreach (PdfLoadedPage loadedPage in loadedDocument.Pages) + { + List removeItems = new List(); + foreach (PdfLoadedAnnotation annotation in loadedPage.Annotations) + { + if (annotation is PdfLoadedRectangleAnnotation) + { + if (annotation.Author == "Redaction") + { + // Add the annotation to the removeItems list + removeItems.Add(annotation); + // Create a new redaction with the annotation bounds and color + PdfRedaction redaction = new PdfRedaction(annotation.Bounds, annotation.Color); + // Add the redaction to the page + loadedPage.AddRedaction(redaction); + annotation.Flatten = true; + } + if (annotation.Author == "Text") + { + // Add the annotation to the removeItems list + removeItems.Add(annotation); + // Create a new redaction with the annotation bounds and color + PdfRedaction redaction = new PdfRedaction(annotation.Bounds); + //Set the font family and font size + PdfStandardFont font = new PdfStandardFont(PdfFontFamily.Courier, 8); + //Create the appearance like repeated text in the redaction area + CreateRedactionAppearance(redaction.Appearance.Graphics, PdfTextAlignment.Left, true, new SizeF(annotation.Bounds.Width, annotation.Bounds.Height), RedactionText, font, PdfBrushes.Red); + // Add the redaction to the page + loadedPage.AddRedaction(redaction); + annotation.Flatten = true; + } + //Apply the pattern for the Redaction + if (annotation.Author == "Pattern") + { + // Add the annotation to the removeItems list + removeItems.Add(annotation); + // Create a new redaction with the annotation bounds and color + PdfRedaction redaction = new PdfRedaction(annotation.Bounds); + Syncfusion.Drawing.RectangleF rect = new Syncfusion.Drawing.RectangleF(0, 0, 8, 8); + PdfTilingBrush tillingBrush = new PdfTilingBrush(rect); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Gray, new Syncfusion.Drawing.RectangleF(0, 0, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.White, new Syncfusion.Drawing.RectangleF(2, 0, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(4, 0, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.DarkGray, new Syncfusion.Drawing.RectangleF(6, 0, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.White, new Syncfusion.Drawing.RectangleF(0, 2, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(2, 2, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Black, new Syncfusion.Drawing.RectangleF(4, 2, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(6, 2, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(0, 4, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.DarkGray, new Syncfusion.Drawing.RectangleF(2, 4, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(4, 4, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.White, new Syncfusion.Drawing.RectangleF(6, 4, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Black, new Syncfusion.Drawing.RectangleF(0, 6, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(2, 6, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Black, new Syncfusion.Drawing.RectangleF(4, 6, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.DarkGray, new Syncfusion.Drawing.RectangleF(6, 6, 2, 2)); + rect = new Syncfusion.Drawing.RectangleF(0, 0, 16, 14); + PdfTilingBrush tillingBrushNew = new PdfTilingBrush(rect); + tillingBrushNew.Graphics.DrawRectangle(tillingBrush, rect); + //Set the pattern for the redaction area + redaction.Appearance.Graphics.DrawRectangle(tillingBrushNew, new Syncfusion.Drawing.RectangleF(0, 0, annotation.Bounds.Width, annotation.Bounds.Height)); + // Add the redaction to the page + loadedPage.AddRedaction(redaction); + annotation.Flatten = true; + } + } + else if (annotation is PdfLoadedRubberStampAnnotation) + { + if (annotation.Author == "Image") + { + Stream[] images = PdfLoadedRubberStampAnnotationExtension.GetImages(annotation as PdfLoadedRubberStampAnnotation); + // Create a new redaction with the annotation bounds and color + PdfRedaction redaction = new PdfRedaction(annotation.Bounds); + images[0].Position = 0; + PdfImage image = new PdfBitmap(images[0]); + //Apply the image to redaction area + redaction.Appearance.Graphics.DrawImage(image, new Syncfusion.Drawing.RectangleF(0, 0, annotation.Bounds.Width, annotation.Bounds.Height)); + // Add the redaction to the page + loadedPage.AddRedaction(redaction); + annotation.Flatten = true; + } + } + } + foreach (PdfLoadedAnnotation annotation1 in removeItems) + { + loadedPage.Annotations.Remove(annotation1); + } + } + loadedDocument.Redact(); + MemoryStream stream = new MemoryStream(); + loadedDocument.Save(stream); + stream.Position = 0; + loadedDocument.Close(true); + byteArray = stream.ToArray(); + finalbase64 = "data:application/pdf;base64," + Convert.ToBase64String(byteArray); + stream.Dispose(); + return finalbase64; + } + } + + return "data:application/pdf;base64," + ""; +}); + +//The Method used for apply the text in the full area of redaction rectangle +static void CreateRedactionAppearance(PdfGraphics graphics, PdfTextAlignment alignment, bool repeat, SizeF size, string overlayText, PdfFont font, PdfBrush textcolor) +{ + float col = 0, row; + if (font == null) font = new PdfStandardFont(PdfFontFamily.Helvetica, 10); + int textAlignment = Convert.ToInt32(alignment); + float y = 0, x = 0, diff = 0; + Syncfusion.Drawing.RectangleF rect; + Syncfusion.Drawing.SizeF textsize = font.MeasureString(overlayText); + + if (repeat) + { + col = size.Width / textsize.Width; + row = (float)Math.Floor(size.Height / font.Size); + diff = Math.Abs(size.Width - (float)(Math.Floor(col) * textsize.Width)); + if (textAlignment == 1) + x = diff / 2; + if (textAlignment == 2) + x = diff; + for (int i = 1; i < col; i++) + { + for (int j = 0; j < row; j++) + { + rect = new Syncfusion.Drawing.RectangleF(x, y, 0, 0); + graphics.DrawString(overlayText, font, textcolor, rect); + y = y + font.Size; + } + x = x + textsize.Width; + y = 0; + } + } + else + { + diff = Math.Abs(size.Width - textsize.Width); + if (textAlignment == 1) + { + x = diff / 2; + } + if (textAlignment == 2) + { + x = diff; + } + rect = new Syncfusion.Drawing.RectangleF(x, 0, 0, 0); + graphics.DrawString(overlayText, font, textcolor, rect); + } +} + string GetDocumentPath(string document) { string documentPath = string.Empty; diff --git a/ASP.NET Core/PdfViewerWebService_6.0/PdfViewerController.cs b/ASP.NET Core/PdfViewerWebService_6.0/PdfViewerController.cs index f30165c..e2d9319 100644 --- a/ASP.NET Core/PdfViewerWebService_6.0/PdfViewerController.cs +++ b/ASP.NET Core/PdfViewerWebService_6.0/PdfViewerController.cs @@ -7,6 +7,12 @@ using System.Collections.Generic; using System.IO; using System.Net; +using System.Drawing; +using Syncfusion.Pdf.Parsing; +using Syncfusion.Pdf; +using Syncfusion.Pdf.Graphics; +using Syncfusion.Pdf.Interactive; +using Syncfusion.Pdf.Redaction; namespace PdfViewerWebService { @@ -318,6 +324,172 @@ public IActionResult ImportFormFields([FromBody] Dictionary json return Content(JsonConvert.SerializeObject(pageImage)); } + [HttpPost("Redaction")] + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] + [Route("[controller]/Redaction")] + public IActionResult Redaction([FromBody] Dictionary jsonObject) + { + string RedactionText = "Redacted"; + var finalbase64 = string.Empty; + if (jsonObject != null && jsonObject.ContainsKey("base64String")) + { + string base64 = jsonObject["base64String"]; + string base64String = base64.Split(new string[] { "data:application/pdf;base64," }, StringSplitOptions.None)[1]; + if (base64String != null || base64String != string.Empty) + { + byte[] byteArray = Convert.FromBase64String(base64String); + Console.WriteLine("redaction"); + PdfLoadedDocument loadedDocument = new PdfLoadedDocument(byteArray); + foreach (PdfLoadedPage loadedPage in loadedDocument.Pages) + { + List removeItems = new List(); + foreach (PdfLoadedAnnotation annotation in loadedPage.Annotations) + { + if (annotation is PdfLoadedRectangleAnnotation) + { + if (annotation.Author == "Redaction") + { + // Add the annotation to the removeItems list + removeItems.Add(annotation); + // Create a new redaction with the annotation bounds and color + PdfRedaction redaction = new PdfRedaction(annotation.Bounds, annotation.Color); + // Add the redaction to the page + loadedPage.AddRedaction(redaction); + annotation.Flatten = true; + } + if (annotation.Author == "Text") + { + // Add the annotation to the removeItems list + removeItems.Add(annotation); + // Create a new redaction with the annotation bounds and color + PdfRedaction redaction = new PdfRedaction(annotation.Bounds); + //Set the font family and font size + PdfStandardFont font = new PdfStandardFont(PdfFontFamily.Courier, 8); + //Create the appearance like repeated text in the redaction area + CreateRedactionAppearance(redaction.Appearance.Graphics, PdfTextAlignment.Left, true, new SizeF(annotation.Bounds.Width, annotation.Bounds.Height), RedactionText, font, PdfBrushes.Red); + // Add the redaction to the page + loadedPage.AddRedaction(redaction); + annotation.Flatten = true; + } + //Apply the pattern for the Redaction + if (annotation.Author == "Pattern") + { + // Add the annotation to the removeItems list + removeItems.Add(annotation); + // Create a new redaction with the annotation bounds and color + PdfRedaction redaction = new PdfRedaction(annotation.Bounds); + Syncfusion.Drawing.RectangleF rect = new Syncfusion.Drawing.RectangleF(0, 0, 8, 8); + PdfTilingBrush tillingBrush = new PdfTilingBrush(rect); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Gray, new Syncfusion.Drawing.RectangleF(0, 0, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.White, new Syncfusion.Drawing.RectangleF(2, 0, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(4, 0, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.DarkGray, new Syncfusion.Drawing.RectangleF(6, 0, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.White, new Syncfusion.Drawing.RectangleF(0, 2, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(2, 2, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Black, new Syncfusion.Drawing.RectangleF(4, 2, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(6, 2, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(0, 4, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.DarkGray, new Syncfusion.Drawing.RectangleF(2, 4, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(4, 4, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.White, new Syncfusion.Drawing.RectangleF(6, 4, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Black, new Syncfusion.Drawing.RectangleF(0, 6, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(2, 6, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Black, new Syncfusion.Drawing.RectangleF(4, 6, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.DarkGray, new Syncfusion.Drawing.RectangleF(6, 6, 2, 2)); + rect = new Syncfusion.Drawing.RectangleF(0, 0, 16, 14); + PdfTilingBrush tillingBrushNew = new PdfTilingBrush(rect); + tillingBrushNew.Graphics.DrawRectangle(tillingBrush, rect); + //Set the pattern for the redaction area + redaction.Appearance.Graphics.DrawRectangle(tillingBrushNew, new Syncfusion.Drawing.RectangleF(0, 0, annotation.Bounds.Width, annotation.Bounds.Height)); + // Add the redaction to the page + loadedPage.AddRedaction(redaction); + annotation.Flatten = true; + } + } + else if (annotation is PdfLoadedRubberStampAnnotation) + { + if (annotation.Author == "Image") + { + Stream[] images = PdfLoadedRubberStampAnnotationExtension.GetImages(annotation as PdfLoadedRubberStampAnnotation); + // Create a new redaction with the annotation bounds and color + PdfRedaction redaction = new PdfRedaction(annotation.Bounds); + images[0].Position = 0; + PdfImage image = new PdfBitmap(images[0]); + //Apply the image to redaction area + redaction.Appearance.Graphics.DrawImage(image, new Syncfusion.Drawing.RectangleF(0, 0, annotation.Bounds.Width, annotation.Bounds.Height)); + // Add the redaction to the page + loadedPage.AddRedaction(redaction); + annotation.Flatten = true; + } + } + } + foreach (PdfLoadedAnnotation annotation1 in removeItems) + { + loadedPage.Annotations.Remove(annotation1); + } + } + loadedDocument.Redact(); + MemoryStream stream = new MemoryStream(); + loadedDocument.Save(stream); + stream.Position = 0; + loadedDocument.Close(true); + byteArray = stream.ToArray(); + finalbase64 = "data:application/pdf;base64," + Convert.ToBase64String(byteArray); + stream.Dispose(); + return Content(finalbase64); + } + } + + return Content("data:application/pdf;base64," + ""); + } + + //The Method used for apply the text in the full area of redaction rectangle + private static void CreateRedactionAppearance(PdfGraphics graphics, PdfTextAlignment alignment, bool repeat, SizeF size, string overlayText, PdfFont font, PdfBrush textcolor) + { + float col = 0, row; + if (font == null) font = new PdfStandardFont(PdfFontFamily.Helvetica, 10); + int textAlignment = Convert.ToInt32(alignment); + float y = 0, x = 0, diff = 0; + Syncfusion.Drawing.RectangleF rect; + Syncfusion.Drawing.SizeF textsize = font.MeasureString(overlayText); + + if (repeat) + { + col = size.Width / textsize.Width; + row = (float)Math.Floor(size.Height / font.Size); + diff = Math.Abs(size.Width - (float)(Math.Floor(col) * textsize.Width)); + if (textAlignment == 1) + x = diff / 2; + if (textAlignment == 2) + x = diff; + for (int i = 1; i < col; i++) + { + for (int j = 0; j < row; j++) + { + rect = new Syncfusion.Drawing.RectangleF(x, y, 0, 0); + graphics.DrawString(overlayText, font, textcolor, rect); + y = y + font.Size; + } + x = x + textsize.Width; + y = 0; + } + } + else + { + diff = Math.Abs(size.Width - textsize.Width); + if (textAlignment == 1) + { + x = diff / 2; + } + if (textAlignment == 2) + { + x = diff; + } + rect = new Syncfusion.Drawing.RectangleF(x, 0, 0, 0); + graphics.DrawString(overlayText, font, textcolor, rect); + } + } + //Gets the path of the PDF document private string GetDocumentPath(string document) { diff --git a/ASP.NET Core/PdfViewerWebService_6.0/PdfViewerWebService_6.0.csproj b/ASP.NET Core/PdfViewerWebService_6.0/PdfViewerWebService_6.0.csproj index b44f048..c9a21e1 100644 --- a/ASP.NET Core/PdfViewerWebService_6.0/PdfViewerWebService_6.0.csproj +++ b/ASP.NET Core/PdfViewerWebService_6.0/PdfViewerWebService_6.0.csproj @@ -8,7 +8,11 @@ - + + + + + diff --git a/ASP.NET Core/PdfViewerWebService_8.0 _redis_config/PdfViewerWebService_8.0/PdfViewerController.cs b/ASP.NET Core/PdfViewerWebService_8.0 _redis_config/PdfViewerWebService_8.0/PdfViewerController.cs index 29be713..9b0b269 100644 --- a/ASP.NET Core/PdfViewerWebService_8.0 _redis_config/PdfViewerWebService_8.0/PdfViewerController.cs +++ b/ASP.NET Core/PdfViewerWebService_8.0 _redis_config/PdfViewerWebService_8.0/PdfViewerController.cs @@ -13,6 +13,11 @@ using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Configuration; using System.Net.Http; +using Syncfusion.Pdf.Parsing; +using Syncfusion.Pdf; +using Syncfusion.Pdf.Graphics; +using Syncfusion.Pdf.Interactive; +using Syncfusion.Pdf.Redaction; namespace ej2_pdfviewer_web_service.Controllers { @@ -215,6 +220,172 @@ public IActionResult Download([FromBody] Dictionary args) return Content(documentBase); } + [HttpPost] + [EnableCors("AllowAllOrigins")] + [Route("Redaction")] + public IActionResult Redaction([FromBody] Dictionary jsonObject) + { + string RedactionText = "Redacted"; + var finalbase64 = string.Empty; + if (jsonObject != null && jsonObject.ContainsKey("base64String")) + { + string base64 = jsonObject["base64String"]; + string base64String = base64.Split(new string[] { "data:application/pdf;base64," }, StringSplitOptions.None)[1]; + if (base64String != null || base64String != string.Empty) + { + byte[] byteArray = Convert.FromBase64String(base64String); + Console.WriteLine("redaction"); + PdfLoadedDocument loadedDocument = new PdfLoadedDocument(byteArray); + foreach (PdfLoadedPage loadedPage in loadedDocument.Pages) + { + List removeItems = new List(); + foreach (PdfLoadedAnnotation annotation in loadedPage.Annotations) + { + if (annotation is PdfLoadedRectangleAnnotation) + { + if (annotation.Author == "Redaction") + { + // Add the annotation to the removeItems list + removeItems.Add(annotation); + // Create a new redaction with the annotation bounds and color + PdfRedaction redaction = new PdfRedaction(annotation.Bounds, annotation.Color); + // Add the redaction to the page + loadedPage.AddRedaction(redaction); + annotation.Flatten = true; + } + if (annotation.Author == "Text") + { + // Add the annotation to the removeItems list + removeItems.Add(annotation); + // Create a new redaction with the annotation bounds and color + PdfRedaction redaction = new PdfRedaction(annotation.Bounds); + //Set the font family and font size + PdfStandardFont font = new PdfStandardFont(PdfFontFamily.Courier, 8); + //Create the appearance like repeated text in the redaction area + CreateRedactionAppearance(redaction.Appearance.Graphics, PdfTextAlignment.Left, true, new SizeF(annotation.Bounds.Width, annotation.Bounds.Height), RedactionText, font, PdfBrushes.Red); + // Add the redaction to the page + loadedPage.AddRedaction(redaction); + annotation.Flatten = true; + } + //Apply the pattern for the Redaction + if (annotation.Author == "Pattern") + { + // Add the annotation to the removeItems list + removeItems.Add(annotation); + // Create a new redaction with the annotation bounds and color + PdfRedaction redaction = new PdfRedaction(annotation.Bounds); + Syncfusion.Drawing.RectangleF rect = new Syncfusion.Drawing.RectangleF(0, 0, 8, 8); + PdfTilingBrush tillingBrush = new PdfTilingBrush(rect); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Gray, new Syncfusion.Drawing.RectangleF(0, 0, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.White, new Syncfusion.Drawing.RectangleF(2, 0, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(4, 0, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.DarkGray, new Syncfusion.Drawing.RectangleF(6, 0, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.White, new Syncfusion.Drawing.RectangleF(0, 2, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(2, 2, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Black, new Syncfusion.Drawing.RectangleF(4, 2, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(6, 2, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(0, 4, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.DarkGray, new Syncfusion.Drawing.RectangleF(2, 4, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(4, 4, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.White, new Syncfusion.Drawing.RectangleF(6, 4, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Black, new Syncfusion.Drawing.RectangleF(0, 6, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(2, 6, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Black, new Syncfusion.Drawing.RectangleF(4, 6, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.DarkGray, new Syncfusion.Drawing.RectangleF(6, 6, 2, 2)); + rect = new Syncfusion.Drawing.RectangleF(0, 0, 16, 14); + PdfTilingBrush tillingBrushNew = new PdfTilingBrush(rect); + tillingBrushNew.Graphics.DrawRectangle(tillingBrush, rect); + //Set the pattern for the redaction area + redaction.Appearance.Graphics.DrawRectangle(tillingBrushNew, new Syncfusion.Drawing.RectangleF(0, 0, annotation.Bounds.Width, annotation.Bounds.Height)); + // Add the redaction to the page + loadedPage.AddRedaction(redaction); + annotation.Flatten = true; + } + } + else if (annotation is PdfLoadedRubberStampAnnotation) + { + if (annotation.Author == "Image") + { + Stream[] images = PdfLoadedRubberStampAnnotationExtension.GetImages(annotation as PdfLoadedRubberStampAnnotation); + // Create a new redaction with the annotation bounds and color + PdfRedaction redaction = new PdfRedaction(annotation.Bounds); + images[0].Position = 0; + PdfImage image = new PdfBitmap(images[0]); + //Apply the image to redaction area + redaction.Appearance.Graphics.DrawImage(image, new Syncfusion.Drawing.RectangleF(0, 0, annotation.Bounds.Width, annotation.Bounds.Height)); + // Add the redaction to the page + loadedPage.AddRedaction(redaction); + annotation.Flatten = true; + } + } + } + foreach (PdfLoadedAnnotation annotation1 in removeItems) + { + loadedPage.Annotations.Remove(annotation1); + } + } + loadedDocument.Redact(); + MemoryStream stream = new MemoryStream(); + loadedDocument.Save(stream); + stream.Position = 0; + loadedDocument.Close(true); + byteArray = stream.ToArray(); + finalbase64 = "data:application/pdf;base64," + Convert.ToBase64String(byteArray); + stream.Dispose(); + return Content(finalbase64); + } + } + + return Content("data:application/pdf;base64," + ""); + } + + //The Method used for apply the text in the full area of redaction rectangle + private static void CreateRedactionAppearance(PdfGraphics graphics, PdfTextAlignment alignment, bool repeat, SizeF size, string overlayText, PdfFont font, PdfBrush textcolor) + { + float col = 0, row; + if (font == null) font = new PdfStandardFont(PdfFontFamily.Helvetica, 10); + int textAlignment = Convert.ToInt32(alignment); + float y = 0, x = 0, diff = 0; + Syncfusion.Drawing.RectangleF rect; + Syncfusion.Drawing.SizeF textsize = font.MeasureString(overlayText); + + if (repeat) + { + col = size.Width / textsize.Width; + row = (float)Math.Floor(size.Height / font.Size); + diff = Math.Abs(size.Width - (float)(Math.Floor(col) * textsize.Width)); + if (textAlignment == 1) + x = diff / 2; + if (textAlignment == 2) + x = diff; + for (int i = 1; i < col; i++) + { + for (int j = 0; j < row; j++) + { + rect = new Syncfusion.Drawing.RectangleF(x, y, 0, 0); + graphics.DrawString(overlayText, font, textcolor, rect); + y = y + font.Size; + } + x = x + textsize.Width; + y = 0; + } + } + else + { + diff = Math.Abs(size.Width - textsize.Width); + if (textAlignment == 1) + { + x = diff / 2; + } + if (textAlignment == 2) + { + x = diff; + } + rect = new Syncfusion.Drawing.RectangleF(x, 0, 0, 0); + graphics.DrawString(overlayText, font, textcolor, rect); + } + } + [HttpPost] [EnableCors("AllowAllOrigins")] [Route("SaveUrl")] diff --git a/ASP.NET Core/PdfViewerWebService_8.0 _redis_config/PdfViewerWebService_8.0/PdfViewerWebService_8.0.csproj b/ASP.NET Core/PdfViewerWebService_8.0 _redis_config/PdfViewerWebService_8.0/PdfViewerWebService_8.0.csproj index d122fde..a7178fe 100644 --- a/ASP.NET Core/PdfViewerWebService_8.0 _redis_config/PdfViewerWebService_8.0/PdfViewerWebService_8.0.csproj +++ b/ASP.NET Core/PdfViewerWebService_8.0 _redis_config/PdfViewerWebService_8.0/PdfViewerWebService_8.0.csproj @@ -17,7 +17,11 @@ + + + + diff --git a/ASP.NET Core/PdfViewerWebService_8.0/PdfViewerWebService_8.0/PdfViewerController.cs b/ASP.NET Core/PdfViewerWebService_8.0/PdfViewerWebService_8.0/PdfViewerController.cs index b249f51..49bb668 100644 --- a/ASP.NET Core/PdfViewerWebService_8.0/PdfViewerWebService_8.0/PdfViewerController.cs +++ b/ASP.NET Core/PdfViewerWebService_8.0/PdfViewerWebService_8.0/PdfViewerController.cs @@ -1,12 +1,30 @@ -using Microsoft.AspNetCore.Hosting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Caching.Memory; -using Newtonsoft.Json; using Syncfusion.EJ2.PdfViewer; -using System; -using System.Collections.Generic; using System.IO; +using Newtonsoft.Json; +using Syncfusion.Pdf.Parsing; +using System.Security.Cryptography.X509Certificates; +using Syncfusion.Pdf.Security; +using Syncfusion.Pdf; using System.Net; +using Syncfusion.DocIORenderer; +using Syncfusion.Pdf.Graphics; +using Syncfusion.Presentation; +using Syncfusion.PresentationRenderer; +using Syncfusion.XlsIO; +using Syncfusion.XlsIORenderer; +using WFormatType = Syncfusion.DocIO.FormatType; +using Microsoft.AspNetCore.Cors; +using Syncfusion.Pdf.Interactive; +using Syncfusion.Pdf.Redaction; +using Syncfusion.Drawing; namespace PdfViewerWebService_8 { @@ -319,6 +337,282 @@ public IActionResult ImportFormFields([FromBody] Dictionary json return Content(JsonConvert.SerializeObject(pageImage)); } + [HttpPost("Redaction")] + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] + [Route("[controller]/Redaction")] + public IActionResult Redaction([FromBody] Dictionary jsonObject) + { + string RedactionText = "Redacted"; + var finalbase64 = string.Empty; + if (jsonObject != null && jsonObject.ContainsKey("base64String")) + { + string base64 = jsonObject["base64String"]; + string base64String = base64.Split(new string[] { "data:application/pdf;base64," }, StringSplitOptions.None)[1]; + if (base64String != null || base64String != string.Empty) + { + byte[] byteArray = Convert.FromBase64String(base64String); + Console.WriteLine("redaction"); + PdfLoadedDocument loadedDocument = new PdfLoadedDocument(byteArray); + foreach (PdfLoadedPage loadedPage in loadedDocument.Pages) + { + List removeItems = new List(); + foreach (PdfLoadedAnnotation annotation in loadedPage.Annotations) + { + if (annotation is PdfLoadedRectangleAnnotation) + { + if (annotation.Author == "Redaction") + { + // Add the annotation to the removeItems list + removeItems.Add(annotation); + // Create a new redaction with the annotation bounds and color + PdfRedaction redaction = new PdfRedaction(annotation.Bounds, annotation.Color); + // Add the redaction to the page + loadedPage.AddRedaction(redaction); + annotation.Flatten = true; + } + if (annotation.Author == "Text") + { + // Add the annotation to the removeItems list + removeItems.Add(annotation); + // Create a new redaction with the annotation bounds and color + PdfRedaction redaction = new PdfRedaction(annotation.Bounds); + //Set the font family and font size + PdfStandardFont font = new PdfStandardFont(PdfFontFamily.Courier, 8); + //Create the appearance like repeated text in the redaction area + CreateRedactionAppearance(redaction.Appearance.Graphics, PdfTextAlignment.Left, true, new SizeF(annotation.Bounds.Width, annotation.Bounds.Height), RedactionText, font, PdfBrushes.Red); + // Add the redaction to the page + loadedPage.AddRedaction(redaction); + annotation.Flatten = true; + } + //Apply the pattern for the Redaction + if (annotation.Author == "Pattern") + { + // Add the annotation to the removeItems list + removeItems.Add(annotation); + // Create a new redaction with the annotation bounds and color + PdfRedaction redaction = new PdfRedaction(annotation.Bounds); + Syncfusion.Drawing.RectangleF rect = new Syncfusion.Drawing.RectangleF(0, 0, 8, 8); + PdfTilingBrush tillingBrush = new PdfTilingBrush(rect); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Gray, new Syncfusion.Drawing.RectangleF(0, 0, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.White, new Syncfusion.Drawing.RectangleF(2, 0, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(4, 0, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.DarkGray, new Syncfusion.Drawing.RectangleF(6, 0, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.White, new Syncfusion.Drawing.RectangleF(0, 2, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(2, 2, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Black, new Syncfusion.Drawing.RectangleF(4, 2, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(6, 2, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(0, 4, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.DarkGray, new Syncfusion.Drawing.RectangleF(2, 4, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(4, 4, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.White, new Syncfusion.Drawing.RectangleF(6, 4, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Black, new Syncfusion.Drawing.RectangleF(0, 6, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(2, 6, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Black, new Syncfusion.Drawing.RectangleF(4, 6, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.DarkGray, new Syncfusion.Drawing.RectangleF(6, 6, 2, 2)); + rect = new Syncfusion.Drawing.RectangleF(0, 0, 16, 14); + PdfTilingBrush tillingBrushNew = new PdfTilingBrush(rect); + tillingBrushNew.Graphics.DrawRectangle(tillingBrush, rect); + //Set the pattern for the redaction area + redaction.Appearance.Graphics.DrawRectangle(tillingBrushNew, new Syncfusion.Drawing.RectangleF(0, 0, annotation.Bounds.Width, annotation.Bounds.Height)); + // Add the redaction to the page + loadedPage.AddRedaction(redaction); + annotation.Flatten = true; + } + } + else if (annotation is PdfLoadedRubberStampAnnotation) + { + if (annotation.Author == "Image") + { + Stream[] images = PdfLoadedRubberStampAnnotationExtension.GetImages(annotation as PdfLoadedRubberStampAnnotation); + // Create a new redaction with the annotation bounds and color + PdfRedaction redaction = new PdfRedaction(annotation.Bounds); + images[0].Position = 0; + PdfImage image = new PdfBitmap(images[0]); + //Apply the image to redaction area + redaction.Appearance.Graphics.DrawImage(image, new Syncfusion.Drawing.RectangleF(0, 0, annotation.Bounds.Width, annotation.Bounds.Height)); + // Add the redaction to the page + loadedPage.AddRedaction(redaction); + annotation.Flatten = true; + } + } + } + foreach (PdfLoadedAnnotation annotation1 in removeItems) + { + loadedPage.Annotations.Remove(annotation1); + } + } + loadedDocument.Redact(); + MemoryStream stream = new MemoryStream(); + loadedDocument.Save(stream); + stream.Position = 0; + loadedDocument.Close(true); + byteArray = stream.ToArray(); + finalbase64 = "data:application/pdf;base64," + Convert.ToBase64String(byteArray); + stream.Dispose(); + return Content(finalbase64); + } + } + + return Content("data:application/pdf;base64," + ""); + } + + //The Method used for apply the text in the full area of redaction rectangle + private static void CreateRedactionAppearance(PdfGraphics graphics, PdfTextAlignment alignment, bool repeat, SizeF size, string overlayText, PdfFont font, PdfBrush textcolor) + { + float col = 0, row; + if (font == null) font = new PdfStandardFont(PdfFontFamily.Helvetica, 10); + int textAlignment = Convert.ToInt32(alignment); + float y = 0, x = 0, diff = 0; + Syncfusion.Drawing.RectangleF rect; + Syncfusion.Drawing.SizeF textsize = font.MeasureString(overlayText); + + if (repeat) + { + col = size.Width / textsize.Width; + row = (float)Math.Floor(size.Height / font.Size); + diff = Math.Abs(size.Width - (float)(Math.Floor(col) * textsize.Width)); + if (textAlignment == 1) + x = diff / 2; + if (textAlignment == 2) + x = diff; + for (int i = 1; i < col; i++) + { + for (int j = 0; j < row; j++) + { + rect = new Syncfusion.Drawing.RectangleF(x, y, 0, 0); + graphics.DrawString(overlayText, font, textcolor, rect); + y = y + font.Size; + } + x = x + textsize.Width; + y = 0; + } + } + else + { + diff = Math.Abs(size.Width - textsize.Width); + if (textAlignment == 1) + { + x = diff / 2; + } + if (textAlignment == 2) + { + x = diff; + } + rect = new Syncfusion.Drawing.RectangleF(x, 0, 0, 0); + graphics.DrawString(overlayText, font, textcolor, rect); + } + } + + [HttpPost("LoadFile")] + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] + [Route("[controller]/LoadFile")] + public IActionResult LoadFile([FromBody] Dictionary jsonObject) + { + if (jsonObject.ContainsKey("data")) + { + + string base64 = jsonObject["data"]; + //string fileName = args.FileData[0].Name; + string type = jsonObject["type"]; + string data = base64.Split(',')[1]; + byte[] bytes = Convert.FromBase64String(data); + var outputStream = new MemoryStream(); + Syncfusion.Pdf.PdfDocument pdfDocument = new Syncfusion.Pdf.PdfDocument(); + using (Stream stream = new MemoryStream(bytes)) + { + switch (type) + { + case "docx": + case "dot": + case "doc": + case "dotx": + case "docm": + case "dotm": + case "rtf": + Syncfusion.DocIO.DLS.WordDocument doc = new Syncfusion.DocIO.DLS.WordDocument(stream, GetWFormatType(type)); + //Initialization of DocIORenderer for Word to PDF conversion + DocIORenderer render = new DocIORenderer(); + //Converts Word document into PDF document + pdfDocument = render.ConvertToPDF(doc); + doc.Close(); + break; + case "pptx": + case "pptm": + case "potx": + case "potm": + //Loads or open an PowerPoint Presentation + IPresentation pptxDoc = Presentation.Open(stream); + pdfDocument = PresentationToPdfConverter.Convert(pptxDoc); + pptxDoc.Close(); + break; + case "xlsx": + case "xls": + ExcelEngine excelEngine = new ExcelEngine(); + //Loads or open an existing workbook through Open method of IWorkbooks + IWorkbook workbook = excelEngine.Excel.Workbooks.Open(stream); + //Initialize XlsIO renderer. + XlsIORenderer renderer = new XlsIORenderer(); + //Convert Excel document into PDF document + pdfDocument = renderer.ConvertToPDF(workbook); + workbook.Close(); + break; + case "jpeg": + case "jpg": + case "png": + case "bmp": + //Add a page to the document + PdfPage page = pdfDocument.Pages.Add(); + //Create PDF graphics for the page + PdfGraphics graphics = page.Graphics; + PdfBitmap image = new PdfBitmap(stream); + //Draw the image + graphics.DrawImage(image, 0, 0); + break; + case "pdf": + string pdfBase64String = Convert.ToBase64String(bytes); + return Content("data:application/pdf;base64," + pdfBase64String); + break; + } + + } + pdfDocument.Save(outputStream); + outputStream.Position = 0; + byte[] byteArray = outputStream.ToArray(); + pdfDocument.Close(); + outputStream.Close(); + + string base64String = Convert.ToBase64String(byteArray); + return Content("data:application/pdf;base64," + base64String); + + + } + return Content("data:application/pdf;base64," + ""); + } + public static WFormatType GetWFormatType(string format) + { + if (string.IsNullOrEmpty(format)) + throw new NotSupportedException("This is not a valid Word documnet."); + switch (format.ToLower()) + { + case "dotx": + return WFormatType.Dotx; + case "docx": + return WFormatType.Docx; + case "docm": + return WFormatType.Docm; + case "dotm": + return WFormatType.Dotm; + case "dot": + return WFormatType.Dot; + case "doc": + return WFormatType.Doc; + case "rtf": + return WFormatType.Rtf; + default: + throw new NotSupportedException("This is not a valid Word documnet."); + } + } + //Gets the path of the PDF document private string GetDocumentPath(string document) { diff --git a/ASP.NET Core/PdfViewerWebService_8.0/PdfViewerWebService_8.0/PdfViewerWebService_8.0.csproj b/ASP.NET Core/PdfViewerWebService_8.0/PdfViewerWebService_8.0/PdfViewerWebService_8.0.csproj index 9d9a180..d08bc4b 100644 --- a/ASP.NET Core/PdfViewerWebService_8.0/PdfViewerWebService_8.0/PdfViewerWebService_8.0.csproj +++ b/ASP.NET Core/PdfViewerWebService_8.0/PdfViewerWebService_8.0/PdfViewerWebService_8.0.csproj @@ -9,10 +9,24 @@ - - - + + + + + + + + + + + + + + + + + diff --git a/ASP.NET MVC/MvcWebService_4.8/MvcWebService.csproj b/ASP.NET MVC/MvcWebService_4.8/MvcWebService.csproj index 44ea306..5246411 100644 --- a/ASP.NET MVC/MvcWebService_4.8/MvcWebService.csproj +++ b/ASP.NET MVC/MvcWebService_4.8/MvcWebService.csproj @@ -54,20 +54,50 @@ packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll True - - packages\Syncfusion.EJ2.PdfViewer.AspNet.Mvc5.27.1.52\lib\net45\Syncfusion.Compression.Base.dll + + packages\Syncfusion.OfficeChartToImageConverter.AspNet.Mvc5.100.2.1521\lib\net462\Syncfusion.Compression.Base.dll - - packages\Syncfusion.EJ2.PdfViewer.AspNet.Mvc5.27.1.52\lib\net45\Syncfusion.EJ2.PdfViewer.dll + + packages\Syncfusion.DocToPdfConverter.AspNet.Mvc5.100.2.1567\lib\net462\Syncfusion.DocIO.Base.dll - - packages\Syncfusion.EJ2.PdfViewer.AspNet.Mvc5.27.1.52\lib\net45\Syncfusion.Licensing.dll + + packages\Syncfusion.DocToPdfConverter.AspNet.Mvc5.100.2.1567\lib\net462\Syncfusion.DocToPDFConverter.Base.dll - - packages\Syncfusion.EJ2.PdfViewer.AspNet.Mvc5.27.1.52\lib\net45\Syncfusion.Pdf.Base.dll + + packages\Syncfusion.EJ2.MVC5.28.2.6\lib\net462\Syncfusion.EJ2.dll - - packages\Syncfusion.EJ2.PdfViewer.AspNet.Mvc5.27.1.52\lib\net45\Syncfusion.PdfToImageConverter.Base.dll + + packages\Syncfusion.EJ2.PdfViewer.AspNet.Mvc5.28.2.6\lib\net462\Syncfusion.EJ2.PdfViewer.dll + + + packages\Syncfusion.ExcelToPdfConverter.AspNet.Mvc5.100.2.1555\lib\net462\Syncfusion.ExcelToPDFConverter.Base.dll + + + packages\Syncfusion.Licensing.28.2.6\lib\net462\Syncfusion.Licensing.dll + + + packages\Syncfusion.OfficeChart.Base.100.2.1552\lib\net462\Syncfusion.OfficeChart.Base.dll + + + packages\Syncfusion.OfficeChartToImageConverter.AspNet.Mvc5.100.2.1521\lib\net462\Syncfusion.OfficeChartToImageConverter.WPF.dll + + + packages\Syncfusion.PresentationToPdfConverter.AspNet.Mvc5.100.2.1519\lib\net462\Syncfusion.Pdf.Base.dll + + + packages\Syncfusion.PdfToImageConverter.Base.100.2.544\lib\net462\Syncfusion.PdfToImageConverter.Base.dll + + + packages\Syncfusion.PresentationToPdfConverter.AspNet.Mvc5.100.2.1519\lib\net462\Syncfusion.Presentation.Base.dll + + + packages\Syncfusion.PresentationToPdfConverter.AspNet.Mvc5.100.2.1519\lib\net462\Syncfusion.PresentationToPdfConverter.Base.dll + + + packages\Syncfusion.OfficeChartToImageConverter.AspNet.Mvc5.100.2.1521\lib\net462\Syncfusion.SfChart.Wpf.dll + + + packages\Syncfusion.XlsIO.AspNet.Mvc5.100.2.1785\lib\net462\Syncfusion.XlsIO.Base.dll @@ -144,8 +174,69 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -154,7 +245,19 @@ + + + + + + + + + + + + 10.0 diff --git a/ASP.NET MVC/MvcWebService_4.8/Web.config b/ASP.NET MVC/MvcWebService_4.8/Web.config index 54e4e04..9914a19 100644 --- a/ASP.NET MVC/MvcWebService_4.8/Web.config +++ b/ASP.NET MVC/MvcWebService_4.8/Web.config @@ -34,7 +34,7 @@ - + @@ -48,6 +48,14 @@ + + + + + + + + diff --git a/ASP.NET MVC/MvcWebService_4.8/packages.config b/ASP.NET MVC/MvcWebService_4.8/packages.config index 0267bcf..eebbca0 100644 --- a/ASP.NET MVC/MvcWebService_4.8/packages.config +++ b/ASP.NET MVC/MvcWebService_4.8/packages.config @@ -16,6 +16,21 @@ - + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ASP.NET MVC/MvcWebService_4.8/webapi/PdfViewerController.cs b/ASP.NET MVC/MvcWebService_4.8/webapi/PdfViewerController.cs index e98b8dd..414b8be 100644 --- a/ASP.NET MVC/MvcWebService_4.8/webapi/PdfViewerController.cs +++ b/ASP.NET MVC/MvcWebService_4.8/webapi/PdfViewerController.cs @@ -6,8 +6,28 @@ using System.Linq; using System.Net; using System.Net.Http; +using System.Security.Cryptography.X509Certificates; using System.Web; +using System.Web.Caching; using System.Web.Http; +using Syncfusion.Pdf.Parsing; +using System.Security.Cryptography.X509Certificates; +using Syncfusion.Pdf.Security; +using Syncfusion.Pdf; +using Syncfusion.ExcelToPdfConverter; +using Syncfusion.Pdf.Graphics; +using Syncfusion.Presentation; +using Syncfusion.PresentationToPdfConverter; +using Syncfusion.XlsIO; +using Syncfusion.DocIO; +using Syncfusion.DocIO.DLS; +using Syncfusion.OfficeChart; +using Syncfusion.OfficeChartToImageConverter; +using WFormatType = Syncfusion.DocIO.FormatType; +using Syncfusion.DocToPDFConverter; +using Syncfusion.Pdf.Interactive; +using Syncfusion.Pdf.Redaction; +using System.Drawing; namespace MVCwebservice.webapi { @@ -223,6 +243,274 @@ public object ImportFormFields(Dictionary jsonObject) return (JsonConvert.SerializeObject(jsonResult)); } + [System.Web.Mvc.HttpPost] + public object LoadFile(Dictionary jsonObject) + { + if (jsonObject.ContainsKey("data")) + { + string base64 = jsonObject["data"]; + //string fileName = args.FileData[0].Name; + string type = jsonObject["type"]; + string data = base64.Split(',')[1]; + byte[] bytes = Convert.FromBase64String(data); + var outputStream = new MemoryStream(); + Syncfusion.Pdf.PdfDocument pdfDocument = new Syncfusion.Pdf.PdfDocument(); + using (Stream stream = new MemoryStream(bytes)) + { + switch (type) + { + case "docx": + case "dot": + case "doc": + case "dotx": + case "docm": + case "dotm": + case "rtf": + Syncfusion.DocIO.DLS.WordDocument doc = new Syncfusion.DocIO.DLS.WordDocument(stream, GetWFormatType(type)); + //Initialization of DocIORenderer for Word to PDF conversion + DocToPDFConverter converter = new DocToPDFConverter(); + //Converts Word document into PDF document + pdfDocument = converter.ConvertToPDF(doc); + doc.Close(); + break; + case "pptx": + case "pptm": + case "potx": + case "potm": + //Loads or open an PowerPoint Presentation + IPresentation pptxDoc = Presentation.Open(stream); + pdfDocument = PresentationToPdfConverter.Convert(pptxDoc); + pptxDoc.Close(); + break; + case "xlsx": + case "xls": + ExcelEngine excelEngine = new ExcelEngine(); + //Loads or open an existing workbook through Open method of IWorkbooks + IWorkbook workbook = excelEngine.Excel.Workbooks.Open(stream); + //Initialize XlsIO renderer. + ExcelToPdfConverter Excelconverter = new ExcelToPdfConverter(workbook); + //Convert Excel document into PDF document + pdfDocument = Excelconverter.Convert(); + workbook.Close(); + break; + case "jpeg": + case "jpg": + case "png": + case "bmp": + //Add a page to the document + PdfPage page = pdfDocument.Pages.Add(); + //Create PDF graphics for the page + PdfGraphics graphics = page.Graphics; + PdfBitmap image = new PdfBitmap(stream); + //Draw the image + graphics.DrawImage(image, 0, 0); + break; + case "pdf": + string pdfBase64String = Convert.ToBase64String(bytes); + return (GetPlainText("data:application/pdf;base64," + pdfBase64String)); + } + } + pdfDocument.Save(outputStream); + outputStream.Position = 0; + byte[] byteArray = outputStream.ToArray(); + pdfDocument.Close(); + outputStream.Close(); + string base64String = Convert.ToBase64String(byteArray); + return (GetPlainText("data:application/pdf;base64," + base64String)); + } + return (GetPlainText("data:application/pdf;base64," + "")); + } + + public static WFormatType GetWFormatType(string format) + { + if (string.IsNullOrEmpty(format)) + throw new NotSupportedException("This is not a valid Word documnet."); + switch (format.ToLower()) + { + case "dotx": + return WFormatType.Dotx; + case "docx": + return WFormatType.Docx; + case "docm": + return WFormatType.Docm; + case "dotm": + return WFormatType.Dotm; + case "dot": + return WFormatType.Dot; + case "doc": + return WFormatType.Doc; + case "rtf": + return WFormatType.Rtf; + default: + throw new NotSupportedException("This is not a valid Word documnet."); + } + } + [System.Web.Mvc.HttpPost] + public object Redaction(Dictionary jsonObject) + { + string RedactionText = "Redacted"; + var finalbase64 = string.Empty; + if (jsonObject != null && jsonObject.ContainsKey("base64String")) + { + string base64 = jsonObject["base64String"]; + string base64String = base64.Split(new string[] { "data:application/pdf;base64," }, StringSplitOptions.None)[1]; + if (base64String != null || base64String != string.Empty) + { + byte[] byteArray = Convert.FromBase64String(base64String); + Console.WriteLine("redaction"); + PdfLoadedDocument loadedDocument = new PdfLoadedDocument(byteArray); + foreach (PdfLoadedPage loadedPage in loadedDocument.Pages) + { + List removeItems = new List(); + foreach (PdfLoadedAnnotation annotation in loadedPage.Annotations) + { + if (annotation is PdfLoadedRectangleAnnotation) + { + if (annotation.Author == "Redaction") + { + // Add the annotation to the removeItems list + removeItems.Add(annotation); + // Create a new redaction with the annotation bounds and color + PdfRedaction redaction = new PdfRedaction(annotation.Bounds, annotation.Color); + // Add the redaction to the page + loadedPage.AddRedaction(redaction); + annotation.Flatten = true; + } + if (annotation.Author == "Text") + { + // Add the annotation to the removeItems list + removeItems.Add(annotation); + // Create a new redaction with the annotation bounds and color + PdfRedaction redaction = new PdfRedaction(annotation.Bounds); + //Set the font family and font size + PdfStandardFont font = new PdfStandardFont(PdfFontFamily.Courier, 8); + //Create the appearance like repeated text in the redaction area + CreateRedactionAppearance(redaction.Appearance.Graphics, PdfTextAlignment.Left, true, new SizeF(annotation.Bounds.Width, annotation.Bounds.Height), RedactionText, font, PdfBrushes.Red); + // Add the redaction to the page + loadedPage.AddRedaction(redaction); + annotation.Flatten = true; + } + //Apply the pattern for the Redaction + if (annotation.Author == "Pattern") + { + // Add the annotation to the removeItems list + removeItems.Add(annotation); + // Create a new redaction with the annotation bounds and color + PdfRedaction redaction = new PdfRedaction(annotation.Bounds); + RectangleF rect = new RectangleF(0, 0, 8, 8); + PdfTilingBrush tillingBrush = new PdfTilingBrush(rect); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Gray, new RectangleF(0, 0, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.White, new RectangleF(2, 0, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new RectangleF(4, 0, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.DarkGray, new RectangleF(6, 0, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.White, new RectangleF(0, 2, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new RectangleF(2, 2, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Black, new RectangleF(4, 2, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new RectangleF(6, 2, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new RectangleF(0, 4, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.DarkGray, new RectangleF(2, 4, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new RectangleF(4, 4, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.White, new RectangleF(6, 4, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Black, new RectangleF(0, 6, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new RectangleF(2, 6, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Black, new RectangleF(4, 6, 2, 2)); + tillingBrush.Graphics.DrawRectangle(PdfBrushes.DarkGray, new RectangleF(6, 6, 2, 2)); + rect = new RectangleF(0, 0, 16, 14); + PdfTilingBrush tillingBrushNew = new PdfTilingBrush(rect); + tillingBrushNew.Graphics.DrawRectangle(tillingBrush, rect); + //Set the pattern for the redaction area + redaction.Appearance.Graphics.DrawRectangle(tillingBrushNew, new RectangleF(0, 0, annotation.Bounds.Width, annotation.Bounds.Height)); + // Add the redaction to the page + loadedPage.AddRedaction(redaction); + annotation.Flatten = true; + } + } + else if (annotation is PdfLoadedRubberStampAnnotation) + { + if (annotation.Author == "Image") + { + //Get the existing rubber stamp annotation. + PdfLoadedRubberStampAnnotation rubberStampAnnotation = annotation as PdfLoadedRubberStampAnnotation; + //Get the custom images used for the rubber stamp annotation. + Image[] images = rubberStampAnnotation.GetImages(); + + PdfRedaction redaction = new PdfRedaction(annotation.Bounds); + //images[0].Position = 0; + PdfImage image = new PdfBitmap(images[0]); + //Apply the image to redaction area + redaction.Appearance.Graphics.DrawImage(image, new RectangleF(0, 0, annotation.Bounds.Width, annotation.Bounds.Height)); + // Add the redaction to the page + loadedPage.AddRedaction(redaction); + annotation.Flatten = true; + } + } + } + foreach (PdfLoadedAnnotation annotation1 in removeItems) + { + loadedPage.Annotations.Remove(annotation1); + } + } + loadedDocument.Redact(); + MemoryStream stream = new MemoryStream(); + loadedDocument.Save(stream); + stream.Position = 0; + loadedDocument.Close(true); + byteArray = stream.ToArray(); + finalbase64 = "data:application/pdf;base64," + Convert.ToBase64String(byteArray); + stream.Dispose(); + return GetPlainText(finalbase64); + } + } + + return GetPlainText("data:application/pdf;base64," + ""); + } + + //The Method used for apply the text in the full area of redaction rectangle + private static void CreateRedactionAppearance(PdfGraphics graphics, PdfTextAlignment alignment, bool repeat, SizeF size, string overlayText, PdfFont font, PdfBrush textcolor) + { + float col = 0, row; + if (font == null) font = new PdfStandardFont(PdfFontFamily.Helvetica, 10); + int textAlignment = Convert.ToInt32(alignment); + float y = 0, x = 0, diff = 0; + RectangleF rect; + SizeF textsize = font.MeasureString(overlayText); + if (repeat) + { + col = size.Width / textsize.Width; + row = (float)Math.Floor(size.Height / font.Size); + diff = Math.Abs(size.Width - (float)(Math.Floor(col) * textsize.Width)); + if (textAlignment == 1) + x = diff / 2; + if (textAlignment == 2) + x = diff; + for (int i = 1; i < col; i++) + { + for (int j = 0; j < row; j++) + { + rect = new RectangleF(x, y, 0, 0); + graphics.DrawString(overlayText, font, textcolor, rect); + y = y + font.Size; + } + x = x + textsize.Width; + y = 0; + } + } + else + { + diff = Math.Abs(size.Width - textsize.Width); + if (textAlignment == 1) + { + x = diff / 2; + } + if (textAlignment == 2) + { + x = diff; + } + rect = new RectangleF(x, 0, 0, 0); + graphics.DrawString(overlayText, font, textcolor, rect); + } + } + private HttpResponseMessage GetPlainText(string pageImage) { var responseText = new HttpResponseMessage(HttpStatusCode.OK);