-(void) createPDF
{
NSString *pdfFilePath = [NSString stringWithFormat:@"%s",filePath];
CGPDFDocumentRef document = CGPDFDocumentCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:pdfFilePath]);
NSUInteger totalPages = CGPDFDocumentGetNumberOfPages(document);
UIGraphicsBeginPDFContextToFile(pdfFilePath, CGRectZero, nil);
for (int i = 1; i <= totalPages; i++)
{
CGPDFPageRef pageRef = CGPDFDocumentGetPage(document, i);
CGRect cropBoxRect = CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox);
CGRect mediaBoxRect = CGPDFPageGetBoxRect(pageRef, kCGPDFMediaBox);
CGRect effectiveRect = CGRectIntersection(cropBoxRect, mediaBoxRect);
NSUInteger rotationAngle = 0;//;
int pageAngle = (CGPDFPageGetRotationAngle(pageRef) + rotationAngle) % 360;
NSInteger pageWidth = 0;
NSInteger pageHeight = 0;
switch (pageAngle) // Page rotation angle (in degrees)
{
default: // Default case
case 0: case 180: // 0 and 180 degrees
{
pageWidth = effectiveRect.size.width;
pageHeight = effectiveRect.size.height;
break;
}
case 90: case 270: // 90 and 270 degrees
{
pageWidth = effectiveRect.size.height;
pageHeight = effectiveRect.size.width;
break;
}
}
if (pageWidth % 2) pageWidth--;
if (pageHeight % 2) pageHeight--;
CGRect pageFrame = CGRectZero;
pageFrame.size = CGSizeMake(pageWidth, pageHeight);
UIGraphicsBeginPDFPageWithInfo(pageFrame, nil);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextScaleCTM(context, 1.0f, -1.0f);
CGContextTranslateCTM(context, 0.0f, -pageFrame.size.height);
CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(pageRef, kCGPDFCropBox, pageFrame, (int)rotationAngle, true));
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextSetRenderingIntent(context, kCGRenderingIntentDefault);
CGContextDrawPDFPage(context, pageRef);
CGContextRestoreGState(context);
CGContextTranslateCTM(context, pageWidth/2, pageHeight/2);
CGContextRotateCTM(context, degreesToRadians(rotationAngle));
CGFloat aspectRatio = (CGFloat)pageWidth/pageHeight;
if (rotationAngle == 90 || rotationAngle == 270)
{
CGContextTranslateCTM(context, - pageHeight/2, - pageWidth/2);
CGContextScaleCTM(context, 1/aspectRatio, aspectRatio);
}
else
{
CGContextTranslateCTM(context, - pageWidth/2, - pageHeight/2);
}
UIImage *pageViewImage = [self getPageImageAtIndex:i];
[pageViewImage drawInRect:pageFrame];
}
UIGraphicsEndPDFContext();
CGPDFDocumentRelease(document);
}
-(UIImage *) getPageImageAtIndex:(int) index
{
// Design page view and take snapshot of view with fixed number of questions and answers on view and return the snapshot image from here.
int startIndex = 0;
int endIndex = 0;
if(i==1)
{
startIndex = 0; // start from 0 and draw upto 10
endIndex = 10;
}
else if(i==2)
{
startIndex = 11; // start from 11 and draw upto 20
endIndex = 20;
}
else if(i==3)
{
startIndex = 21; // start from 21 and draw upto 30
endIndex = 30;
}
.
.
.
.
for(int i= startIndex; i<= endIndex;i++)
{
// Drawing goes here
}
}
使用上述功能和邏輯來完成您的工作...... –