UIGraphics in Mac

Discussion in 'Mac Programming' started by Avicenna, Aug 13, 2011.

  1. Avicenna macrumors member

    Joined:
    Jul 16, 2009
    #1
    I am porting some code from iPhone to Mac and I have no idea how I can do this in Mac. Here's my code that I am trying to convert and I know that there's no UIGraphic in Mac. Can someone point me to a guide or give me a quick hint? Thanks.

    Code:
    NSString *newFilePath = @"path/to/your/newfile.pdf";
    NSString *templatePath = @"path/to/your/template.pdf";
    
    //create empty pdf file;
    UIGraphicsBeginPDFContextToFile(newFilePath, CGRectMake(0, 0, 792, 612), nil);
    
    CFURLRef url = CFURLCreateWithFileSystemPath (NULL, (CFStringRef)templatePath, kCFURLPOSIXPathStyle, 0);
    
    //open template file
    CGPDFDocumentRef templateDocument = CGPDFDocumentCreateWithURL(url);
    CFRelease(url);
    
    //get amount of pages in template
    size_t count = CGPDFDocumentGetNumberOfPages(templateDocument);
    
    //for each page in template
    for (size_t pageNumber = 1; pageNumber <= count; pageNumber++) {
        //get bounds of template page
        CGPDFPageRef templatePage = CGPDFDocumentGetPage(templateDocument, pageNumber);
        CGRect templatePageBounds = CGPDFPageGetBoxRect(templatePage, kCGPDFCropBox);
    
        //create empty page with corresponding bounds in new document
        UIGraphicsBeginPDFPageWithInfo(templatePageBounds, nil);
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        //flip context due to different origins
        CGContextTranslateCTM(context, 0.0, templatePageBounds.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
    
        //copy content of template page on the corresponding page in new file
        CGContextDrawPDFPage(context, templatePage);
    
        //flip context back
        CGContextTranslateCTM(context, 0.0, templatePageBounds.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
    
        /* Here you can do any drawings */
        [@"Test" drawAtPoint:CGPointMake(200, 300) withFont:[UIFont systemFontOfSize:20]];
    }
    CGPDFDocumentRelease(templateDocument);
    UIGraphicsEndPDFContext();
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    Check out the Quartz 2D Programming Guide, in particular the PDF Document Creation, Viewing, and Transforming chapter.

    Actually your code already used Quartz 2D's PDF functions to read the template PDF file. You just need to use Quartz 2D for creating the new PDF file as well.
     

Share This Page