您的位置:首页 > 移动开发 > IOS开发

截取PDF的某一页 iOS

2016-04-05 13:59 417 查看
项目中,可能需要截取某一份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];

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: