Drawing to a PDF image representation

  • 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,
  • > How do I draw NSBezierPaths to my pdf representation?

      Give this a thorough read.

    http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaDrawingGuide
    /Introduction/chapter_1_section_1.html


    It answers your question completely, though you'll need to read most
    of (if not all) of this guide to understand what you need to do and why.

    --
    I.S.
previous month february 2008 next month
MTWTFSS
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29    
Go to today
MindNode
MindNode offered a free license !