• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Drawing to a PDF Context
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Drawing to a PDF Context


  • Subject: Re: Drawing to a PDF Context
  • From: Antonio Nunes <email@hidden>
  • Date: Wed, 28 Mar 2007 07:20:33 +0100


On Mar 28, 2007, at 7:17 AM, Antonio Nunes wrote:

On Mar 27, 2007, at 10:59 PM, Fabio Mancinelli wrote:

I have loaded a PDF and I have a CGPDFDocumentRef.
Now I want to draw a rectangle on a page of this document.
[…]
The CGPDFContextCreate requires a CGDataConsumerRef.

I suppose that I have to create such a consumer by using CGDataConsumerCreateWithCFData because the CGDataConsumerCreate requires that I provide callbacks to put bytes and, obviously, I don't know how to do it in the PDF format.

So now I need a CFMutableDataRef representing the PDF page and pass it to CGDataConsumerCreateWithCFData.

And here I am stuck... How do I retrieve a CFMutableDataRef from a CGPDFPageRef for creating a data consumer?

Hi Fabio,

You do not create CFMutableDataRef from a CGPDFPageRef. The consumer will be the destination of the data from the Quartz drawing functions. You draw the CGPDFPageRef into the PDF context created with the consumer. Here is a minimal example:

CGRect mediaBox = CGRectMake(0, 0, width, height);
// Create data object with default allocator and 'unlimited' size
CFMutableDataRef data = CFDataCreateMutable(NULL, 0);
// Create consumer that writes to the data object
CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData(data);
// Create minimal PDF context with the consumer to receive the PDF output data
CGContextRef pdfContext = CGPDFContextCreate(consumer, &mediaBox, NULL);
CGDataConsumerRelease(consumer); // The context retains the consumer, so we can safely release here

// Start a page in the context
CGPDFContextBeginPage(pdfContext, NULL);


	// Draw the page
	CGContextDrawPDFPage(pdfContext, yourCGPDFPageRef);
	// Draw the rectangle
	…

	// Finalise the page
	CGPDFContextEndPage(pdfContext);

	// Finalise the PDF context
	CGContextRelease(pdfContext);

After finalising the context the CFMutableDataRef holds the data for the PDF document. So you could do something like:

	PDFDocument *doc = [[PDFDocument alloc] initWithData:(NSData *)data];
	CFRelease(data);

I haven't tested this code as typed, but it shouldn't be far from the truth.

Salut,
António

----------------------------------------------------
A merry heart does good like medicine
----------------------------------------------------



----------------------------------------------------------- And could you keep your heart in wonder at the daily miracles of your life, your pain would not seem less wondrous than your joy.

--Kahlil Gibran
-----------------------------------------------------------



_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Drawing to a PDF Context (From: Fabio Mancinelli <email@hidden>)

  • Prev by Date: Re: Simple Cocoa bindings question
  • Next by Date: [Q] NSTextView Binding property
  • Previous by thread: Drawing to a PDF Context
  • Next by thread: Re: Drawing to a PDF Context
  • Index(es):
    • Date
    • Thread