Documentation Archive Developer
Search

ADC Home > Reference Library > Technical Q&As > Carbon > Printing >

Not Recommended Documentclose button

Important: The information in this document is Not Recommended and should not be used for new development.

Current information on this Reference Library topic can be found here:

How do I use PMSessionGetGraphicsContext to get a CGContextRef?


Q: How do I use PMSessionGetGraphicsContext to get a CGContextRef?

A: As documented in the Carbon Printing Manager Reference for the PMSessionSetDocumentFormatGeneration API, you must call PMSessionSetDocumentFormatGeneration with the kPMGraphicsContextCoreGraphics context type before you call PMSessionBeginDocument and then call PMSessionBeginPage in order for PMSessionGetGraphicsContext to return a CGContextRef. Keep in mind that the coordinate system for a CGContextRef (origin in the lower left corner of page and not the imageable area, +y up) is not the same as for a QuickDraw port (origin in upper left, +y down). Also, note that even though these APIs are available in CarbonLib 1.1 and later, Quartz 2D didn't exist before Mac OS X 10.0 and so requesting kPMGraphicsContextCoreGraphics doesn't make sense if you aren't running on Mac OS X. Listing 1 below is a modified version of the code found in the Carbon Printing Manager Reference section on PMSessionSetDocumentFormatGeneration.



Listing 1. Getting a CGContextRef for printing

    CFStringRef         strings[1];
    CFArrayRef          ourGraphicsContextsArray;
    CGContextRef        printingContext;
    OSErr               err = noErr;
    PMPrintSession      printSession;
    
    //
    //    at this point you've already created a print session
    //
    strings[0] = kPMGraphicsContextCoreGraphics; // This is important!
    ourGraphicsContextsArray = CFArrayCreate (kCFAllocatorDefault,
                        (const void **)strings,
                        1, &kCFTypeArrayCallBacks);
    if (ourGraphicsContextsArray != NULL)
    {
            err = PMSessionSetDocumentFormatGeneration (printSession,
                            kPMDocumentFormatPDF,
                            ourGraphicsContextsArray, NULL);
            CFRelease (ourGraphicsContextsArray);
    }
    
    //
    //    more of your print loop
    //
    
    //
    //    then you call PMSessionBeginDocument and PMSessionBeginPage
    //
    
    //    Now you are ready to request the printing context
    err = PMSessionGetGraphicsContext (printSession,
        kPMGraphicsContextCoreGraphics, (void **) &printingContext);
        
    //
    //    render your content to the printingContext using Quartz 
    //    2D and continue your print loop
    //


Both the PMSessionSetDocumentFormatGeneration and PMSessionGetGraphicsContext APIs can be found in the "PMCore.h" header file while the kPMGraphicsContextCoreGraphics constant can be found in the "PMDefinitions.h" header file.


[Feb 25 2003]