截取PDF的某一页 iOS

这篇博客介绍了如何在iOS应用中截取并保存PDF的某一页到本地。ReaderDocument类提供了两个方法,一个用于保存指定页,另一个用于删除相应页面。代码涉及到CGPDFDocumentRef和UIGraphicsBeginPDFContextToFile等技术,通过CGPDFPageGetDrawingTransform和CGPDFDocumentGetPage等API来处理PDF页面。
项目中,可能需要截取某一份PDF喜欢的页面,所以需要进行对PDF进行切割。网上找了好多资源都没解决。下面附上基于Reader第三方的解决方法。

在ReaderDocument.h中暴露两个方法

/**

  保存PDF的某一页到本地

 */

+ (void) saveOnePDFPageWithDocument:(ReaderDocument *)document pdfID:(NSString *)pdfID FileName:(NSString *)fileName;

/**

   删除保存在本地的PDF、通过相同的文件名删除

 */

+ (void) deleteOnePDFPageWithDocument:(ReaderDocument *)document pdfID:(NSString *)pdfID FileName:(NSString *)fileName;


=====================================
ReaderDocument.m中实现两个方法

///////////////////////////////删除本地相同PDF名称页面///////////////////////////////////

+ (void) deleteOnePDFPageWithDocument:(ReaderDocument *)document pdfID:(NSString *)pdfID FileName:(NSString *)fileName

{

    //创建文件管理器

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSString *tmpFolder=[NSString stringWithFormat:@"%@/tmp/%@-%@",NSHomeDirectory(),fileName,pdfID];

    NSLog(@"地址是:%@",tmpFolder);

    BOOL fileExists = [fileManager fileExistsAtPath:tmpFolder];

    if (fileExists) {//如果不存在说创建

        NSError *err;

        [fileManager removeItemAtPath:tmpFolder error:&err];

        NSLog(@"%@",err);

    }

}



+ (void) saveOnePDFPageWithDocument:(ReaderDocument *)document pdfID:(NSString *)pdfID FileName:(NSString *)fileName

{

    

    //创建文件管理器

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSString *tmpFolder=[NSString stringWithFormat:@"%@/tmp",NSHomeDirectory()];

     NSLog(@"保存地址是:%@",tmpFolder);

    BOOL fileExists = [fileManager fileExistsAtPath:tmpFolder];

    if (!fileExists) {//如果不存在说创建

        [fileManager createDirectoryAtPath:tmpFolder

               withIntermediateDirectories:YES

                                attributes:nil

                                     error:nil];

    }

    

    CGPDFDocumentRef doc = CGPDFDocumentCreateUsingUrl((__bridge CFURLRef)document.fileURL, document.password);

//    NSLog(@"合并地址是:%@",document.fileURL);

    

    NSString *signFileName=[NSString stringWithFormat:@"%@-%@",fileName,pdfID];

    //[signFileName stringByAppendingString:iName];

    


    NSString *tempPath = [NSTemporaryDirectory() stringByAppendingString:signFileName];

    

    //CGRectZero means the default page size is 8.5x11

    //We don't care about the default anyway, because we set each page to be a specific size

    UIGraphicsBeginPDFContextToFile(tempPath, CGRectZero, nil);


    //Iterate over each page - 1-based indexing (obnoxious...)

    

    // 获取当前页码

    for (int i = [pdfID intValue]; i <= [pdfID intValue]; i++) {

        

        CGPDFPageRef page = CGPDFDocumentGetPage (doc, i); // grab page i of the PDF

        

        

        CGRect tmpRect = [ReaderDocument boundsForPDFPage:page];

        

        

        //Create a new page

        UIGraphicsBeginPDFPageWithInfo(tmpRect, nil);

        

        

        CGContextRef context = UIGraphicsGetCurrentContext();

        // flip context so page is right way up

        

        CGContextTranslateCTM(context, 0.0, tmpRect.size.height);

        CGContextScaleCTM(context, 1.0, -1.0);

        

        int angle = CGPDFPageGetRotationAngle(page);

        

        if (angle != 0 && angle !=180) {

            CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, tmpRect, 0, false));

        }

        

        

        CGContextDrawPDFPage (context, page); // draw the page into graphics context

        

        

        

        CGContextScaleCTM(context, 1.0, -1.0);

        CGContextTranslateCTM(context, 0, -tmpRect.size.height);

        

        if (angle != 0 && angle !=180) {

            CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, tmpRect, 0, false));

        }

        

    }

    

    UIGraphicsEndPDFContext();

    

    CGPDFDocumentRelease (doc);

    

}


#pragma mark-

+ (CGRect) boundsForPDFPage:(CGPDFPageRef) page{

    CGRect cropBoxRect = CGPDFPageGetBoxRect(page, kCGPDFCropBox);

    CGRect mediaBoxRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);

    CGRect effectiveRect = CGRectIntersection(cropBoxRect, mediaBoxRect);

    

    int pageAngle = CGPDFPageGetRotationAngle(page); // Angle

    

    float pageWidth, pageHeight, pageOffsetX, pageOffsetY;

    switch (pageAngle) // Page rotation angle (in degrees)

    {

        default: // Default case

        case 0: case 180: // 0 and 180 degrees

        {

            pageWidth = effectiveRect.size.width;

            pageHeight = effectiveRect.size.height;

            pageOffsetX = effectiveRect.origin.x;

            pageOffsetY = effectiveRect.origin.y;

            break;

        }

            

        case 90: case 270: // 90 and 270 degrees

        {

            pageWidth = effectiveRect.size.height;

            pageHeight = effectiveRect.size.width;

            pageOffsetX = effectiveRect.origin.y;

            pageOffsetY = effectiveRect.origin.x;

            break;

        }

    }

    

    return CGRectMake(pageOffsetX, pageOffsetY, pageWidth, pageHeight);

}


///////////////////////////////保存某一个单独PDF页面///////////////////////////////////




在ReaderViewController.m添加标签实现删除、保存功能

- (void)tappedInToolbar:(ReaderMainToolbar *)toolbar markButton:(UIButton *)button

{


    

    if (printInteraction != nil) [printInteraction dismissAnimated:YES];

    

     NSString *pdfID = [NSString stringWithFormat:@"%lu",(long)currentPage];

    

    NSString *fileName = [document.fileName stringByReplacingOccurrencesOfString:@".pdf" withString:@""];

    

    if ([document.bookmarks containsIndex:currentPage]) // Remove bookmark

    {

        [document.bookmarks removeIndex:currentPage]; [mainToolbar setBookmarkState:NO];

        

        //删除PDF

        [ReaderDocument deleteOnePDFPageWithDocument:document pdfID:pdfID FileName:fileName];


    }

    else // Add the bookmarked page number to the bookmark index set

    {

        [document.bookmarks addIndex:currentPage]; [mainToolbar setBookmarkState:YES];

        //合并PDF,历史图层,text图层方法、保存涂鸦笔记

      

        [ReaderDocument saveOnePDFPageWithDocument:document pdfID:pdfID FileName:fileName];

    }




}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值