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

< Previous PageNext Page > Hide TOC

Creating a PDF File

It’s as easy to create a PDF file using Quartz 2D as it is to draw to any graphics context. You specify a location for a PDF file, set up a PDF graphics context, and use the same drawing routine you’d use for any graphics context. The function MyCreatePDFFile, shown in Listing 13-4, shows all the tasks your code performs to create a PDF. A detailed explanation for each numbered line of code appears following the listing.

Note that the code delineates PDF pages by calling the functions CGContextBeginPage and CGContextEndPage. In Mac OS X v10.4 and later, you should instead use the functions CGPDFContextBeginPage and CGPDFContextEndPage. One of the advantages of the newer functions is that you can pass a CFDictionary to specify page properties including the media, crop bleed, trim, and art boxes.

For a list of dictionary key constants and a more detailed description of each, see CGPDFContext Reference.

Listing 13-4  A function that creates a PDF

void MyCreatePDFFile (CGRect pageRect, const char *filename)// 1
{
    CGContextRef pdfContext;
    CFStringRef path;
    CFURLRef url;
    CFMutableDictionaryRef myDictionary = NULL;
 
    path = CFStringCreateWithCString (NULL, filename, // 2
                                kCFStringEncodingUTF8);
    url = CFURLCreateWithFileSystemPath (NULL, path, // 3
                     kCFURLPOSIXPathStyle, 0);
    CFRelease (path);
    myDictionary = CFDictionaryCreateMutable(NULL, 0,
                        &kCFTypeDictionaryKeyCallBacks,
                        &kCFTypeDictionaryValueCallBacks); // 4
    CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
    CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
    pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary); // 5
    CFRelease(myDictionary);
    CFRelease(url);
    CGContextBeginPage (pdfContext, &pageRect); // 6
    myDrawContent (pdfContext);// 7
    CGContextEndPage (pdfContext);// 8
    CGContextRelease (pdfContext);// 9
}

Here’s what the code does:

  1. Takes as parameters a rectangle that specifies the size of the PDF page and a string that specifies the filename.

  2. Creates a CFString object from a filename passed to the function MyCreatePDFFile.

  3. Creates a CFURL object from the CFString object.

  4. Creates an empty CFDictionary object to hold metadata. The next two lines add a title and creator. You can add as many key-value pairs as you’d like using the function CFDictionarySetValue. For more information on creating dictionaries, see CFDictionary Reference.

  5. Creates a PDF graphics context, passing three parameters:

    • A CFURL object that specifies a location for the PDF data.

    • A pointer to a rectangle that defines the default size and location of the PDF page. The origin of the rectangle is typically (0, 0). Quartz uses this rectangle as the default bounds of the page media box. If you pass NULL, Quartz uses a default page size of 8.5 by 11 inches (612 by 792 points).

    • A CFDictionary object that contains PDF metadata. Pass NULL if you don’t have metadata to add.

      In Mac OS X v10.4, you can use the CFDictionary object to specify output intent options—intent subtype, condition, condition identifier, registry name, destination output profile, and a human-readable text string that contains additional information or comments about the intended target device or production condition. For more information about output intent options, see CGPDFContext Reference.

  6. Signals the start of a page. When you use a graphics context that supports multiple pages (such as PDF), you call the function CGContextBeginPage together with CGContextEndPage to delineate the page boundaries in the output. Each page must be bracketed by calls to CGContextBeginPage and CGContextEndPage. Quartz ignores all drawing operations performed outside a page boundary in a page-based context.

    In Mac OS X v10.4 and later, you should instead use the function CGPDFContextBeginPage, supplying the graphics context and a CFDictionary that contains key-value pairs to define the page properties.

  7. Calls an application-defined function to draw content to the PDF context. You supply your drawing routine here.

  8. Signals the end of a page in a page-based graphics context.

    In Mac OS X v10.4 and later, if you previously called CGPDFContextBeginPage, then you must use the function CGPDFContextEndPage to signal the end of a page.

  9. Releases the PDF context.



< 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