Skip to content
Guides

Add Headers or Footers in PDF

This sample shows how to add and remove headers and footers.

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

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

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

// Edit header footer
- (void)editHeaderFooter {
  NSString *commandLineStr = @"";
  
    commandLineStr = [commandLineStr stringByAppendingString:@"-------------------------------------\n"];
    commandLineStr = [commandLineStr stringByAppendingString:@"Samples 3: Edit header footer\n"];
    
    // Get Sandbox path for saving the PDF File
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"HeaderFoooter"];
    NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"HeaderFoooter",@"AddCommonHeaderFooterTest"];
    
    // 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,@"EditHeaderFooterTest"];
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
        [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
    
    // Edit text header footer
    NSURL *editHeaderFooterURL = [NSURL fileURLWithPath:writeFilePath];
    CPDFDocument *document = [[CPDFDocument alloc] initWithURL:editHeaderFooterURL];
    
    CPDFHeaderFooter *headerFooter = document.headerFooter;
    [headerFooter setText:@"ComPDFKit Samples" atIndex:0];
    [headerFooter setText:@"ComPDFKit" atIndex:1];
    [headerFooter setText:@"ComPDFKit" atIndex:2];
    [headerFooter update];
    
    for (int i = 0; i < 3; i++) {
        commandLineStr = [commandLineStr stringByAppendingFormat:@"Text: %@\n", [headerFooter textAtIndex:i]];
        commandLineStr = [commandLineStr stringByAppendingFormat:@"Location: %@\n\n", [self getStringFromEnumLocation:i]];
    }
    
    [document writeToURL:editHeaderFooterURL];
    
    commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in EditHeaderFooterTest.pdf\n"];
}

// Delete header footer
- (void)deleteHeaderFooter {
  NSString *commandLineStr = @"";
  
    commandLineStr = [commandLineStr stringByAppendingString:@"-------------------------------------\n"];
    commandLineStr = [commandLineStr stringByAppendingString:@"Samples 4: Delete header footer\n"];
    
    // Save a document in Sandbox
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"HeaderFoooter"];
    NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"HeaderFoooter",@"AddCommonHeaderFooterTest"];
    
    // 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,@"DeleteHeaderFooterTest"];
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
        [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
    
    NSURL *deleteHeaderFooterURL = [NSURL fileURLWithPath:writeFilePath];
    CPDFDocument *document = [[CPDFDocument alloc] initWithURL:deleteHeaderFooterURL];
    
    // Delete header footer
    CPDFHeaderFooter *headerFooter = document.headerFooter;
    [headerFooter clear];
    
    [document writeToURL:deleteHeaderFooterURL];
    commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in DeleteHeaderFooterTest.pdf\n"];
}

- (NSString *)getStringFromEnumLocation:(NSInteger)enums {
    switch (enums) {
        case 0:
            return @"Top Left";
            break;
        case 1:
            return @"Top Middle";
            break;
        case 2:
            return @"Top Right";
            break;
        case 3:
            return @"Button Left";
            break;
        case 4:
            return @"Button Middle";
            break;
        case 5:
            return @"Button Right";
            break;
        default:
            return @" ";
            break;
    }
}