iOS合并pdf文件 [英] iOS merging pdf files

查看:66
本文介绍了iOS合并pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用程序中合并两个 pdf 文件.但是我的应用程序在创建组合文件引用时不断中断.(请参阅代码中的注释).有人可以在这里指出我正确的方向吗?谢谢.

I am trying to merge two pdf files in my app. But my app keeps breaking at the point where the combined file reference is created. (See comments in code). Can someone point me in the right direction here? Thanks.

- (void) mergerPDFFiles : (NSString*) firstPDFFile : (NSString* ) secondPDFFile {

    //set path to plist to read in docs directory
    NSString* pdf1Path = [documentsDirectory stringByAppendingPathComponent:firstPDFFile];
    NSString* pdf2Path = [documentsDirectory stringByAppendingPathComponent:secondPDFFile];

    BOOL hasError = NO;
    NSMutableString* strErrMsg = [[NSMutableString alloc] init];

    //check to see if the file exists
    if (![fileManager fileExistsAtPath:pdf1Path]) {
        hasError = YES;
        [strErrMsg appendString:[NSString stringWithFormat:@"Could not find the PDF just created: %@", firstPDFFile]];
    }

    //check to see if the file exists
    if (![fileManager fileExistsAtPath:pdf2Path]) {
        hasError = YES;
        [strErrMsg appendString:[NSString stringWithFormat:@"Could not find the marketing materials PDF: %@", secondPDFFile]];
    }

    if (!hasError) {

        NSArray* arrPDFPaths = [[NSArray alloc] initWithObjects:pdf1Path, pdf2Path, nil];

        //make a new file name
        NSArray* arrPDF1Split = [firstPDFFile componentsSeparatedByString:@"."];
        NSString* strNewPDF = [NSString stringWithFormat:@"%@_Complete", [arrPDF1Split objectAtIndex:0]];

        // File paths
        NSString* pdfPathOutput = [documentsDirectory stringByAppendingPathComponent:strNewPDF];


/******************DIES HERE!***********************************/
        CFURLRef pdfURLOutput = (__bridge CFURLRef)[[NSURL alloc] initFileURLWithPath:pdfPathOutput];
        NSInteger numberOfPages = 0;
        // Create the output context
        CGContextRef writeContext = CGPDFContextCreateWithURL(pdfURLOutput, NULL, NULL);

/********************ERROR MESSAGE******************************
 CGDataConsumerCreateWithFilename: failed to open `ers/msg-surans/Library/Application Support/iPhone Simulator/6.0/Applications/299DBD9D-738F-49E0-B593-159033DDE39B/Documents/OER_Pro_Cost_Test_Complete' for writing: No such file or directory.
    ****************************************************************/



        //loop through the pdfs
        for(int i=0; i<arrPDFPaths.count; i++) {

            //get the file
            NSString* pdfSource = [arrPDFPaths objectAtIndex:i];

            //set a CFURL to the pdf
            CFURLRef pdfURL = (__bridge CFURLRef)[[NSURL alloc] initFileURLWithPath:pdfSource];

            //set a reference to the pdf
            CGPDFDocumentRef pdfRef = CGPDFDocumentCreateWithURL((CFURLRef) pdfURL);
            numberOfPages = CGPDFDocumentGetNumberOfPages(pdfRef);

            //set some ivars
            CGPDFPageRef page;
            CGRect mediaBox;

            // Read the first PDF and generate the output pages
            NSLog(@"GENERATING PAGES FROM PDF (%@)...", pdfSource);
            for (int x=1; x<=numberOfPages; x++) {
                page = CGPDFDocumentGetPage(pdfRef, x);
                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);

    } else {

        UIAlertView *alert = [[UIAlertView alloc]
          initWithTitle: @"Error Merging PDF Files"
          message: strErrMsg
          delegate: nil
          cancelButtonTitle:@"OK"
          otherButtonTitles:
        nil];
        [alert show];
    }

}

推荐答案

我认为是您的 documentsDirectory 变量设置不正确.检查它是如何获得价值的.但是 ers/msg-surans/ 看起来不对.

I think it's your documentsDirectory variable that isn't set right. Check how it's getting its value. But ers/msg-surans/ doesn't look right.

应该是这样的:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentsDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; 

这篇关于iOS合并pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