Skip to content

Commit 244ba8d

Browse files
authored
Merge pull request #20 from SyncfusionExamples/EJ2-923321
923321: Added Redaction and LoadFile action
2 parents 0d04403 + 27e17b0 commit 244ba8d

File tree

14 files changed

+1447
-22
lines changed

14 files changed

+1447
-22
lines changed

ASP.NET Core/PdfViewerWebService_5.0/Controllers/PdfViewerController.cs

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
using System.Collections.Generic;
88
using System.IO;
99
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;
1017

1118
namespace PdfViewerService2.Controllers
1219
{
@@ -318,6 +325,172 @@ public IActionResult ImportFormFields([FromBody] Dictionary<string, string> json
318325
return Content(JsonConvert.SerializeObject(pageImage));
319326
}
320327

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+
321494
//Gets the path of the PDF document
322495
private string GetDocumentPath(string document)
323496
{

ASP.NET Core/PdfViewerWebService_5.0/PdfViewerWebService_5.0.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88

99
<ItemGroup>
1010
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.0" />
11-
<PackageReference Include="Syncfusion.EJ2.PdfViewer.AspNet.Core" Version="*" />
11+
<PackageReference Include="Syncfusion.Compression.Net.Core" Version="*" />
12+
<PackageReference Include="Syncfusion.EJ2.AspNet.Core" Version="*" />
13+
<PackageReference Include="Syncfusion.EJ2.PdfViewer.AspNet.Core" Version="*" />
14+
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="*" />
15+
<PackageReference Include="Syncfusion.Pdf.Imaging.Net.Core" Version="*" />
1216
</ItemGroup>
1317

1418
</Project>

ASP.NET Core/PdfViewerWebService_6.0 - Minimal API/PdfViewerWebService_6.0.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.0" />
12-
<PackageReference Include="Syncfusion.EJ2.PdfViewer.AspNet.Core" Version="*" />
12+
<PackageReference Include="Syncfusion.Compression.Net.Core" Version="*" />
13+
<PackageReference Include="Syncfusion.EJ2.AspNet.Core" Version="*" />
14+
<PackageReference Include="Syncfusion.EJ2.PdfViewer.AspNet.Core" Version="*" />
15+
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="*" />
16+
<PackageReference Include="Syncfusion.Pdf.Imaging.Net.Core" Version="*" />
1317
</ItemGroup>
1418

1519
</Project>

0 commit comments

Comments
 (0)