Drawing to a PDF image representation
Drawing to a PDF image representation
- Subject: Drawing to a PDF image representation
- From: "ad veloper" <email@hidden>
- Date: Mon, 25 Feb 2008 22:42:05 +0000
List,
How do I draw NSBezierPaths to my pdf representation?
This method only seems to capture the last NSBezierPath in the saved .pdf,
and none of the previous ones, even though they are successfully being drawn
to the image using the [image lockFocus] method.
Here's some of the implementation file:
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
_pdfRep = [[NSPDFImageRep imageRepWithContentsOfFile:
@"/path/to/file.pdf"] retain];
image = [[NSImage alloc] initWithSize:[_pdfRep size]];
[image setDataRetained:YES]; // ensure vector data is kept
[image addRepresentation:_pdfRep];
}
return self;
}
- (void)drawRect:(NSRect)aRect
{
[[NSColor grayColor] set];
NSRectFill( aRect );
// other code here that sets viewSize etc. has been removed for
brevity
// if saving, get the best, i.e. pdf representation, otherwise
redraw the cached image.
if (needToUseRealPDFData == YES) {
needToUseRealPDFData = NO;
[[image bestRepresentationForDevice:nil] drawInRect:NSMakeRect(0.0,
0.0, viewSize.width, viewSize.height)];
// [_pdfRep drawInRect:NSMakeRect(0.0,0.0, viewSize.width, viewSize.height
)];
} else
[image drawInRect:NSMakeRect(0.0,0.0,viewSize.width,viewSize.height)
fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
// when saving the .pdf only the last line appears in the file
[[NSColor redColor] set];
[NSBezierPath strokeLineFromPoint:previousLocation toPoint:location];
// add a path for the updated mouse positions
[image lockFocus];
[[NSColor redColor] set];
[NSBezierPath strokeLineFromPoint:previousLocation toPoint:location];
[image unlockFocus];
}
- (void)keyDown:(NSEvent *)theEvent
{
NSString *keyChar = [theEvent characters];
if ( [keyChar isEqualToString:@"s"] ) {
needToUseRealPDFData = YES;
NSRect r = [self bounds];
NSData *data = [self dataWithPDFInsideRect:r];
[data writeToFile:@"/path/to/test.pdf" atomically:NO];
}
}
Thanks,
_______________________________________________
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