-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPDFDocument_PDCategory.m
106 lines (76 loc) · 2.6 KB
/
PDFDocument_PDCategory.m
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// PDDocument_PDCategory.m
// SproutedUtilities
//
// Created by Philip Dow on 1/19/07.
// Copyright Sprouted. All rights reserved.
// All inquiries should be directed to [email protected]
//
#import <SproutedUtilities/PDFDocument_PDCategory.h>
@implementation PDFDocument (PDCategory )
- (NSImage*) thumbnailForPage:(unsigned int)index size:(float)edge
{
if ( index > [self pageCount] )
return nil;
//NSPDFImageRep *imageRep = [NSPDFImageRep imageRepWithData:[[self pageAtIndex:0] dataRepresentation]];
//if ( imageRep == nil )
// return nil;
//NSSize repSize = [imageRep size];
NSPDFImageRep *imageRep = [NSPDFImageRep imageRepWithData:[self dataRepresentation]];
if ( imageRep == nil )
return nil;
[imageRep setCurrentPage:index];
NSSize repSize = [imageRep size];
NSImage *renderedPage = [[[NSImage alloc] initWithSize:NSMakeSize(edge,edge)] autorelease];
[renderedPage lockFocus];
NSRect targetRect;
if ( repSize.width > repSize.height )
{
int newHeight = edge*repSize.height/repSize.width;
targetRect = NSMakeRect(0, edge/2 - newHeight/2 ,edge, newHeight);
}
else
{
int newWidth = edge*repSize.width/repSize.height;
targetRect = NSMakeRect(edge/2 - newWidth/2,0,newWidth,edge);
}
[[NSColor whiteColor] set];
NSRectFillUsingOperation(targetRect, NSCompositeSourceOver);
[imageRep drawInRect:targetRect];
[[NSColor lightGrayColor] set];
[[NSBezierPath bezierPathWithRect:targetRect] stroke];
[renderedPage unlockFocus];
return renderedPage;
}
- (NSImage*) efficientThumbnailForPage:(unsigned int)index size:(float)edge
{
NSImage *theImage = nil;
// I had this commented out, why? another option might be drawWithBox
NSPDFImageRep *imageRep = [NSPDFImageRep imageRepWithData:[[self pageAtIndex:index] dataRepresentation]];
if ( imageRep == nil )
return nil;
[imageRep setCurrentPage:0];
NSSize repSize = [imageRep size];
theImage = [[[NSImage alloc] initWithSize:NSMakeSize(edge,edge)] autorelease];
//[theImage addRepresentation:imageRep];
[theImage lockFocus];
NSRect targetRect;
if ( repSize.width > repSize.height )
{
int newHeight = edge*repSize.height/repSize.width;
targetRect = NSMakeRect(0, edge/2 - newHeight/2 ,edge, newHeight);
}
else
{
int newWidth = edge*repSize.width/repSize.height;
targetRect = NSMakeRect(edge/2 - newWidth/2,0,newWidth,edge);
}
[[NSColor whiteColor] set];
NSRectFillUsingOperation(targetRect, NSCompositeSourceOver);
[imageRep drawInRect:targetRect];
[[NSColor lightGrayColor] set];
[[NSBezierPath bezierPathWithRect:targetRect] stroke];
[theImage unlockFocus];
return theImage;
}
@end