在合并的PDF中添加页码

时间:2014-02-11 10:07:00

标签: ios pdf-generation core-text

我正在使用https://stackoverflow.com/a/15431035/2745211合并2个PDF。 现在我需要在最后添加页码。 我在https://developer.apple.com/library/ios/documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GeneratingPDF/GeneratingPDF.html中尝试了以下方法,但没有帮助。

任何想法/示例代码都会有很大的帮助。

1 个答案:

答案 0 :(得分:1)

我设法打印PDF页面:

 // 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);