For detailed information about PDF, including thePDF language and syntax, see PDF Reference, Fourth Edition, Version 1.5.
CGPDFXXXXX
Quartz creates high-quality PDF documents
Quartz not only uses PDF as its “digital paper” but also includes as part of its API a number of functions that you can use to display and generate PDF files and to accomplish a number of other PDF-related tasks.
CGPDFDocumentRef MyGetPDFDocumentRef (const char *filename) |
{ |
CFStringRef path; |
CFURLRef url; |
CGPDFDocumentRef document; |
size_t count; |
|
path = CFStringCreateWithCString (NULL, filename, |
kCFStringEncodingUTF8); |
url = CFURLCreateWithFileSystemPath (NULL, path, // 1 |
kCFURLPOSIXPathStyle, 0); |
CFRelease (path); |
document = CGPDFDocumentCreateWithURL (url);// 2 |
CFRelease(url); |
count = CGPDFDocumentGetNumberOfPages (document);// 3 |
if (count == 0) { |
printf("`%s' needs at least one page!", filename); |
return NULL; |
} |
return document; |
} |
Here’s what the code does:
-
Calls the Core Foundation function to create a CFURL object from a CFString object that represents the filename of the PDF file to display.
-
Creates a CGPDFDocument object from a CFURL object.
-
Gets the number of pages in the PDF so that the next statement in the code can ensure that the document has at least one page.
You can see how to draw a PDF page to a graphics context by looking at the code in Listing 13-2. A detailed explanation for each numbered line of code appears following the listing.
Listing 13-2 Drawing a PDF page
void MyDisplayPDFPage (CGContextRef myContext, |
size_t pageNumber, |
const char *filename) |
{ |
CGPDFDocumentRef document; |
CGPDFPageRef page; |
|
document = MyGetPDFDocumentRef (filename);// 1 |
page = CGPDFDocumentGetPage (document, pageNumber);// 2 |
CGContextDrawPDFPage (myContext, page);// 3 |
CGPDFDocumentRelease (document);// 4 |
} |
Here’s what the code does:
-
Calls your function (see Listing 13-1) to create a CGPDFDocument object from a filename you supply.
-
Gets the page for the specified page number from the PDF document.
-
Draws the specified page from the PDF file by calling the function
CGContextDrawPDFPage
. You need to supply a graphics context and the page to draw. -
Releases the CGPDFDocument object.
CGPDFPageGetDrawingTransform
—that creates an affine transform by mapping a box in a PDF page to a rectangle you specify.
Adding Links
You can add links and anchors to PDF context you create. Quartz provides three functions, each of which takes a PDF graphics context as a parameter, along with information about the links:
-
CGPDFContextSetURLForRect
lets you specify a URL to open when the user clicks a rectangle in the current PDF page. -
CGPDFContextSetDestinationForRect
lets you set a destination to jump to when the user clicks a rectangle in the current PDF page. You must supply a destination name. -
CGPDFContextAddDestinationAtPoint
lets you set a destination to jump to when the user clicks a point in the current PDF page. You must supply a destination name.