Skip to content
Guides

Add Bates Codes in PDF

This sample shows how to add and remove bates codes.

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

//  Insert common bates
- (void)addCommonBates:(CPDFDocument *)oldDocument {
  NSString *commandLineStr = @"";
  
    commandLineStr = [commandLineStr stringByAppendingString:@"-------------------------------------\n"];
    commandLineStr = [commandLineStr stringByAppendingString:@"Samples 1: Insert common bates\n"];
    
    // Get Sandbox path for saving the PDF File
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Bates"];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
        [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"AddCommonBatesTest"];
    
    // Save the document in the test PDF file
    NSURL *addCommonBatesURL = [NSURL fileURLWithPath:writeFilePath];
    [oldDocument writeToURL:addCommonBatesURL];
    
    // Create a new document for test PDF file
    CPDFDocument *document = [[CPDFDocument alloc] initWithURL:addCommonBatesURL];
    
    // Create common bates
    CPDFBates *bates = document.bates;
    [bates setText:@"<<#3#5#Prefix-#-Suffix>>" atIndex:0];
    [bates setTextColor:[CPDFKitPlatformColor redColor] atIndex:0];
    [bates setFontSize:14.0 atIndex:0];
    [bates setText:@"<<#3#5#Prefix-#-Suffix>>" atIndex:1];
    [bates setTextColor:[CPDFKitPlatformColor redColor] atIndex:1];
    [bates setFontSize:14.0 atIndex:1];
    [bates setText:@"<<#3#5#Prefix-#-Suffix>>" atIndex:2];
    [bates setTextColor:[CPDFKitPlatformColor redColor] atIndex:2];
    [bates setFontSize:14.0 atIndex:2];
    [bates setText:@"<<#3#5#Prefix-#-Suffix>>" atIndex:3];
    [bates setTextColor:[CPDFKitPlatformColor redColor] atIndex:3];
    [bates setFontSize:14.0 atIndex:3];
    [bates setText:@"<<#3#5#Prefix-#-Suffix>>" atIndex:4];
    [bates setTextColor:[CPDFKitPlatformColor redColor] atIndex:4];
    [bates setFontSize:14.0 atIndex:4];
    [bates setText:@"<<#3#5#Prefix-#-Suffix>>" atIndex:5];
    [bates setTextColor:[CPDFKitPlatformColor redColor] atIndex:5];
    [bates setFontSize:14.0 atIndex:5];
    bates.pageString = @"0-4";
    [bates update];
    
    // Print bates message
    for (int i = 0; i < 6; i++) {
        commandLineStr = [commandLineStr stringByAppendingFormat:@"Text: %@\n", [bates textAtIndex:i]];
        commandLineStr = [commandLineStr stringByAppendingFormat:@"Location: %@\n\n", [self getStringFromEnumLocation:i]];
    }
    
    [document writeToURL:addCommonBatesURL];
    
    commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in AddCommonBatesTest.pdf\n"];
}

// Edit common bates
- (void)editBates {
  NSString *commandLineStr = @"";
  
    commandLineStr = [commandLineStr stringByAppendingString:@"-------------------------------------\n"];
    commandLineStr = [commandLineStr stringByAppendingString:@"Samples 2: Edit common bates\n"];
    
    // Save a document in Sandbox
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Bates"];
    NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"Bates",@"AddCommonBatesTest"];
    
    // Copy file
    if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
        [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"EditBatesTest"];
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
        [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
    
    NSURL *editBatesURL = [NSURL fileURLWithPath:writeFilePath];
    CPDFDocument *document = [[CPDFDocument alloc] initWithURL:editBatesURL];
    
    // Edit dates message
    CPDFBates *bates = document.bates;
    [bates setText:@"<<#3#5#ComPDFKit-#-ComPDFKit>>" atIndex:0];
    [bates setTextColor:[CPDFKitPlatformColor redColor] atIndex:0];
    [bates setFontSize:14.0 atIndex:0];
    [bates setText:@"<<#3#5#Prefix-#-Suffix>>" atIndex:1];
    [bates setTextColor:[CPDFKitPlatformColor redColor] atIndex:1];
    [bates setFontSize:14.0 atIndex:1];
    [bates setText:@"<<#3#5#Prefix-#-Suffix>>" atIndex:2];
    [bates setTextColor:[CPDFKitPlatformColor redColor] atIndex:2];
    [bates setFontSize:14.0 atIndex:2];
    [bates setText:@"<<#3#5#Prefix-#-Suffix>>" atIndex:3];
    [bates setTextColor:[CPDFKitPlatformColor redColor] atIndex:3];
    [bates setFontSize:14.0 atIndex:3];
    [bates setText:@"<<#3#5#Prefix-#-Suffix>>" atIndex:4];
    [bates setTextColor:[CPDFKitPlatformColor redColor] atIndex:4];
    [bates setFontSize:14.0 atIndex:4];
    [bates setText:@"<<#3#5#Prefix-#-Suffix>>" atIndex:5];
    [bates setTextColor:[CPDFKitPlatformColor redColor] atIndex:5];
    [bates setFontSize:14.0 atIndex:5];
    bates.pageString = @"0-4";
    [bates update];
    
    for (int i = 0; i < 3; i++) {
        commandLineStr = [commandLineStr stringByAppendingFormat:@"Text: %@\n", [bates textAtIndex:i]];
        commandLineStr = [commandLineStr stringByAppendingFormat:@"Location: %@\n\n", [self getStringFromEnumLocation:i]];
    }
    
    [document writeToURL:editBatesURL];
    
    commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in EditBatesTest.pdf\n"];
}

// Delete common bates
- (void)deleteBates {
  NSString *commandLineStr = @"";
  
    commandLineStr = [commandLineStr stringByAppendingString:@"-------------------------------------\n"];
    commandLineStr = [commandLineStr stringByAppendingString:@"Samples 3: Delete common bates\n"];
    
    // Save a document in Sandbox
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Bates"];
    NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"Bates",@"AddCommonBatesTest"];
    
    // Copy file
    if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
        [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"DeleteBatesTest"];
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
        [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
    
    NSURL *deleteBatesURL = [NSURL fileURLWithPath:writeFilePath];
      CPDFDocument *document = [[CPDFDocument alloc] initWithURL:deleteBatesURL];
    
    CPDFBates *bates = document.bates;
    [bates clear];
    
    [document writeToURL:deleteBatesURL];
    commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in DeleteBatesTest.pdf\n"];
}