PDF を描画する別のメカニズムは、CGPDF* 関数を使用することです。これを行うには、 を使用CGPDFDocumentCreateWithURLしてオブジェクトを作成しCGPDFDocumentRefます。次に、を使用CGPDFDocumentGetPageしてオブジェクトを取得しCGPDFPageRefます。その後、 を使用CGContextDrawPDFPageしてページをグラフィックス コンテキストに描画できます。
ドキュメントが希望どおりのサイズになるように、変換を適用する必要がある場合があります。CGAffineTransformこれを行うには、とを使用CGContextConcatCTMします。
これは、私のプロジェクトの 1 つから引き出されたサンプル コードです。
// use your own constants here
NSString *path = @"/path/to/my.pdf";
NSUInteger pageNumber = 14;
CGSize size = [self frame].size;
// if we're drawing into an NSView, then we need to get the current graphics context
CGContextRef context = (CGContextRef)([[NSGraphicsContext currentContext] graphicsPort]);
CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)path, kCFURLPOSIXPathStyle, NO);
CGPDFDocumentRef document = CGPDFDocumentCreateWithURL(url);
CGPDFPageRef page = CGPDFDocumentGetPage(document, pageNumber);
// in my case, I wanted the PDF page to fill in the view
// so we apply a scaling transform to fir the page into the view
double ratio = size.width / CGPDFPageGetBoxRect(page, kCGPDFTrimBox).size.width;
CGAffineTransform transform = CGAffineTransformMakeScale(ratio, ratio);
CGContextConcatCTM(context, transform);
// now we draw the PDF into the context
CGContextDrawPDFPage(context, page);
// don't forget memory management!
CGPDFDocumentRelease(document);