|
7 | 7 | using System.Collections.Generic; |
8 | 8 | using System.IO; |
9 | 9 | using System.Net; |
| 10 | +using System.Drawing; |
| 11 | +using Syncfusion.Pdf.Parsing; |
| 12 | +using Syncfusion.Pdf; |
| 13 | +using Syncfusion.Pdf.Graphics; |
| 14 | +using Syncfusion.Pdf.Interactive; |
| 15 | +using Syncfusion.Pdf.Redaction; |
| 16 | +using System.Linq; |
10 | 17 |
|
11 | 18 | namespace PdfViewerService2.Controllers |
12 | 19 | { |
@@ -318,6 +325,172 @@ public IActionResult ImportFormFields([FromBody] Dictionary<string, string> json |
318 | 325 | return Content(JsonConvert.SerializeObject(pageImage)); |
319 | 326 | } |
320 | 327 |
|
| 328 | + [HttpPost("Redaction")] |
| 329 | + [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] |
| 330 | + [Route("[controller]/Redaction")] |
| 331 | + public IActionResult Redaction([FromBody] Dictionary<string, string> jsonObject) |
| 332 | + { |
| 333 | + string RedactionText = "Redacted"; |
| 334 | + var finalbase64 = string.Empty; |
| 335 | + if (jsonObject != null && jsonObject.ContainsKey("base64String")) |
| 336 | + { |
| 337 | + string base64 = jsonObject["base64String"]; |
| 338 | + string base64String = base64.Split(new string[] { "data:application/pdf;base64," }, StringSplitOptions.None)[1]; |
| 339 | + if (base64String != null || base64String != string.Empty) |
| 340 | + { |
| 341 | + byte[] byteArray = Convert.FromBase64String(base64String); |
| 342 | + Console.WriteLine("redaction"); |
| 343 | + PdfLoadedDocument loadedDocument = new PdfLoadedDocument(byteArray); |
| 344 | + foreach (PdfLoadedPage loadedPage in loadedDocument.Pages) |
| 345 | + { |
| 346 | + List<PdfLoadedAnnotation> removeItems = new List<PdfLoadedAnnotation>(); |
| 347 | + foreach (PdfLoadedAnnotation annotation in loadedPage.Annotations) |
| 348 | + { |
| 349 | + if (annotation is PdfLoadedRectangleAnnotation) |
| 350 | + { |
| 351 | + if (annotation.Author == "Redaction") |
| 352 | + { |
| 353 | + // Add the annotation to the removeItems list |
| 354 | + removeItems.Add(annotation); |
| 355 | + // Create a new redaction with the annotation bounds and color |
| 356 | + PdfRedaction redaction = new PdfRedaction(annotation.Bounds, annotation.Color); |
| 357 | + // Add the redaction to the page |
| 358 | + loadedPage.AddRedaction(redaction); |
| 359 | + annotation.Flatten = true; |
| 360 | + } |
| 361 | + if (annotation.Author == "Text") |
| 362 | + { |
| 363 | + // Add the annotation to the removeItems list |
| 364 | + removeItems.Add(annotation); |
| 365 | + // Create a new redaction with the annotation bounds and color |
| 366 | + PdfRedaction redaction = new PdfRedaction(annotation.Bounds); |
| 367 | + //Set the font family and font size |
| 368 | + PdfStandardFont font = new PdfStandardFont(PdfFontFamily.Courier, 8); |
| 369 | + //Create the appearance like repeated text in the redaction area |
| 370 | + CreateRedactionAppearance(redaction.Appearance.Graphics, PdfTextAlignment.Left, true, new SizeF(annotation.Bounds.Width, annotation.Bounds.Height), RedactionText, font, PdfBrushes.Red); |
| 371 | + // Add the redaction to the page |
| 372 | + loadedPage.AddRedaction(redaction); |
| 373 | + annotation.Flatten = true; |
| 374 | + } |
| 375 | + //Apply the pattern for the Redaction |
| 376 | + if (annotation.Author == "Pattern") |
| 377 | + { |
| 378 | + // Add the annotation to the removeItems list |
| 379 | + removeItems.Add(annotation); |
| 380 | + // Create a new redaction with the annotation bounds and color |
| 381 | + PdfRedaction redaction = new PdfRedaction(annotation.Bounds); |
| 382 | + Syncfusion.Drawing.RectangleF rect = new Syncfusion.Drawing.RectangleF(0, 0, 8, 8); |
| 383 | + PdfTilingBrush tillingBrush = new PdfTilingBrush(rect); |
| 384 | + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Gray, new Syncfusion.Drawing.RectangleF(0, 0, 2, 2)); |
| 385 | + tillingBrush.Graphics.DrawRectangle(PdfBrushes.White, new Syncfusion.Drawing.RectangleF(2, 0, 2, 2)); |
| 386 | + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(4, 0, 2, 2)); |
| 387 | + tillingBrush.Graphics.DrawRectangle(PdfBrushes.DarkGray, new Syncfusion.Drawing.RectangleF(6, 0, 2, 2)); |
| 388 | + tillingBrush.Graphics.DrawRectangle(PdfBrushes.White, new Syncfusion.Drawing.RectangleF(0, 2, 2, 2)); |
| 389 | + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(2, 2, 2, 2)); |
| 390 | + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Black, new Syncfusion.Drawing.RectangleF(4, 2, 2, 2)); |
| 391 | + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(6, 2, 2, 2)); |
| 392 | + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(0, 4, 2, 2)); |
| 393 | + tillingBrush.Graphics.DrawRectangle(PdfBrushes.DarkGray, new Syncfusion.Drawing.RectangleF(2, 4, 2, 2)); |
| 394 | + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(4, 4, 2, 2)); |
| 395 | + tillingBrush.Graphics.DrawRectangle(PdfBrushes.White, new Syncfusion.Drawing.RectangleF(6, 4, 2, 2)); |
| 396 | + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Black, new Syncfusion.Drawing.RectangleF(0, 6, 2, 2)); |
| 397 | + tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(2, 6, 2, 2)); |
| 398 | + tillingBrush.Graphics.DrawRectangle(PdfBrushes.Black, new Syncfusion.Drawing.RectangleF(4, 6, 2, 2)); |
| 399 | + tillingBrush.Graphics.DrawRectangle(PdfBrushes.DarkGray, new Syncfusion.Drawing.RectangleF(6, 6, 2, 2)); |
| 400 | + rect = new Syncfusion.Drawing.RectangleF(0, 0, 16, 14); |
| 401 | + PdfTilingBrush tillingBrushNew = new PdfTilingBrush(rect); |
| 402 | + tillingBrushNew.Graphics.DrawRectangle(tillingBrush, rect); |
| 403 | + //Set the pattern for the redaction area |
| 404 | + redaction.Appearance.Graphics.DrawRectangle(tillingBrushNew, new Syncfusion.Drawing.RectangleF(0, 0, annotation.Bounds.Width, annotation.Bounds.Height)); |
| 405 | + // Add the redaction to the page |
| 406 | + loadedPage.AddRedaction(redaction); |
| 407 | + annotation.Flatten = true; |
| 408 | + } |
| 409 | + } |
| 410 | + else if (annotation is PdfLoadedRubberStampAnnotation) |
| 411 | + { |
| 412 | + if (annotation.Author == "Image") |
| 413 | + { |
| 414 | + Stream[] images = PdfLoadedRubberStampAnnotationExtension.GetImages(annotation as PdfLoadedRubberStampAnnotation); |
| 415 | + // Create a new redaction with the annotation bounds and color |
| 416 | + PdfRedaction redaction = new PdfRedaction(annotation.Bounds); |
| 417 | + images[0].Position = 0; |
| 418 | + PdfImage image = new PdfBitmap(images[0]); |
| 419 | + //Apply the image to redaction area |
| 420 | + redaction.Appearance.Graphics.DrawImage(image, new Syncfusion.Drawing.RectangleF(0, 0, annotation.Bounds.Width, annotation.Bounds.Height)); |
| 421 | + // Add the redaction to the page |
| 422 | + loadedPage.AddRedaction(redaction); |
| 423 | + annotation.Flatten = true; |
| 424 | + } |
| 425 | + } |
| 426 | + } |
| 427 | + foreach (PdfLoadedAnnotation annotation1 in removeItems) |
| 428 | + { |
| 429 | + loadedPage.Annotations.Remove(annotation1); |
| 430 | + } |
| 431 | + } |
| 432 | + loadedDocument.Redact(); |
| 433 | + MemoryStream stream = new MemoryStream(); |
| 434 | + loadedDocument.Save(stream); |
| 435 | + stream.Position = 0; |
| 436 | + loadedDocument.Close(true); |
| 437 | + byteArray = stream.ToArray(); |
| 438 | + finalbase64 = "data:application/pdf;base64," + Convert.ToBase64String(byteArray); |
| 439 | + stream.Dispose(); |
| 440 | + return Content(finalbase64); |
| 441 | + } |
| 442 | + } |
| 443 | + |
| 444 | + return Content("data:application/pdf;base64," + ""); |
| 445 | + } |
| 446 | + |
| 447 | + //The Method used for apply the text in the full area of redaction rectangle |
| 448 | + private static void CreateRedactionAppearance(PdfGraphics graphics, PdfTextAlignment alignment, bool repeat, SizeF size, string overlayText, PdfFont font, PdfBrush textcolor) |
| 449 | + { |
| 450 | + float col = 0, row; |
| 451 | + if (font == null) font = new PdfStandardFont(PdfFontFamily.Helvetica, 10); |
| 452 | + int textAlignment = Convert.ToInt32(alignment); |
| 453 | + float y = 0, x = 0, diff = 0; |
| 454 | + Syncfusion.Drawing.RectangleF rect; |
| 455 | + Syncfusion.Drawing.SizeF textsize = font.MeasureString(overlayText); |
| 456 | + |
| 457 | + if (repeat) |
| 458 | + { |
| 459 | + col = size.Width / textsize.Width; |
| 460 | + row = (float)Math.Floor(size.Height / font.Size); |
| 461 | + diff = Math.Abs(size.Width - (float)(Math.Floor(col) * textsize.Width)); |
| 462 | + if (textAlignment == 1) |
| 463 | + x = diff / 2; |
| 464 | + if (textAlignment == 2) |
| 465 | + x = diff; |
| 466 | + for (int i = 1; i < col; i++) |
| 467 | + { |
| 468 | + for (int j = 0; j < row; j++) |
| 469 | + { |
| 470 | + rect = new Syncfusion.Drawing.RectangleF(x, y, 0, 0); |
| 471 | + graphics.DrawString(overlayText, font, textcolor, rect); |
| 472 | + y = y + font.Size; |
| 473 | + } |
| 474 | + x = x + textsize.Width; |
| 475 | + y = 0; |
| 476 | + } |
| 477 | + } |
| 478 | + else |
| 479 | + { |
| 480 | + diff = Math.Abs(size.Width - textsize.Width); |
| 481 | + if (textAlignment == 1) |
| 482 | + { |
| 483 | + x = diff / 2; |
| 484 | + } |
| 485 | + if (textAlignment == 2) |
| 486 | + { |
| 487 | + x = diff; |
| 488 | + } |
| 489 | + rect = new Syncfusion.Drawing.RectangleF(x, 0, 0, 0); |
| 490 | + graphics.DrawString(overlayText, font, textcolor, rect); |
| 491 | + } |
| 492 | + } |
| 493 | + |
321 | 494 | //Gets the path of the PDF document |
322 | 495 | private string GetDocumentPath(string document) |
323 | 496 | { |
|
0 commit comments