iOS
ComPDFKit PDF SDK
Guides

Document Editor

 

ComPDFKit PDF SDK provides a wide range of APIs for document editing operations. These are mostly available through the CPDFDocument and CPDFPage classes.

ComPDFKit PDF SDK benefits include:

- PDF manipulation Split pages Merge pages Extract pages

- Page edit Delete pages Insert pages (choose from another document, a blank page or an image) Move pages Rotate pages Replace pages Exchange pages

- Access document information

- Extract image

 

PDF Manipulation                

 

Split pages

 

CPDFDocument can extract ranges of pages from one document and put them into another document. If you run this operation multiple times with different page indexes, you can effectively split a PDF into as many documents as you require.

 

To split a PDF document into multiple pages, please use the following method:

 

1. Create a blank PDF document.

CPDFDocument *document = [[[CPDFDocument alloc] init] autorelease];

 

2. Open PDF document that contains the pages you want to split.

// File path
NSString *path1 = @"...";
NSURL *url1 = [NSURL fileURLWithPath:path1];
CPDFDocument *document1 = [[[CPDFDocument alloc] initWithURL:url1] autorelease];

 

3. Extract specific pages from PDF document that you just opened, and import it into the blank PDF document.

// Pages that need to be split, e.g. 2 to 5 pages
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 4)];
[document importPages:indexSet fromDocument:document1 atIndex:0];

 

4. Save the document.

// Save path
NSString *path = @"...";
NSURL *url = [NSURL fileURLWithPath:path];
[document writeToURL:url];

 

Merge pages

 

ComPDFKit allows you to instantiate multiple CPDFDocument, and you can use the CPDFDocument API to merge multiple PDF files into a single one.

To merge PDF documents into one file, please use the following method:

 

1. Create a blank PDF document.

CPDFDocument *document = [[[CPDFDocument alloc] init] autorelease];

 

2. Open the PDF documents that contain the pages you want to merge.

// File path
NSString *path1 = @"...";
NSURL *url1 = [NSURL fileURLWithPath:path1];
CPDFDocument *document1 = [[[CPDFDocument alloc] initWithURL:url1] autorelease];
​
// File path
NSString *path2 = @"...";
NSURL *url2 = [NSURL fileURLWithPath:path2];
CPDFDocument *document2 = [[[CPDFDocument alloc] initWithURL:url2] autorelease];

 

3. Merge all the pages from the documents you just opened, and import it into the blank PDF document.

[document importPages:nil fromDocument:document1 atIndex:document.pageCount];
[document importPages:nil fromDocument:document2 atIndex:document.pageCount];


4. Save the document.

// Save path
NSString *path = @"...";
NSURL *url = [NSURL fileURLWithPath:path];
[document writeToURL:url];

 

The sample code above allows you to merge all the pages from the two documents. If you’re looking to merge or add specific pages from one document to another, you can use importPages of CPDFDocument::importPages:fromDocument:atIndex: to set specific pages.

 

Extract pages

 

CPDFDocument can extract ranges of pages from one document and put them into a blank document. If you run this operation, you can effectively extract a PDF as you require. Refer to split pages for more details.