Skip to content

Commit a12380d

Browse files
committed
923321: Added Redaction action
1 parent 9072cd7 commit a12380d

File tree

2 files changed

+332
-0
lines changed

2 files changed

+332
-0
lines changed

ASP.NET Core/PdfViewerWebService_8.0 _redis_config/PdfViewerWebService_8.0/PdfViewerController.cs

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,172 @@ public IActionResult Download([FromBody] Dictionary<string, object> args)
215215
return Content(documentBase);
216216
}
217217

218+
[HttpPost]
219+
[EnableCors("AllowAllOrigins")]
220+
[Route("Redaction")]
221+
public IActionResult Redaction([FromBody] Dictionary<string, string> jsonObject)
222+
{
223+
string RedactionText = "Redacted";
224+
var finalbase64 = string.Empty;
225+
if (jsonObject != null && jsonObject.ContainsKey("base64String"))
226+
{
227+
string base64 = jsonObject["base64String"];
228+
string base64String = base64.Split(new string[] { "data:application/pdf;base64," }, StringSplitOptions.None)[1];
229+
if (base64String != null || base64String != string.Empty)
230+
{
231+
byte[] byteArray = Convert.FromBase64String(base64String);
232+
Console.WriteLine("redaction");
233+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(byteArray);
234+
foreach (PdfLoadedPage loadedPage in loadedDocument.Pages)
235+
{
236+
List<PdfLoadedAnnotation> removeItems = new List<PdfLoadedAnnotation>();
237+
foreach (PdfLoadedAnnotation annotation in loadedPage.Annotations)
238+
{
239+
if (annotation is PdfLoadedRectangleAnnotation)
240+
{
241+
if (annotation.Author == "Redaction")
242+
{
243+
// Add the annotation to the removeItems list
244+
removeItems.Add(annotation);
245+
// Create a new redaction with the annotation bounds and color
246+
PdfRedaction redaction = new PdfRedaction(annotation.Bounds, annotation.Color);
247+
// Add the redaction to the page
248+
loadedPage.AddRedaction(redaction);
249+
annotation.Flatten = true;
250+
}
251+
if (annotation.Author == "Text")
252+
{
253+
// Add the annotation to the removeItems list
254+
removeItems.Add(annotation);
255+
// Create a new redaction with the annotation bounds and color
256+
PdfRedaction redaction = new PdfRedaction(annotation.Bounds);
257+
//Set the font family and font size
258+
PdfStandardFont font = new PdfStandardFont(PdfFontFamily.Courier, 8);
259+
//Create the appearance like repeated text in the redaction area
260+
CreateRedactionAppearance(redaction.Appearance.Graphics, PdfTextAlignment.Left, true, new SizeF(annotation.Bounds.Width, annotation.Bounds.Height), RedactionText, font, PdfBrushes.Red);
261+
// Add the redaction to the page
262+
loadedPage.AddRedaction(redaction);
263+
annotation.Flatten = true;
264+
}
265+
//Apply the pattern for the Redaction
266+
if (annotation.Author == "Pattern")
267+
{
268+
// Add the annotation to the removeItems list
269+
removeItems.Add(annotation);
270+
// Create a new redaction with the annotation bounds and color
271+
PdfRedaction redaction = new PdfRedaction(annotation.Bounds);
272+
Syncfusion.Drawing.RectangleF rect = new Syncfusion.Drawing.RectangleF(0, 0, 8, 8);
273+
PdfTilingBrush tillingBrush = new PdfTilingBrush(rect);
274+
tillingBrush.Graphics.DrawRectangle(PdfBrushes.Gray, new Syncfusion.Drawing.RectangleF(0, 0, 2, 2));
275+
tillingBrush.Graphics.DrawRectangle(PdfBrushes.White, new Syncfusion.Drawing.RectangleF(2, 0, 2, 2));
276+
tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(4, 0, 2, 2));
277+
tillingBrush.Graphics.DrawRectangle(PdfBrushes.DarkGray, new Syncfusion.Drawing.RectangleF(6, 0, 2, 2));
278+
tillingBrush.Graphics.DrawRectangle(PdfBrushes.White, new Syncfusion.Drawing.RectangleF(0, 2, 2, 2));
279+
tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(2, 2, 2, 2));
280+
tillingBrush.Graphics.DrawRectangle(PdfBrushes.Black, new Syncfusion.Drawing.RectangleF(4, 2, 2, 2));
281+
tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(6, 2, 2, 2));
282+
tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(0, 4, 2, 2));
283+
tillingBrush.Graphics.DrawRectangle(PdfBrushes.DarkGray, new Syncfusion.Drawing.RectangleF(2, 4, 2, 2));
284+
tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(4, 4, 2, 2));
285+
tillingBrush.Graphics.DrawRectangle(PdfBrushes.White, new Syncfusion.Drawing.RectangleF(6, 4, 2, 2));
286+
tillingBrush.Graphics.DrawRectangle(PdfBrushes.Black, new Syncfusion.Drawing.RectangleF(0, 6, 2, 2));
287+
tillingBrush.Graphics.DrawRectangle(PdfBrushes.LightGray, new Syncfusion.Drawing.RectangleF(2, 6, 2, 2));
288+
tillingBrush.Graphics.DrawRectangle(PdfBrushes.Black, new Syncfusion.Drawing.RectangleF(4, 6, 2, 2));
289+
tillingBrush.Graphics.DrawRectangle(PdfBrushes.DarkGray, new Syncfusion.Drawing.RectangleF(6, 6, 2, 2));
290+
rect = new Syncfusion.Drawing.RectangleF(0, 0, 16, 14);
291+
PdfTilingBrush tillingBrushNew = new PdfTilingBrush(rect);
292+
tillingBrushNew.Graphics.DrawRectangle(tillingBrush, rect);
293+
//Set the pattern for the redaction area
294+
redaction.Appearance.Graphics.DrawRectangle(tillingBrushNew, new Syncfusion.Drawing.RectangleF(0, 0, annotation.Bounds.Width, annotation.Bounds.Height));
295+
// Add the redaction to the page
296+
loadedPage.AddRedaction(redaction);
297+
annotation.Flatten = true;
298+
}
299+
}
300+
else if (annotation is PdfLoadedRubberStampAnnotation)
301+
{
302+
if (annotation.Author == "Image")
303+
{
304+
Stream[] images = PdfLoadedRubberStampAnnotationExtension.GetImages(annotation as PdfLoadedRubberStampAnnotation);
305+
// Create a new redaction with the annotation bounds and color
306+
PdfRedaction redaction = new PdfRedaction(annotation.Bounds);
307+
images[0].Position = 0;
308+
PdfImage image = new PdfBitmap(images[0]);
309+
//Apply the image to redaction area
310+
redaction.Appearance.Graphics.DrawImage(image, new Syncfusion.Drawing.RectangleF(0, 0, annotation.Bounds.Width, annotation.Bounds.Height));
311+
// Add the redaction to the page
312+
loadedPage.AddRedaction(redaction);
313+
annotation.Flatten = true;
314+
}
315+
}
316+
}
317+
foreach (PdfLoadedAnnotation annotation1 in removeItems)
318+
{
319+
loadedPage.Annotations.Remove(annotation1);
320+
}
321+
}
322+
loadedDocument.Redact();
323+
MemoryStream stream = new MemoryStream();
324+
loadedDocument.Save(stream);
325+
stream.Position = 0;
326+
loadedDocument.Close(true);
327+
byteArray = stream.ToArray();
328+
finalbase64 = "data:application/pdf;base64," + Convert.ToBase64String(byteArray);
329+
stream.Dispose();
330+
return Content(finalbase64);
331+
}
332+
}
333+
334+
return Content("data:application/pdf;base64," + "");
335+
}
336+
337+
//The Method used for apply the text in the full area of redaction rectangle
338+
private static void CreateRedactionAppearance(PdfGraphics graphics, PdfTextAlignment alignment, bool repeat, SizeF size, string overlayText, PdfFont font, PdfBrush textcolor)
339+
{
340+
float col = 0, row;
341+
if (font == null) font = new PdfStandardFont(PdfFontFamily.Helvetica, 10);
342+
int textAlignment = Convert.ToInt32(alignment);
343+
float y = 0, x = 0, diff = 0;
344+
Syncfusion.Drawing.RectangleF rect;
345+
Syncfusion.Drawing.SizeF textsize = font.MeasureString(overlayText);
346+
347+
if (repeat)
348+
{
349+
col = size.Width / textsize.Width;
350+
row = (float)Math.Floor(size.Height / font.Size);
351+
diff = Math.Abs(size.Width - (float)(Math.Floor(col) * textsize.Width));
352+
if (textAlignment == 1)
353+
x = diff / 2;
354+
if (textAlignment == 2)
355+
x = diff;
356+
for (int i = 1; i < col; i++)
357+
{
358+
for (int j = 0; j < row; j++)
359+
{
360+
rect = new Syncfusion.Drawing.RectangleF(x, y, 0, 0);
361+
graphics.DrawString(overlayText, font, textcolor, rect);
362+
y = y + font.Size;
363+
}
364+
x = x + textsize.Width;
365+
y = 0;
366+
}
367+
}
368+
else
369+
{
370+
diff = Math.Abs(size.Width - textsize.Width);
371+
if (textAlignment == 1)
372+
{
373+
x = diff / 2;
374+
}
375+
if (textAlignment == 2)
376+
{
377+
x = diff;
378+
}
379+
rect = new Syncfusion.Drawing.RectangleF(x, 0, 0, 0);
380+
graphics.DrawString(overlayText, font, textcolor, rect);
381+
}
382+
}
383+
218384
[HttpPost]
219385
[EnableCors("AllowAllOrigins")]
220386
[Route("SaveUrl")]

0 commit comments

Comments
 (0)