在iOS中合并2个pdf文件

在iOS中合并2个pdf文件,ios,objective-c,pdf,cgpdf,Ios,Objective C,Pdf,Cgpdf,我正在尝试在iOS中合并两个PDF文件。我正在一页一页地合并。如果文件很大,则需要花费大量时间。有没有办法,我们可以合并这两个文件,而不是一页一页地合并 如果你愿意,我可以发布我的代码 我的代码类似于一个试试下面的代码。它把第一个pdf文件加起来,然后在第二个pdf文件上加上一个pdf文件 NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) last

我正在尝试在iOS中合并两个PDF文件。我正在一页一页地合并。如果文件很大,则需要花费大量时间。有没有办法,我们可以合并这两个文件,而不是一页一页地合并

如果你愿意,我可以发布我的代码


我的代码类似于一个

试试下面的代码。它把第一个pdf文件加起来,然后在第二个pdf文件上加上一个pdf文件

NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

// File paths
NSString *pdfPath1 = [[NSBundle mainBundle] pathForResource:@"pdf1" ofType:@"pdf"];
NSString *pdfPath2 = [cacheDir stringByAppendingPathComponent:@"temp.pdf"];
NSString *pdfPathOutput = [cacheDir stringByAppendingPathComponent:@"out.pdf"];

// File URLs - bridge casting for ARC
CFURLRef pdfURL1 = (__bridge_retained CFURLRef)[[NSURL alloc] initFileURLWithPath:(NSString *)pdfPath1];//(CFURLRef) NSURL
CFURLRef pdfURL2 = (__bridge_retained CFURLRef)[[NSURL alloc] initFileURLWithPath:(NSString *)pdfPath2];//(CFURLRef)
CFURLRef pdfURLOutput =(__bridge_retained CFURLRef) [[NSURL alloc] initFileURLWithPath:(NSString *)pdfPathOutput];//(CFURLRef)

// File references
CGPDFDocumentRef pdfRef1 = CGPDFDocumentCreateWithURL((CFURLRef) pdfURL1);
CGPDFDocumentRef pdfRef2 = CGPDFDocumentCreateWithURL((CFURLRef) pdfURL2);

// Number of pages
NSInteger numberOfPages1 = CGPDFDocumentGetNumberOfPages(pdfRef1);
NSInteger numberOfPages2 = CGPDFDocumentGetNumberOfPages(pdfRef2);

// Create the output context
CGContextRef writeContext = CGPDFContextCreateWithURL(pdfURLOutput, NULL, NULL);

// Loop variables
CGPDFPageRef page;
CGRect mediaBox;

// Read the first PDF and generate the output pages
NSLog(@"GENERATING PAGES FROM PDF 1 (%i)...", numberOfPages1);
for (int i=1; i<=numberOfPages1; i++) {
    page = CGPDFDocumentGetPage(pdfRef1, i);
    mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
    CGContextBeginPage(writeContext, &mediaBox);
    CGContextDrawPDFPage(writeContext, page);
    CGContextEndPage(writeContext);
}

// Read the second PDF and generate the output pages
NSLog(@"GENERATING PAGES FROM PDF 2 (%i)...", numberOfPages2);
for (int i=1; i<=numberOfPages2; i++) {
    page = CGPDFDocumentGetPage(pdfRef2, i);
    mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
    CGContextBeginPage(writeContext, &mediaBox);
    CGContextDrawPDFPage(writeContext, page);
    CGContextEndPage(writeContext);      
}
NSLog(@"DONE!");

// Finalize the output file
CGPDFContextClose(writeContext);

