Skip navigation.
 
mlDrawing to a PDF image representation
FROM : ad veloper
DATE : Mon Feb 25 23:42:05 2008

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,

Related mailsAuthorDate
mlDrawing to a PDF image representation ad veloper Feb 25, 23:42
mlRe: Drawing to a PDF image representation I. Savant Feb 26, 00:33