Question

I am merging 2 PDF's using https://stackoverflow.com/a/15431035/2745211. Now I need to add a page number at the end. I have tried following methods in https://developer.apple.com/library/ios/documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GeneratingPDF/GeneratingPDF.html but it did not help.

Any ideas/ sample code would be of great help.

Was it helpful?

Solution

I managed to print the PDF pages:

 // Number of pages
NSInteger numberOfPages1 = CGPDFDocumentGetNumberOfPages(pdfRef1);
NSInteger numberOfPages2 = CGPDFDocumentGetNumberOfPages(pdfRef2);
int totalNumberofPages = numberOfPages1 + numberOfPages2;
// 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++) {
     UIImage *pageNoImage = [self drawPageNumber:[NSString stringWithFormat:@"Page %d of %d",i,totalNumberofPages]];
    page = CGPDFDocumentGetPage(pdfRef1, i);
    mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
    CGContextBeginPage(writeContext, &mediaBox);
    CGContextDrawPDFPage(writeContext, page);
    CGContextDrawImage(writeContext, CGRectMake (490, 70, 80, 20 ), pageNoImage.CGImage);
    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++) {
      UIImage *pageNoImage = [self drawPageNumber:[NSString stringWithFormat:@"Page %d of %d",(i+numberOfPages1),totalNumberofPages]];
    page = CGPDFDocumentGetPage(pdfRef2, i);
    mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
    CGContextBeginPage(writeContext, &mediaBox);
    CGContextDrawPDFPage(writeContext, page);
    CGContextDrawImage(writeContext, CGRectMake (490, 70, 80, 20 ), pageNoImage.CGImage);
    CGContextEndPage(writeContext);
}
NSLog(@"DONE!");

// Finalize the output file
CGPDFContextClose(writeContext);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top