// Release from memory
CFRelease(pdfURL1);
CFRelease(pdfURL2);
CFRelease(pdfURLOutput);
CGPDFDocumentRelease(pdfRef1);
CGPDFDocumentRelease(pdfRef2);
CGContextRelease(writeContext);
NSString*cacheDir=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)lastObject];
//文件路径
NSString*pdfPath1=[[NSBundle mainBundle]pathForResource:@“pdf1”类型:@“pdf”];
NSString*pdfPath2=[cacheDir stringByAppendingPathComponent:@“temp.pdf”];
NSString*pdfPathOutput=[cacheDir stringByAppendingPathComponent:@“out.pdf”];
//文件URL-弧的桥接转换
CFURLRef pdfURL1=(uu_桥_保留CFURLRef)[[NSURL alloc]initFileURLWithPath:(NSString*)pdfPath1]//(CFURLRef)NSURL
CFURLRef pdfURL2=(uu_桥_保留的CFURLRef)[[NSURL alloc]initFileURLWithPath:(NSString*)pdfPath2]//(CFURLRef)
CFURLRef Pdforloutput=(uu_桥_保留CFURLRef)[[NSURL alloc]initFileURLWithPath:(NSString*)pdfPathOutput]//(CFURLRef)
//文件引用
CGPDFDocumentRef pdfRef1=CGPDFDocumentCreateWithURL((CFURLRef)pdfURL1);
CGPDFDocumentRef pdfRef2=CGPDFDocumentCreateWithURL((CFURLRef)pdfURL2);
//页数
NSInteger numberOfPages1=CGPDFDocumentGetNumberOfPages(pdfRef1);
NSInteger numberOfPages2=CGPDFDocumentGetNumberOfPages(pdfRef2);
//创建输出上下文
CGContextRef writeContext=CGPDFContextCreateWithURL(pdfURLOutput,NULL,NULL);
//循环变量
CGPDFPageRef页面;
CGRect媒体盒;
//阅读第一份PDF并生成输出页面
NSLog(@“从PDF 1(%i)…生成页面”,numberOfPages1);

对于(int i=1;i对任意数量的PDF使用此方法,这可能有助于

+ (NSString *)joinPDF:(NSArray *)listOfPath{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *layOutPath=[NSString stringWithFormat:@"%@/mergepdf/pdf",[paths objectAtIndex:0]];
    if(![[NSFileManager defaultManager] fileExistsAtPath:layOutPath]){
        [[NSFileManager defaultManager] createDirectoryAtPath:layOutPath withIntermediateDirectories:YES attributes:nil error:nil];        
    } 
    // File paths
    NSString *fileName = @"ALL.pdf";
    NSString *pdfPathOutput = [layOutPath stringByAppendingPathComponent:fileName];
    CFURLRef pdfURLOutput = (__bridge CFURLRef)[NSURL fileURLWithPath:pdfPathOutput];
    NSInteger numberOfPages = 0;
    // Create the output context
    CGContextRef writeContext = CGPDFContextCreateWithURL(pdfURLOutput, NULL, NULL);

    for (NSString *source in listOfPath) {
//        CFURLRef pdfURL = (__bridge CFURLRef)[NSURL fileURLWithPath:source];

       CFURLRef pdfURL =  CFURLCreateFromFileSystemRepresentation(NULL, [source UTF8String],[source length], NO);

        //file ref
        CGPDFDocumentRef pdfRef = CGPDFDocumentCreateWithURL(pdfURL);
        numberOfPages = CGPDFDocumentGetNumberOfPages(pdfRef);

        // Loop variables
        CGPDFPageRef page;
        CGRect mediaBox;

        // Read the first PDF and generate the output pages
        DLog(@"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;
}
+(NSString*)joinPDF:(NSArray*)列表路径{
NSArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
NSString*layOutPath=[NSString stringWithFormat:@“%@/mergepdf/pdf”,[PATH objectAtIndex:0]];
如果(![[NSFileManager defaultManager]文件存在路径:layOutPath]){
[[NSFileManager defaultManager]createDirectoryAtPath:layOutPath with IntermediateDirectory:YES属性:nil错误:nil];
} 
//文件路径
NSString*fileName=@“ALL.pdf”;
NSString*pdfPathOutput=[layOutPath stringByAppendingPathComponent:fileName];
CFURLRef Pdforloutput=(u_桥CFURLRef)[NSURL fileURLWithPath:pdfPathOutput];
NSInteger numberOfPages=0;
//创建输出上下文
CGContextRef writeContext=CGPDFContextCreateWithURL(pdfURLOutput,NULL,NULL);
for(listOfPath中的NSString*源){
//CFURLRef pdfURL=(uu桥CFURLRef)[NSURL fileURLWithPath:source];
CFURLRef pdfURL=CFURLCreateFromFileSystemRepresentation(NULL,[源UTF8String],[源长度],否);
//文件引用
CGPDFDocumentRef pdfRef=CGPDFDocumentCreateWithURL(pdfURL);
numberOfPages=CGPDFDocumentGetNumberOfPages(pdfRef);
//循环变量
CGPDFPageRef页面;
CGRect媒体盒;
//阅读第一份PDF并生成输出页面
DLog(@“从PDF 1生成页面(%@)…”,来源);

for(int i=1;如果你知道pdf文件的路径,我可以跟随我的帖子完美!谢谢。可惜它从来没有被标记为正确答案。没有被标记为答案。@Nathan因为你从来没有因为复制过去而得到奖励我修改了这段代码来处理NSData,而不是处理文件。效果很好