Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Creating a PDF Graphics Context

When you create a PDF graphics context and draw to that context, Quartz records your drawing as a series of PDF drawing commands written to a file. You supply a location for the PDF output and a default media box—a rectangle that specifies bounds of the page. Figure 2-6 shows the result of drawing to a PDF graphics context and then opening the resulting PDF in Preview.


Figure 2-6  A PDF created by using CGPDFContextCreateWithURL

A PDF created by using CGPDFContextCreateWithURL

The Quartz 2D API provides two functions that create a PDF graphics context:

A detailed explanation for each numbered line of code follows each listing.

Listing 2-5  A routine that calls CGPDFContextCreateWithURL to create a PDF graphics context

CGContextRef MyCreatePDFContext (const CGRect *inMediaBox,
                                    CFStringRef path)
{
    CGContextRef myOutContext = NULL;
    CFURLRef url;
 
    url = CFURLCreateWithFileSystemPath (NULL, // 1
                                path,
                                kCFURLPOSIXPathStyle,
                                false);
    if (url != NULL) {
        myOutContext = CGPDFContextCreateWithURL (url,// 2
                                        inMediaBox,
                                        NULL);
        CFRelease(url);// 3
    }
    return myOutContext;// 4
}

Here’s what the code does:

  1. Calls the Core Foundation function to create a CFURL object from the CFString object supplied to the MyPDFContextCreate function. You pass NULL as the first parameter to use the default allocator. You also need to specify a path style, which for this example is a POSIX-style pathname.

  2. Calls the Quartz 2D function to create a PDF graphics context using the PDF location just created (as a CFURL object) and a rectangle that specifies the bounds of the PDF. The rectangle (a CGRect) was passed to the MyPDFContextCreate function and is the default page media bounding box for the PDF.

  3. Releases the CFURL object.

  4. Returns the PDF graphics context. The caller must release the graphics context when it is no longer needed.

Listing 2-6  A routine that calls CGPDFContextCreate to create a PDF graphics context

CGContextRef MyCreatePDFContext (const CGRect *inMediaBox,
                                    CFStringRef path)
{
    CGContextRef        myOutContext = NULL;
    CFURLRef            url;
    CGDataConsumerRef   dataConsumer;
 
    url = CFURLCreateWithFileSystemPath (NULL, // 1
                                        path,
                                        kCFURLPOSIXPathStyle,
                                        false);
 
    if (url != NULL)
    {
        dataConsumer = CGDataConsumerCreateWithURL (url);// 2
        if (dataConsumer != NULL)
        {
            myOutContext = CGPDFContextCreate (dataConsumer, // 3
                                        inMediaBox,
                                        NULL);
            CGDataConsumerRelease (dataConsumer);// 4
        }
        CFRelease(url);// 5
    }
    return myOutContext;// 6
}

Here’s what the code does:

  1. Calls the Core Foundation function to create a CFURL object from the CFString object supplied to the MyPDFContextCreate function. You pass NULL as the first parameter to use the default allocator. You also need to specify a path style, which for this example is a POSIX-style path name.

  2. Creates a Quartz data consumer object using the CFURL object. If you don’t want to use a CFURL object (for example, you want to place the PDF data in a location that can’t be specified by a CFURL object), you can instead create a data consumer from a set of callback functions that you implement in your application. For more information, see “Data Management.”

  3. Calls the Quartz 2D function to create a PDF graphics context passing as parameters the data consumer and the rectangle (of type CGRect) that was passed to the MyPDFContextCreate function. This rectangle is the default page media bounding box for the PDF.

  4. Releases the data consumer.

  5. Releases the CFURL object.

  6. Returns the PDF graphics context. The caller must release the graphics context when it is no longer needed.

Listing 2-7 shows how to call the MyCreatePDFContext routine and draw to it. A detailed explanation for each numbered line of code appears following the listing.

Listing 2-7  Code that draws to a PDF graphics context

    CGRect mediaBox;// 1
 
    mediaBox = CGRectMake (0, 0, myPageWidth, myPageHeight);// 2
    myPDFContext = MyCreatePDFContext (&mediaBox, CFSTR("test.pdf"));// 3
    CGContextBeginPage(myPDFContext, &mediaBox);// 4
        // ********** Your drawing code here **********// 5
        CGContextSetRGBFillColor (myPDFContext, 1, 0, 0, 1);
        CGContextFillRect (myPDFContext, CGRectMake (0, 0, 200, 100 ));
        CGContextSetRGBFillColor (myPDFContext, 0, 0, 1, .5);
        CGContextFillRect (myPDFContext, CGRectMake (0, 0, 100, 200 ));
    CGContextEndPage(myPDFContext);// 6
    CGContextRelease(myPDFContext);// 7

Here’s what the code does:

  1. Declares a variable for the rectangle that you use to define the PDF media box.

  2. Sets the origin of the media box to (0,0) and the width and height to variables supplied by the application.

  3. Calls the function MyCreatePDFContext (See Listing 2-6) to obtain a PDF graphics context, supplying a media box and a path name. The macro CFSTR converts a string to a CFStringRef data type.

  4. Signals the start of a page. This function is used for page-paged graphics, which is what PDF drawing is. This example passes the media box to define the page boundary. You don’t have to pass the same rectangle you used to set up the PDF graphics context. The rectangle you pass to CGContextBeginPage supersedes the rectangle you pass to set up the PDF graphics context.

    Note:  Starting in Mac OS X v10.4, you can call the function CGPDFContextBeginPage and its companion CGPDFContextEndPage to delineate PDF pages. You can provide a CFDictionary to the function CGPDFContextBeginPage to specify page properties.

  5. Calls Quartz 2D drawing functions. You replace this and the following four lines of code with the drawing code appropriate for your application.

  6. Signals the end of the PDF page.

  7. Releases the PDF graphics context when it is no longer needed.

You can write any content to a PDF that’s appropriate for your application—images, text, path drawing—and you can add links and encryption. For more information see “PDF Document Creation, Viewing, and Transforming.”



< Previous PageNext Page > Hide TOC


Last updated: 2007-12-11




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice