前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Edit PDF Pages With ComPDFKit in Objective-C

Edit PDF Pages With ComPDFKit in Objective-C

原创
作者头像
Youna
发布2022-09-28 13:32:42
3110
发布2022-09-28 13:32:42
举报
文章被收录于专栏:ComPDFKit tutorials

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.

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.

代码语言:javascript
复制
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.

代码语言:javascript
复制
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.

代码语言:javascript
复制
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.

代码语言:javascript
复制
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.

代码语言:javascript
复制
[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.

代码语言:javascript
复制
CPDFPage *page =   [document pageAtIndex:4];
NSInteger rotate = page.rotation;
rotate += 180;
rotate = rotate % 360;
[page setRotation:rotate];

Conclusion

ComPDFKit provides complete PDF editing features like editing PDFs, security, etc.

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
作者已关闭评论
0 条评论
热度
最新
推荐阅读
目录
  • The Operations Between PDF Files
    • Import PDF Pages
      • Replace PDF Pages
      • The Operations in the Current PDF file
        • Add PDF Pages
          • Delete PDF Pages
            • Move PDF Pages
              • Exchange PDF Pages
                • Rotate PDF Page
                • Conclusion
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档