Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1100855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:56:50+00:00 2026-05-17T00:56:50+00:00

I am trying to draw a pdf page onto a pdf context and then

  • 0

I am trying to draw a pdf page onto a pdf context and then save it to disk. I cannot seem to figure out what is wrong. Can someone give me a few pointers. Thanks.

- (void)testQuartz:(NSData *)pdfDocumentData
{   
    //Create the pdf document reference
    CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData((CFDataRef)pdfDocumentData);
    CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(dataProvider);
    CGDataProviderRelease(dataProvider); //Release the data provider

    //Create the pdf context
    CGPDFPageRef page = CGPDFDocumentGetPage(document, 0);
    CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
    CFMutableDataRef mutableData = CFDataCreateMutable(NULL, 0);

    CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData(mutableData);
    CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, &pageRect, NULL);

    if (CGPDFDocumentGetNumberOfPages(document) > 0)
    {       
        //Draw the page onto the new context
        page = CGPDFDocumentGetPage(document, 0);
        CGContextDrawPDFPage(pdfContext, page);
    }
    else
    {
        NSLog(@"Failed to create the document");
    }

    //Write to disk
    [(NSMutableData *)mutableData writeToFile:@"/Users/David/Desktop/test.pdf" atomically:YES];

    //Clean up
    CGDataConsumerRelease(dataConsumer);
    CFRelease(mutableData);
    CGPDFDocumentRelease(document);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:56:51+00:00Added an answer on May 17, 2026 at 12:56 am

    So I found 3 things wrong with it.

    1. PDF pages start at 1 and not 0.
    2. Need to tell the pdf context when a page begins and ends.
    3. Release the pdf context prior to writing the data to disk. This is key for some reason.

    Here is the corrected code that works.

    - (void)testQuartz:(NSData *)pdfDocumentData
    {       
        //Create the pdf document reference
        CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData((CFDataRef)pdfDocumentData);
        CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(dataProvider);
    
        //Create the pdf context
        CGPDFPageRef page = CGPDFDocumentGetPage(document, 1); //Pages are numbered starting at 1
        CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
        CFMutableDataRef mutableData = CFDataCreateMutable(NULL, 0);
    
        //NSLog(@"w:%2.2f, h:%2.2f",pageRect.size.width, pageRect.size.height);
        CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData(mutableData);
        CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, &pageRect, NULL);
    
    
        if (CGPDFDocumentGetNumberOfPages(document) > 0)
        {       
            //Draw the page onto the new context
            //page = CGPDFDocumentGetPage(document, 1); //Pages are numbered starting at 1
    
            CGPDFContextBeginPage(pdfContext, NULL);
            CGContextDrawPDFPage(pdfContext, page);
            CGPDFContextEndPage(pdfContext);
        }
        else
        {
            NSLog(@"Failed to create the document");
        }
    
        CGContextRelease(pdfContext); //Release before writing data to disk.
    
        //Write to disk
        [(NSData *)mutableData writeToFile:@"/Users/David/Desktop/test.pdf" atomically:YES];
    
        //Clean up
        CGDataProviderRelease(dataProvider); //Release the data provider
        CGDataConsumerRelease(dataConsumer);
        CGPDFDocumentRelease(document);
        CFRelease(mutableData);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to draw the pages of a PDF using the code below. Some
I'm trying to draw PNGs onto BitmapData that is transparent. I create my BitmapData
I'm trying to understand how CATiledLayer works when rendering a PDF page using CGContextDrawPDFPage()
I'm trying to draw QR barcodes in a PDF file using iTextSharp. If I'm
I am trying to draw a line using the Graphics 2D but then the
I am trying to draw a figure something like this: I need to have
I'm trying to draw a nine patch onto a Canvas object on the Android.
Does CGContextMoveToPoint work with CGContextShowText? I'm trying to draw to a PDF. Without any
I am currently trying to draw a polygon and then fill it with a
I am trying to draw with brush on UserControl control. I can draw lines,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.