Re: dataWithPDFInsideRect and NSPrintOperation
Re: dataWithPDFInsideRect and NSPrintOperation
- Subject: Re: dataWithPDFInsideRect and NSPrintOperation
- From: Graham Cox <email@hidden>
- Date: Fri, 11 Jun 2010 22:36:38 +1000
On 11/06/2010, at 3:06 AM, Gideon King wrote:
> Hi, I have an offscreen view that I want to get the PDF data from. I use the dataWithPDFInsideRect method, but I have found that this creates an NSPrintOperation. This is a problem because I want to print using different settings for this view only when a *real* print operation is happening. I detect this in my drawing code like this:
>
> NSPrintOperation *printOperation = [NSPrintOperation currentOperation];
> if (printOperation && ![[NSGraphicsContext currentContext] isDrawingToScreen]) {
> [self doSpecialStuffForPrinting];
> }
>
> Now the problem is that my special printing stuff is being called when I use dataWithPDFInsideRect.
>
> I see there is an -isCopyingOperation method which can tell me whether it is sending to PDF at the time, but that is still not going to differentiate between the print to PDF from the print panel, and the dataWithPDFInsideRect.
>
> Is there any way of telling for sure whether it is a call to dataWithPDFInsideRect that caused the drawing to happen? If not, I guess I'll have to set up some sort of status variable, but I'd rather not if I don't have to.
I don't know the direct answer to your question, but there are alternative ways to get PDF data out of a view, as a I put together some code to do almost exactly that last week.
Essentially you create a PDF graphics context and draw into that. -isDrawingToScreen should still return NO but AFAICT no print operation comes into play.
My code looks like this:
NSSize size = [self bounds].size;
NSRect destRect = NSZeroRect;
destRect.size = size;
NSMutableData* pdfData = [NSMutableData data];
CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData((CFMutableDataRef) pdfData );
CGRect mediaBox = CGRectMake( 0, 0, size.width, size.height );
CGContextRef pdfContext = CGPDFContextCreate( consumer, &mediaBox, NULL );
CGDataConsumerRelease( consumer );
NSAssert( pdfContext != NULL, @"could not create PDF context");
NSGraphicsContext* newGC = [NSGraphicsContext graphicsContextWithGraphicsPort:pdfContext flipped:YES];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:newGC];
CGPDFContextBeginPage( pdfContext, NULL );
// here you do your drawing. In my case I flip the context and call a method that draws an object into the current context.
// for a general purpose method operating on a view I guess you could just call -drawRect: here having set a suitable CTM (or
// perhaps lockFocus/unlockFocus will handle that).
CGContextTranslateCTM( pdfContext, 0.0, size.height );
CGContextScaleCTM( pdfContext, 1.0, -1.0 );
[self drawContentInRect:destRect fromRect:NSZeroRect withStyle:nil];
// end of drawing
CGPDFContextEndPage( pdfContext );
[NSGraphicsContext restoreGraphicsState];
CGPDFContextClose( pdfContext );
CGContextRelease( pdfContext );
return pdfData;
--Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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