IOS - PDF合并 - 转
2015-09-29 21:23
465 查看
来自:http://www.cnblogs.com/tx8899/p/4082749.html
#pragma mark - Merge PDF
- (void)mergePDF
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath1 = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"阅办卡.pdf"];
NSString *filePath2 = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"文件.pdf"];
NSArray *PDFURLS = [NSArray arrayWithObjects:filePath1,filePath2, nil];
[self joinPDF:PDFURLS];
}
- (NSString *)joinPDF:(NSArray *)listOfPaths {
// File paths
NSString *fileName = [NSString stringWithFormat:@"公文%d.pdf",arc4random_uniform(100)];
NSString *pdfPathOutput = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0] stringByAppendingPathComponent:fileName];
CFURLRef pdfURLOutput = ( CFURLRef)CFBridgingRetain([NSURL fileURLWithPath:pdfPathOutput]);
NSInteger numberOfPages = 0;
// Create the output context
CGContextRef writeContext = CGPDFContextCreateWithURL(pdfURLOutput, NULL, NULL);
for (NSString *source in listOfPaths) {
CFURLRef pdfURL = ( CFURLRef)CFBridgingRetain([[NSURL alloc] initFileURLWithPath:source]);
//file ref
CGPDFDocumentRef pdfRef = CGPDFDocumentCreateWithURL((CFURLRef) pdfURL);
numberOfPages = CGPDFDocumentGetNumberOfPages(pdfRef);
// Loop variables
CGPDFPageRef page;
CGRect mediaBox;
// Read the first PDF and generate the output pages
// NSLog(@"GENERATING PAGES FROM PDF 1 (%@)...", source);
for (int i=1; i<=numberOfPages; i++) {
page = CGPDFDocumentGetPage(pdfRef, i);
mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
CGContextBeginPage(writeContext, &mediaBox);
CGContextDrawPDFPage(writeContext, page);
CGContextEndPage(writeContext);
}
CGPDFDocumentRelease(pdfRef);
CFRelease(pdfURL);
}
CFRelease(pdfURLOutput);
// Finalize the output file
CGPDFContextClose(writeContext);
CGContextRelease(writeContext);
return pdfPathOutput;
}
#pragma mark - Merge PDF
- (void)mergePDF
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath1 = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"阅办卡.pdf"];
NSString *filePath2 = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"文件.pdf"];
NSArray *PDFURLS = [NSArray arrayWithObjects:filePath1,filePath2, nil];
[self joinPDF:PDFURLS];
}
- (NSString *)joinPDF:(NSArray *)listOfPaths {
// File paths
NSString *fileName = [NSString stringWithFormat:@"公文%d.pdf",arc4random_uniform(100)];
NSString *pdfPathOutput = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0] stringByAppendingPathComponent:fileName];
CFURLRef pdfURLOutput = ( CFURLRef)CFBridgingRetain([NSURL fileURLWithPath:pdfPathOutput]);
NSInteger numberOfPages = 0;
// Create the output context
CGContextRef writeContext = CGPDFContextCreateWithURL(pdfURLOutput, NULL, NULL);
for (NSString *source in listOfPaths) {
CFURLRef pdfURL = ( CFURLRef)CFBridgingRetain([[NSURL alloc] initFileURLWithPath:source]);
//file ref
CGPDFDocumentRef pdfRef = CGPDFDocumentCreateWithURL((CFURLRef) pdfURL);
numberOfPages = CGPDFDocumentGetNumberOfPages(pdfRef);
// Loop variables
CGPDFPageRef page;
CGRect mediaBox;
// Read the first PDF and generate the output pages
// NSLog(@"GENERATING PAGES FROM PDF 1 (%@)...", source);
for (int i=1; i<=numberOfPages; i++) {
page = CGPDFDocumentGetPage(pdfRef, i);
mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
CGContextBeginPage(writeContext, &mediaBox);
CGContextDrawPDFPage(writeContext, page);
CGContextEndPage(writeContext);
}
CGPDFDocumentRelease(pdfRef);
CFRelease(pdfURL);
}
CFRelease(pdfURLOutput);
// Finalize the output file
CGPDFContextClose(writeContext);
CGContextRelease(writeContext);
return pdfPathOutput;
}
相关文章推荐
- iOS关于通讯录的实现
- iOS开发中对OC字符串的相关操作<转>
- iOS Framework
- iOS 自定义cell(思路,以及详细步骤)
- iOS 核心动画2
- iOS 核心动画-1
- iOS中各种动画分类
- ios 内存小结
- iOS开发——高级篇——如何集成支付宝SDK
- iOS大典之自定义cell
- IOS学习随笔——TK-0721
- IOS学习随笔——TK0720
- IOS JSON的四种解析方法(转)
- iOS讲解迷惑深入浅出之复杂对象的归档
- HeapInspector-for-iOS——iOS应用调试工具
- iOS讲解迷惑深入浅出之单例类的两种写法
- [iOS]如何在iOS开发中切换显示语言实现国际化
- ios开发系列之排序算法
- 使用XMPPFrameworke遇到的问题
- IOS- CALayer