Skip to content
Guides

Add Backgrounds in PDF

This sample shows how to add color and picture backs and delete backgrounds.

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

// Set the document background color
- (void)addColorBackground:(CPDFDocument *)oldDocument {
  NSString *commandLineStr = @"";
  
    commandLineStr = [commandLineStr stringByAppendingString:@"-------------------------------------\n"];
    commandLineStr = [commandLineStr stringByAppendingString:@"Samples 1 : Set the document background color\n"];
    
    // Get Sandbox path for saving the PDFFile
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Background"];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
        [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"AddColorBackgroundTest"];
    
    // Save the document in the test PDF file
    NSURL *addColorBackgroundURL = [NSURL fileURLWithPath:writeFilePath];
    [oldDocument writeToURL:addColorBackgroundURL];
    
    // Create a new document for test PDF file
    CPDFDocument *document = [[CPDFDocument alloc] initWithURL:addColorBackgroundURL];
    
    // Create color background
    CPDFBackground *background = document.background;
    background.type = CPDFBackgroundTypeColor;
    background.color = [CPDFKitPlatformColor redColor]; // Background color (image background does not work).
    background.opacity = 1.0; // Background transparency, the range of 0~1, with the default of 1.
    background.scale = 1.0; // Background tiling scale.
    background.rotation = 0; // Background rotation angle, the range of 0~360, the default is 0 (rotate at the centre of the page).
    background.horizontalAlignment = 1; // Background vertical alignment. `0` for top alignment, `1` for centre alignment, `1` for bottom alignment.
    background.verticalAlignment = 1; // Horizontal alignment of the background. `0` for left alignment, `1` for centre alignment, `1` for right alignment.
    background.xOffset = 0; // The horizontal offset of the background. Positive numbers are shifted to the right, negative numbers are shifted to the left.
    background.yOffset = 0; // The vertical offset of the background. Positive numbers are shifted downwards, negative numbers are shifted upwards.

    background.pageString = @"0,1,2,3,4"; // Background page range, such as "0,3,5-7".

    [background update];
    
    [document writeToURL:addColorBackgroundURL];
    
    // Print color background object message
    commandLineStr = [commandLineStr stringByAppendingString:@"Type : Color"];
    CGFloat red, green, blue, alpha;
    [background.color getRed:&red green:&green blue:&blue alpha:&alpha];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"Color : red:%.1f, green:%.1f, blue:%.1f, alpha:%.1f\n", red, green, blue, alpha];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"Opacity :%.1f\n", background.opacity];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"Rotation :%.1f\n", background.rotation];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"Vertalign :%@\n", [self getStringFromEnumVertalign:background.verticalAlignment]];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"Horizalign :%@\n", [self getStringFromEnumHorizalign:background.horizontalAlignment]];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"VertOffset :%.1f\n", background.xOffset];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"HorizOffset :%.1f\n", background.yOffset];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"Pages :%@\n", background.pageString];
    commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in AddColorBackgroundTest.pdf\n"];
}

// Set the document background image
- (void)addImageBackground:(CPDFDocument *)oldDocument {
  NSString *commandLineStr = @"";
  
    commandLineStr = [commandLineStr stringByAppendingString:@"-------------------------------------\n"];
    commandLineStr = [commandLineStr stringByAppendingString:@"Samples 2 : Set the document background image\n"];
    
    // Get Sandbox path for saving the PDFFile
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Background"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
        [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"AddImageBackgroundTest"];
    
    // Save the document in the test PDF file
    NSURL *addImageBackgroundURL = [NSURL fileURLWithPath:writeFilePath];
    [oldDocument writeToURL:addImageBackgroundURL];
    
    // Create a new document for test PDF file
    CPDFDocument *document = [[CPDFDocument alloc] initWithURL:addImageBackgroundURL];
    
    // Create image background
    CPDFBackground *background = document.background;
    background.type = CPDFBackgroundTypeImage;
    [background setImage:[UIImage imageNamed:@"Logo"]];
    background.opacity = 1.0; // Background transparency, the range of 0~1, with the default of 1.
    background.scale = 1.0; // Background tiling scale.
    background.rotation = 0; // Background rotation angle, the range of 0~360, the default is 0 (rotate at the centre of the page).
    background.horizontalAlignment = 1; // Background vertical alignment. `0` for top alignment, `1` for centre alignment, `1` for bottom alignment.
    background.verticalAlignment = 1; // Horizontal alignment of the background. `0` for left alignment, `1` for centre alignment, `1` for right alignment.
    background.xOffset = 0; // The horizontal offset of the background. Positive numbers are shifted to the right, negative numbers are shifted to the left.
    background.yOffset = 0; // The vertical offset of the background. Positive numbers are shifted downwards, negative numbers are shifted upwards.

    background.pageString = @"0,1,2,3,4"; // Background page range, such as "0,3,5-7".

    [background update];
    
    [document writeToURL:addImageBackgroundURL];
    
    // Print image background object message
    commandLineStr = [commandLineStr stringByAppendingString:@"Type : Image\n"];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"Opacity :%.1f\n", background.opacity];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"Rotation :%.1f\n", background.rotation];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"Vertalign :%@\n", [self getStringFromEnumVertalign:background.verticalAlignment]];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"Horizalign :%@\n", [self getStringFromEnumHorizalign:background.horizontalAlignment]];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"VertOffset :%.1f\n", background.xOffset];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"HorizOffset :%.1f\n", background.yOffset];
    commandLineStr = [commandLineStr stringByAppendingFormat:@"Pages :%@\n", background.pageString];
    commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in AddImageBackgroundTest.pdf\n"];
}

// Delete document background
- (void)deleteColorBackground {
  NSString *commandLineStr = @"";
  
    commandLineStr = [commandLineStr stringByAppendingString:@"-------------------------------------\n"];
    commandLineStr = [commandLineStr stringByAppendingString:@"Samples 3 :  Delete document background\n"];
    
    // Get Sandbox path for saving the PDFFile
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Background"];
    NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"Background",@"AddColorBackgroundTest"];
    
    // 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,@"DeleteBackgroundTest"];
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
        [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
    
    
    NSURL *deleteBackgroundURL = [NSURL fileURLWithPath:writeFilePath];
    CPDFDocument *document = [[CPDFDocument alloc] initWithURL:deleteBackgroundURL];
    
    CPDFBackground *pageBackground = [document background];
    
    [pageBackground clear];
    
    [document writeToURL:deleteBackgroundURL];
    commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in AddImageBackgroundTest.pdf\n"];
}

- (NSString *)getStringFromEnumVertalign:(NSInteger)enums {
    switch (enums) {
        case 0:
            return @"top alignment";
            break;
        case 1:
            return @"center alignment";
            break;
        case 2:
            return @"button alignment";
            break;
        default:
            return @" ";
            break;
    }
}

- (NSString *)getStringFromEnumHorizalign:(NSInteger)enums {
    switch (enums) {
        case 0:
            return @"left alignment";
            break;
        case 1:
            return @"center alignment";
            break;
        case 2:
            return @"right alignment";
            break;
        default:
            return @" ";
            break;
    }
}