2013-03-15 1 views
4

Я пытаюсь объединить два файла PDF в iOS. Я объединяюсь по страницам. Это занимает много времени в случае очень больших файлов. Есть ли способ, мы можем просто объединить два файла, а не по страницам?Объединить 2 pdf-файла в iOS

Я могу выслать свой код, если хотите.

Мой код similat к одному Here on SO

+0

и может следовать за пост я даю если и знает путь к файлам PDF – 08442

ответ

11

Попробуйте code..It складывает 1-ый файл в формате PDF, то на нем этого, он добавляет 2-й файл в формате 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); 
+0

Идеальные! Спасибо. Позор его никогда не отмечался как правильный ответ. – Nathan

+0

Не помечено как ответ. @Nathan, потому что вы никогда не получаете награду за копию за последние http://stackoverflow.com/questions/13448446/add-pdf-page-to-an-existing-pdf-objective-c –

+0

Я изменил этот код для работы с NSData вместо этого работы с файлами. Работает как шарм – TMob

4

Используйте этот метод для любого количества PDFs это может помочь у

+ (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; 
} 

 Смежные вопросы

  • Нет связанных вопросов^_^