Welcome Guest! To enable all features, please Login or Register.

Notification

Icon
Error

Options
View
Last Go to last post Unread Go to first unread post
#1 Posted : Thursday, June 21, 2018 3:53:55 PM(UTC)
Duncan Quirk

Groups: Registered, Tech Support, Administrators
Posts: 70

Was thanked: 4 time(s) in 4 post(s)

When working with any file, it is important to bear in mind that some files will contain sensitive information. When archiving digital files, it is often important to remove any sensitive data (such as social security numbers, or MICR information on checks). The attached demo written in C# using V20 of the LEADTOOLS SDK showcases how to take an input file, extract all of the text, and how to search and redact the text based off a regular expression. For the purposes of this demo, we are searching for any word containing LEAD or LEADTOOLS.

The code:
Code:

            string inputFile = @"C:\Users\Public\Documents\LEADTOOLS Images\leadtools.pdf";
            string outputFile = $@"{Path.GetDirectoryName(inputFile)}\{Path.GetFileNameWithoutExtension(inputFile)}-redacted.pdf";
            using (var ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD, false))
            {
               ocrEngine.Startup(null, null, null, OcrEnginePath);
               var options = new LoadDocumentOptions();
               using (var document = DocumentFactory.LoadFromFile(inputFile, options))
               {
                  document.IsReadOnly = false;
                  document.Text.OcrEngine = ocrEngine;

                  RasterImage redactedDocument = document.Pages.First().GetImage();
                  foreach(var page in document.Pages)
                  {
                     if (page.PageNumber == 1)
                        continue;
                     redactedDocument.AddPage(page.GetImage());
                  }

                  Parallel.ForEach(document.Pages, (page) =>
                  {
                     AnnContainer container = new AnnContainer();
                     var pageText = page.GetText();
                     pageText.BuildWords();

                     //Regex to search for all instances of LEADTOOLS or LEAD
                     var pattern = "(LEADTOOLS|LEAD)";
                     var rgx = new Regex(pattern, RegexOptions.IgnoreCase);
                     var annotations = new ConcurrentBag<AnnRedactionObject>();
                     Parallel.ForEach(pageText.Words, (word) =>
                     {
                        if (rgx.Match(word.Value.ToLower()).Success)
                        {
                           AnnRedactionObject redactionObject = new AnnRedactionObject();
                           redactionObject.Rect = word.Bounds;
                           redactionObject.Fill = AnnSolidColorBrush.Create("Black");
                           annotations.Add(redactionObject);
                        }
                     });

                     var imagePage = page.GetImage();
                     foreach (var annotation in annotations)
                        container.Children.Add(annotation);

                     AnnWinFormsRenderingEngine e = new AnnWinFormsRenderingEngine();
                     e.RenderOnImage(container, imagePage);

                     redactedDocument.ReplacePage(page.PageNumber, imagePage);
                  });

                  using (RasterCodecs codecs = new RasterCodecs())
                     codecs.Save(redactedDocument, outputFile, RasterImageFormat.RasPdfJpeg, 0);

                  redactedDocument.Dispose();
                  Console.WriteLine($"File has been successfully redacted, and saved to {outputFile}");
               }
            }


File Attachment(s):
Redact Document.zip (4kb) downloaded 177 time(s).
Duncan Quirk
Developer Support Engineer
LEAD Technologies, Inc.

LEAD Logo
 

Try the latest version of LEADTOOLS for free for 60 days by downloading the evaluation: https://www.leadtools.com/downloads

Wanna join the discussion? Login to your LEADTOOLS Support accountor Register a new forum account.

You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF.NET | YAF.NET © 2003-2019, Yet Another Forum.NET
This page was generated in 0.086 seconds.