It’s annoying to receive PDFs which are with much extra information or less information than they should have. 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.
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];
If you want to delete and insert the pages, Replace PDF pages in one step by CPDFDocument::removePageAtIndexSet:
and CPDFDocument::importPages:fromDocument:atIndex:
.
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];
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];
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]];
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];
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 like editing PDFs, security, etc.
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。