Edit PDF Pages With ComPDFKit in Objective-C

By ComPDFKit | 2022 Oct 28
Document Editor Objective-C

It’s annoying to receive PDFs with much extra useless information. A large PDF document could make sharing difficult. So, for electric file editors, it’s important to provide the function of editing PDFs like adding and deleting PDF pages. 


We will introduce how to edit PDF pages with ComPDFKit in Objective-C here. The features we mentioned in the following are deleting, adding, moving, exchanging, replacing, and rotating.



The Operations Between PDF Files


Import PDF Pages

ComPDFKit supports importing PDF pages from other PDFs to add lots of information. Here are the methods in code. If you don’t specify the pages, the whole PDF will be imported.


NSURL *url = [NSURL fileURLWithPath:@""];
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];

NSURL *improtUrl = [NSURL fileURLWithPath:@""];;
CPDFDocument *importDocument = [[CPDFDocument alloc]initWithURL:improtUrl];

NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
for (NSUInteger index = 0; index< importDocument.pageCount; index++) {
     [indexSet addIndex:index];
[document importPages:indexSet fromDocument:importDocument atIndex:document.pageCount];


Replace PDF Pages

If you want to delete and insert the pages, replace PDF pages in one step by CPDFDocument::removePageAtIndexSet: and  CPDFDocument::importPages:fromDocument:atIndex:.



The Operations in the Current PDF file


Add PDF Pages

ComPDFKit supports inserting one page or multiple pages into PDFs. You can insert blank pages for adding little content to present. Here are the methods in code to add a blank page in front of page 3. 


NSURL *url = [NSURL fileURLWithPath:@""];
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];

[document insertPage:CGSizeMake(595.0, 842.0) atIndex:2];


Delete PDF Pages

An easy way to delete PDF pages is by interface removePageAtIndexSet:. The pages you want to delete could be a single page, a range of PDF pages, or some randomly selected pages. The code below shows the methods of deleting page 5 and the page range from 8 to 10.


NSURL *url = [NSURL fileURLWithPath:@""];
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];

NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
[indexSet addIndex:4];
NSRange range = NSMakeRange(7, 3);
[indexSet addIndexesInRange:range];

[document removePageAtIndexSet:indexSet];


Move PDF Pages

To have a better reading experience or arrange the content more properly, ComPDFKit supports moving a PDF page or a group of PDF pages. The features we often used when preparing presentations or reports. Follow the methods to move pages 1~2 and insert them after page 5.


NSURL *url = [NSURL fileURLWithPath:@""];
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];

CPDFPage *page0 = [document pageAtIndex:0];
CPDFPage *page1 = [document pageAtIndex:1];
[document movePageAtIndex:[document indexForPage:page0] withPageAtIndex:4];
[document movePageAtIndex:[document indexForPage:page1] withPageAtIndex:[document indexForPage:page0]];


Exchange PDF Pages

With ComPDFKit, you could exchange the location of two PDF pages. Follow the method below to exchange the location of pages 3 and 7.


[document exchangePageAtIndex:2 withPageAtIndex:6];


Rotate PDF Page

Rotating pages could be done by the method in the CPDFPage class. Pages can be rotated at 0, 90, 180, or 270 degrees. Follow the method below to rotate page 5 in 180 degrees.


CPDFPage *page =   [document pageAtIndex:4];
NSInteger rotate = page.rotation;
rotate += 180;
rotate = rotate % 360;
[page setRotation:rotate];



ComPDFKit provides complete PDF editing features. Contact us for more other features like editing PDFs, security, etc.