I am trying to combine 11 pdf files into a single pdf file.The following code i am using ,but in the final pdf only the first pdf is shown ...i nslogged the pdfurls and CGPDFDocumentRef in the loop and they are not nil all the time(in the loop).What may be the reason why only the first page is displayed in the final document
-(void)mergeDocuments
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *oldFile=[documentsDirectory stringByAppendingPathComponent:@"finalPdf.pdf"];
NSMutableData *data=[[NSMutableData alloc] init];
CGRect paperSize=CGRectMake(0,0,kDefaultPageWidth,kDefaultPageHeight);
UIGraphicsBeginPDFContextToData(data, paperSize, nil);
for (int pageNumber = 1; pageNumber <= 11; pageNumber++)
{
NSString *pdfPath = [[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"page_%d.pdf",pageNumber]] retain];
NSURL *pdfUrl = [[NSURL fileURLWithPath:pdfPath] retain];
UIGraphicsBeginPDFPageWithInfo(paperSize, nil);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(currentContext, 0, paperSize.size.height);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGPDFDocumentRef newDocument = CGPDFDocumentCreateWithURL ((CFURLRef) pdfUrl);
CGPDFPageRef newPage = CGPDFDocumentGetPage (newDocument, pageNumber);
CGContextDrawPDFPage (currentContext, newPage);
newPage = nil;
CGPDFDocumentRelease(newDocument);
newDocument = nil;
[pdfUrl release];
}
NSURL *finalUrl=[NSURL URLWithString:oldFile];
UIGraphicsEndPDFContext();
[data writeToURL:finalUrl atomically:YES];
}