How to Set PDF Background in Objective-C

By ComPDFKit | 2022 Aug 18
Security Objective-C

By default, the color of the PDF content is black over a white background. The words would be displayed clearly and outstandingly in this way. However, with a few lines of code, we can also adjust the background for an eye-friendly reading environment, or for displaying something users want to spread. 


We will show you how to set and adjust the background with our ComPDFKit PDF SDK in Objective-C here.



Background Setting


ComPDFKit PDF SDK supports color and image background. You can choose the pages to apply the background. And there are some specific settings for the background like opacity, zoom ratio, rotation angle, etc.



Follow the sample code to set the background to different colors. The code sample below is to set a blue background. You can change the color as you want.


NSURL *url = [NSURL fileURLWithPath:@""];
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
CPDFBackground *background = [document background];
background.type = CPDFBackgroundTypeColor;
background.color = [UIColor blueColor];
[background update];



Import an image and set the background with Objective-C according to the following code. Here is an image of our logo which is set as a background behind the text. It will display as picture 1-1.


NSURL *url = [NSURL fileURLWithPath:@""];
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
CPDFBackground *background = [document background];
background.type = CPDFBackgroundTypeImage;
background.scale = 1.0;
[background setImage:[UIImage imageNamed:@""]];
[background update];


Set Background with ComPDFKitPicture 1-1



Background Changing


There is no standard method to process PDF background in PDF SDK on the market. You may fail to change the background which was set by another method of PDF background. So, make sure to set, reset, and delete the background in the products powered by the same PDF SDK.



We can delete the background easily. Here is the sample code in Objective-C. For other programming languages, you can contact us


[background clear];



Every PDF page has one background. You can set different backgrounds for different pages. The background will be replaced if you set a background for a page that has already had a background. The method of resetting the background is the same as setting the background.



Background vs Display Themes


Like we said before, the background in PDF can be changed to different colors. It looks like page display themes. And you can set any color of both background and themes. Of course, they have differences.


As for the PDF background, the setting of color will be saved with this file. It will be displayed all the time as settled if you don’t delete the setting background. For page display themes, you can call CPDFView::setDisplayMode: or CPDFView::setDisplayModeCustomColor: to set the color. When you close the PDF, the theme color will exit. Choose the different functions of PDF SDK as your needs.



More than PDF Background

Except for PDF background, ComPDFKit PDF SDK supports all the basic and advanced PDF features from PDF viewing and editing to OCR and redacting. Connect to our website for more information.