PDF Viewer question #2
PDF Viewer question #2
- Subject: PDF Viewer question #2
- From: Hasan Diwan <email@hidden>
- Date: Mon, 10 Mar 2003 10:33:05 -0800
How do I get the data for a specific page in the NSPDFRep of a
multi-page PDF? Code below:
#import "MyImageView.h"
#import <Cocoa/Cocoa.h>
@interface MyImageView : NSView
{
NSScrollView *filePages;
NSImage *_image;
NSColor *_bgColor;
NSImageScaling _scaling;
}
- (void)setImage:(NSImage*)image;
- (void)setImageScaling:(NSImageScaling)newScaling;
- (void)setBackgroundColor:(NSColor*)color;
- (NSImage*)image;
- (NSColor*)backgroundColor;
- (NSImageScaling)imageScaling;
@end
@implementation MyImageView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
filePages = [[NSScrollView alloc] init];
_bgColor = [[NSColor clearColor] retain];
_scaling = NSScaleProportionally;
}
[self registerForDraggedTypes:[NSArray
arrayWithObjects:NSPostScriptPboardType, NSPICTPboardType,
NSPDFPboardType, NSFileContentsPboardType, NSTIFFPboardType,
NSFilenamesPboardType, nil]];
return self;
}
- (void)dealloc
{
[filePages release];
[_image release];
[_bgColor release];
[super dealloc];
}
- (void) setImage:(NSImage*)image
{
NSImage *tmp = [image retain];
[_image release];
_image = tmp;
[self setNeedsDisplay:YES];
}
- (NSImage*)image
{
return _image;
}
- (void)setImageScaling:(NSImageScaling)newScaling
{
_scaling = newScaling;
[self setNeedsDisplay:YES];
}
- (NSImageScaling)imageScaling
{
return _scaling;
}
- (void)setBackgroundColor:(NSColor*)color
{
NSColor *tmp = [color retain];
[_bgColor release];
_bgColor = tmp;
[self setNeedsDisplay:YES];
}
- (NSColor*)backgroundColor
{
return _bgColor;
}
- (void) drawRect:(NSRect)rects
{
NSRect bounds = [self bounds];
[_bgColor set];
[NSBezierPath fillRect:bounds];
if (_image) {
NSImage *copy = [_image copy];
NSSize size = [copy size];
float rx, ry, r;
NSPoint pt;
switch (_scaling) {
case NSScaleProportionally:
rx = bounds.size.width / size.width;
ry = bounds.size.height / size.height;
r = rx < ry ? rx : ry;
size.width *= r;
size.height *= r;
[copy setSize:size];
break;
case NSScaleToFit:
size = bounds.size;
[copy setSize:size];
break;
case NSScaleNone:
break;
default:
;
}
pt.x = (bounds.size.width - size.width) / 2;
pt.y = (bounds.size.height - size.height) / 2;
[copy compositeToPoint:pt operation:NSCompositeCopy];
[copy release];
}
}
- (unsigned int)draggingEntered:(id <NSDraggingInfo>)sender {
if ([[sender draggingPasteboard] availableTypeFromArray:[NSArray
arrayWithObject:NSFilenamesPboardType]]) {
return NSDragOperationCopy;
}
return NSDragOperationNone;
}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
NSPasteboard *paste = [sender draggingPasteboard];
NSArray *types = [NSArray arrayWithObjects:NSPostScriptPboardType,
NSPICTPboardType, NSPDFPboardType, NSFileContentsPboardType,
NSTIFFPboardType, NSFilenamesPboardType, nil];
NSString *desiredType = [paste availableTypeFromArray:types];
NSImage *newImage = nil;
if ([desiredType isEqualToString:NSPDFPboardType] || [desiredType
isEqualToString:NSPostScriptPboardType]) {
}
if ([desiredType isEqualToString:NSFilenamesPboardType])
{
NSArray *fileArray = [paste
propertyListForType:@"NSFilenamesPboardType"];
NSString *path = [fileArray objectAtIndex:0];
if ([[path pathExtension] isEqualToString:@"pdf"]) {
NSPDFImageRep *pdfRep = [NSPDFImageRep
imageRepWithContentsOfFile:path];
while ([pdfRep currentPage]) {
NSImageView *pageView = [[NSImageView alloc] init];
newImage = [[[NSImage alloc] initWith
Data:[pdfRep
currentPage]] autorelease];
[pageView setImage:newImage];
[filePages addSubview:pageView];
pageView=nil;
[newImage release];
}
return YES;
}
newImage = [[NSImage alloc] initWithContentsOfFile:path];
if (nil == newImage)
{
NSRunAlertPanel(NSLocalizedString(@"failureTitle",
@"Failed"), NSLocalizedString(@"imageLoadFailedMessage", @"The file
failed to load"), NSLocalizedString(@"Ok", @"Ok"), nil, nil);
return NO;
}
}
else
{
NSData *carriedData = [paste dataForType:desiredType];
if (nil == carriedData)
{
NSRunAlertPanel(NSLocalizedString(@"failureTitle",
@"Failed"), NSLocalizedString(@"imageLoadFailedMessage", @"The file
failed to load"), NSLocalizedString(@"Ok", @"Ok"), nil, nil);
return NO;
}
else
{
newImage = [[NSImage alloc] initWith
Data:carriedData];
if (nil == newImage)
{
NSRunAlertPanel(NSLocalizedString(@"failureTitle",
@"Failed"), NSLocalizedString(@"imageLoadFailedMessage", @"The file
failed to load"), NSLocalizedString(@"Ok", @"Ok"), nil, nil);
return NO;
}
}
}
[self setImage:newImage];
[newImage release];
[self setNeedsDisplay:YES]; //redraw us with the new image
return YES;
}
- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender {
return YES;
}
@end
Hasan Diwan
OpenPGP KeyID: 0x7EE3855B
Fingerprint: 42F0 5758 C3EB BA1F ABD2 ED49 3390 CCF0 7EE3 855B
http://www.cs.rpi.edu/~diwanh/gpg.key
[demime 0.98b removed an attachment of type application/pgp-signature which had a name of PGP.sig]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.