Skip to content
Guides

Redact Text and Data in PDF

This sample shows how to tag ciphertext and is a separate permissioned add-on that provides options to delete (not just overwrite or obscure) content within the PDF area. For a printed page, redaction involves removing or deleting certain areas of the printed page. For electronic documents that use formats such as PDF, redaction typically involves removing sensitive content from the document for safe distribution to courts, patent and government agencies, the media, customers, suppliers, or any other audience with restricted access to the content.

objective-c
// Init Methods
- (instancetype)initWithDocument:(CPDFDocument *)document {
  CPDFDocument *myDocument = document;
  [self addRadact:myDocument];
}

// Add redact
- (void)addRadact:(CPDFDocument *)oldDocument {
  NSString *commandLineStr = @"";
  
    // Get Sandbox path for saving the PDF File
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Redact"];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
        [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"AddRedactTest"];
    
    // Save the document in the test PDF file
    NSURL *addRedactURL = [NSURL fileURLWithPath:writeFilePath];
    [oldDocument writeToURL:addRedactURL];
    
    // Create a new document for test PDF file
    CPDFDocument *document = [[CPDFDocument alloc] initWithURL:addRedactURL];
    
    // Add redact
    CPDFPage *page = [document pageAtIndex:0];
    
    NSArray *resultArray = [document findString:@"Page" withOptions:CPDFSearchCaseInsensitive];
    
    // Get the first page of search resultsGet the first page of search results
    NSArray *selections = [resultArray objectAtIndex:3];
    
    // Get the first search result on the first page
    CPDFSelection *selection = [selections objectAtIndex:0];
    
    CPDFRedactAnnotation *redact = [[CPDFRedactAnnotation alloc] initWithDocument:document];
    redact.bounds = selection.bounds;
    redact.overlayText = @"REDACTED";
    redact.font = [UIFont systemFontOfSize:12];
    redact.fontColor = [UIColor redColor];
    redact.alignment = NSTextAlignmentLeft;
    redact.interiorColor = [UIColor blackColor];
    redact.borderColor = [UIColor yellowColor];
    [page addAnnotation:redact];
    
    [document writeToURL:addRedactURL];
    
    commandLineStr = [commandLineStr stringByAppendingFormat:@"The text need to be redact is: Page%d\n", (int)[document indexForPage:page]];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"\tText in the redacted area is: %d, %d, %d, %d\n\n", (int)redact.bounds.origin.x,(int)redact.bounds.origin.y, (int)redact.bounds.size.width, (int)redact.bounds.size.height];
    commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in AddRedactTest.pdf\n"];
}