Search and Redact Text in Objective-C

Tutorials | Redaction · Objective-C Tue. 28 Feb. 2023

In the last blog, we introduced the feature of redaction which allows you to permanently and irrevocably remove content from PDF documents.

 

Image a use case for redaction — search and redact text such as email addresses, phone numbers, or any other sensitive text. These two functions can be combined together to improve work efficiency for lawyers, HR, etc. In this tutorial,we'll keep things easy by discussing how to redact a simple search term using Objective-C and ComPDFKit for iOS.

 

 

Search for a Specific Text to Redact 

 

You can search for a specific text and identify all of its instances for redaction in the PDF document. For example, let’s search for and redact the text “ComPDFKit”. Here’s how it looks in Objective-C:

 

NSURL *url = [NSURL fileURLWithPath:@""];
  CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];

   CPDFSearchOptions options = CPDFSearchCaseInsensitive;
   NSArray  * pageSelections =  [document findString:@"ComPDFKit" withOptions:options];

      for (NSArray  * selections  in pageSelections) {
          for (CPDFSelection *currentSelection in selections) {
              NSMutableArray *quadrilateralPoints = [NSMutableArray array];
              CPDFRedactAnnotation *annotation = [[CPDFRedactAnnotation alloc] initWithDocument:document];
              for (CPDFSelection *selection in currentSelection.selectionsByLine) {
                  CGRect bounds = selection.bounds;
                  [quadrilateralPoints addObject:[NSValue valueWithPoint:CGPointMake(CGRectGetMinX(bounds), CGRectGetMaxY(bounds))]];
                  [quadrilateralPoints addObject:[NSValue valueWithPoint:CGPointMake(CGRectGetMaxX(bounds), CGRectGetMaxY(bounds))]];
                  [quadrilateralPoints addObject:[NSValue valueWithPoint:CGPointMake(CGRectGetMinX(bounds), CGRectGetMinY(bounds))]];
                  [quadrilateralPoints addObject:[NSValue valueWithPoint:CGPointMake(CGRectGetMaxX(bounds), CGRectGetMinY(bounds))]];
              }
              [annotation setQuadrilateralPoints:quadrilateralPoints];

              [currentSelection.page addAnnotation:annotation];
          }
      }

 

After redacting the sensitive information, you can also customize the font size, font color, background colors, overlay text, etc. as following:

 

Change the font and the font size:

NSFont* font = [NSFont fontWithName:@"Helvetica" size:14.0];
   [(CPDFRedactAnnotation *)annotation setFont:font];

 

Change the font color:

[(CPDFRedactAnnotation *)annotation setFontColor:[CPDFKitPlatformColor redColor]];

 

Change the background colors:

[(CPDFRedactAnnotation *)annotation setInteriorColor:[CPDFKitPlatformColor yellowColor]];

 

Repeat overlay text:

[(CPDFRedactAnnotation *)annotation setOverlayText:@"Redact"];

 

Then, when everything is settled, you can apply the redactions.

[(CPDFRedactAnnotation *)annotation applyRedaction];

 

That’s it! 



Conclusion

 

In this blog, we try to introduce how these two features combine together, and be suitable to your needs. But in real situation, there is more to explore. So don’t hesitate to contact us!

Ready to Get Started?

Download our all-in-one ComPDFKit for free and run it to your project within minutes!