I am trying to merge two PDF files in iOS. I am merging page-by-page. It takes a lot of time in case of very big files. Is there any way, we can just merge the two files,
Use this method for any number of pdfs this may help u
+ (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;
}