PDF를 그리는 또 다른 메커니즘은 CGPDF * 기능을 사용하는 것입니다. 이렇게하려면 CGPDFDocumentCreateWithURL
을 사용하여 CGPDFDocumentRef
개체를 만듭니다. 그런 다음 CGPDFDocumentGetPage
을 사용하여 CGPDFPageRef
개체를 가져옵니다. 그런 다음 CGContextDrawPDFPage
을 사용하여 그래픽 컨텍스트에 페이지를 그릴 수 있습니다.
변환을 적용하여 문서의 크기가 원하는대로 끝나야 할 수도 있습니다. 이 작업을 수행하려면 CGAffineTransform
및 CGContextConcatCTM
을 사용하십시오.
// 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);
: 여기
는 일부 샘플 코드는 내 프로젝트 중 하나 뽑아입니다