Skip to content

Commit 57f2c61

Browse files
First Commit
0 parents  commit 57f2c61

File tree

144 files changed

+8345
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

144 files changed

+8345
-0
lines changed

Classes/Document/CGPDFDocument.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// CGPDFDocument.h
3+
// Reader v2.6.0
4+
//
5+
// Created by Julius Oklamcak on 2011-07-01.
6+
// Copyright © 2011-2013 Julius Oklamcak. All rights reserved.
7+
//
8+
// Permission is hereby granted, free of charge, to any person obtaining a copy
9+
// of this software and associated documentation files (the "Software"), to deal
10+
// in the Software without restriction, including without limitation the rights to
11+
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12+
// of the Software, and to permit persons to whom the Software is furnished to
13+
// do so, subject to the following conditions:
14+
//
15+
// The above copyright notice and this permission notice shall be included in all
16+
// copies or substantial portions of the Software.
17+
//
18+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
//
25+
26+
#import <Foundation/Foundation.h>
27+
#import <QuartzCore/QuartzCore.h>
28+
29+
/**
30+
Create a CGPDFDocumentRef from the PDF file at the given URL.
31+
32+
@param url The URL of the PDF file to load.
33+
@param password The password to unlock the file if necessary.
34+
35+
@return A CGPDFDocumentRef.
36+
*/
37+
CGPDFDocumentRef CGPDFDocumentCreate(NSURL *url, NSString *password);
38+
39+
/**
40+
Wether or not the given password will unlock the PDF file at the given URL.
41+
42+
@param url The URL of the PDF file to check.
43+
@param password The password to attempt to unlock the PDF file with.
44+
45+
@return YES if the password unlocks the document. NO otherwise.
46+
*/
47+
BOOL CGPDFDocumentCanBeUnlockedWithPassword(NSURL *url, NSString *password);

Classes/Document/CGPDFDocument.m

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
//
2+
// CGPDFDocument.m
3+
// Reader v2.6.0
4+
//
5+
// Created by Julius Oklamcak on 2011-07-01.
6+
// Copyright © 2011-2013 Julius Oklamcak. All rights reserved.
7+
//
8+
// Permission is hereby granted, free of charge, to any person obtaining a copy
9+
// of this software and associated documentation files (the "Software"), to deal
10+
// in the Software without restriction, including without limitation the rights to
11+
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12+
// of the Software, and to permit persons to whom the Software is furnished to
13+
// do so, subject to the following conditions:
14+
//
15+
// The above copyright notice and this permission notice shall be included in all
16+
// copies or substantial portions of the Software.
17+
//
18+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
//
25+
26+
#import "CGPDFDocument.h"
27+
28+
CGPDFDocumentRef CGPDFDocumentCreate(NSURL *url, NSString *password)
29+
{
30+
CGPDFDocumentRef docRef = NULL;
31+
32+
//Do we have a URL?
33+
if (url != nil) {
34+
docRef = CGPDFDocumentCreateWithURL((__bridge CFURLRef)url);
35+
36+
//Did the document load?
37+
if (docRef != NULL) {
38+
39+
//Is the document password protected?
40+
if (CGPDFDocumentIsEncrypted(docRef) == TRUE) {
41+
42+
//Try a blank password first, per Apple's Quartz PDF example.
43+
if (CGPDFDocumentUnlockWithPassword(docRef, "") == FALSE) {
44+
45+
//Nope, now let's try the provided password to unlock the PDF
46+
if ((password != nil) && ([password length] > 0)) {
47+
48+
char text[128]; // char array buffer for the string conversion
49+
[password getCString:text maxLength:126 encoding:NSUTF8StringEncoding];
50+
51+
//If we can't unlock the document.
52+
if (CGPDFDocumentUnlockWithPassword(docRef, text) == FALSE) // Log failure
53+
{
54+
#ifdef DEBUG
55+
NSLog(@"CGPDFDocumentCreate: Unable to unlock [%@] with [%@]", url, password);
56+
#endif
57+
}
58+
}
59+
}
60+
//Failed to unlock the document. Cleanup.
61+
if (CGPDFDocumentIsUnlocked(docRef) == FALSE) {
62+
CGPDFDocumentRelease(docRef), docRef = NULL;
63+
}
64+
}
65+
} else {
66+
#ifdef DEBUG
67+
NSLog(@"CGPDFDocumentCreate: Unable to load PDF Document.");
68+
#endif
69+
}
70+
} else {
71+
#ifdef DEBUG
72+
NSLog(@"CGPDFDocumentCreate: No URL Provided");
73+
#endif
74+
}
75+
76+
return docRef;
77+
}
78+
79+
BOOL CGPDFDocumentCanBeUnlockedWithPassword(NSURL *url, NSString *password)
80+
{
81+
BOOL unlockable = NO;
82+
//Do we have a URL
83+
if (url != nil) {
84+
CGPDFDocumentRef thePDFDocRef = CGPDFDocumentCreateWithURL((__bridge CFURLRef)url);
85+
86+
//Do we have a document?
87+
if (thePDFDocRef != NULL)
88+
{
89+
//Is the document locked?
90+
if (CGPDFDocumentIsEncrypted(thePDFDocRef) == TRUE) {
91+
92+
// Try a blank password first, per Apple's Quartz PDF example
93+
if (CGPDFDocumentUnlockWithPassword(thePDFDocRef, "") == FALSE) {
94+
95+
// Nope, now let's try the provided password to unlock the PDF
96+
if ((password != nil) && ([password length] > 0)) // Not blank?
97+
{
98+
char text[128]; // char array buffer for the string conversion
99+
100+
[password getCString:text maxLength:126 encoding:NSUTF8StringEncoding];
101+
102+
if (CGPDFDocumentUnlockWithPassword(thePDFDocRef, text) == FALSE) {
103+
unlockable = YES;
104+
}
105+
} else {
106+
unlockable = NO;
107+
}
108+
}
109+
}
110+
111+
CGPDFDocumentRelease(thePDFDocRef); // Cleanup CGPDFDocumentRef
112+
}
113+
} else {
114+
#ifdef DEBUG
115+
NSLog(@"CGPDFDocumentCanBeUnlockedWithPassword: No URL Provided");
116+
#endif
117+
}
118+
119+
return unlockable;
120+
}

Classes/Document/PDFKDocument.h

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
//
2+
// ReaderDocument.h
3+
// Reader v2.6.0
4+
//
5+
// Created by Julius Oklamcak on 2011-07-01.
6+
// Copyright © 2011-2013 Julius Oklamcak. All rights reserved.
7+
//
8+
// Permission is hereby granted, free of charge, to any person obtaining a copy
9+
// of this software and associated documentation files (the "Software"), to deal
10+
// in the Software without restriction, including without limitation the rights to
11+
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12+
// of the Software, and to permit persons to whom the Software is furnished to
13+
// do so, subject to the following conditions:
14+
//
15+
// The above copyright notice and this permission notice shall be included in all
16+
// copies or substantial portions of the Software.
17+
//
18+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
//
25+
26+
#import <Foundation/Foundation.h>
27+
28+
/**
29+
A object that represents a single PDF File.
30+
*/
31+
@interface PDFKDocument : NSObject <NSObject, NSCoding>
32+
33+
/**@name Document Properties*/
34+
/**
35+
The current/last page that the document displaied.
36+
*/
37+
@property (nonatomic, assign, readwrite) NSUInteger currentPage;
38+
/**
39+
The set of numbers defining the bookmarked pages.
40+
*/
41+
@property (nonatomic, strong, readonly) NSMutableIndexSet *bookmarks;
42+
/**
43+
The password to open the PDF file if necessary.
44+
*/
45+
@property (nonatomic, strong, readonly) NSString *password;
46+
/**
47+
The URL location of the PDF file.
48+
*/
49+
@property (nonatomic, strong, readonly) NSURL *fileURL;
50+
/**
51+
The globally unique identifier for the PDF document.
52+
*/
53+
@property (nonatomic, strong, readonly) NSString *guid;
54+
/**
55+
The last time the PDF file was opened.
56+
*/
57+
@property (nonatomic, strong, readwrite) NSDate *lastOpenedDate;
58+
/**
59+
The size of the PDF file in bytes.
60+
*/
61+
@property (nonatomic, assign, readonly) NSUInteger fileSize;
62+
/**
63+
The total number of pages in the PDF document.
64+
*/
65+
@property (nonatomic, assign, readonly) NSUInteger pageCount;
66+
67+
/**@name File Properties*/
68+
/**
69+
The title of the PDF document.
70+
*/
71+
@property (nonatomic, strong, readonly) NSString *title;
72+
/**
73+
The author of the PDF document.
74+
*/
75+
@property (nonatomic, strong, readonly) NSString *author;
76+
/**
77+
The subject of the document.
78+
*/
79+
@property (nonatomic, strong, readonly) NSString *subject;
80+
/**
81+
Keywords decribing the document's contents.
82+
*/
83+
@property (nonatomic, strong, readonly) NSString *keywords;
84+
/**
85+
The creator of the PDF.
86+
*/
87+
@property (nonatomic, strong, readonly) NSString *creator;
88+
/**
89+
The producer of the PDF.
90+
*/
91+
@property (nonatomic, strong, readonly) NSString *producer;
92+
/**
93+
The last time the PDF file was modified.
94+
*/
95+
@property (nonatomic, strong, readonly) NSDate *modificationDate;
96+
/**
97+
The date the PDF was created.
98+
*/
99+
@property (nonatomic, strong, readonly) NSDate *creationDate;
100+
/**
101+
The PDF version.
102+
*/
103+
@property (nonatomic, assign, readonly) CGFloat version;
104+
105+
/**@name Creation*/
106+
/**
107+
Creates a PDF document from the PDF file at the given path. Unarchiving it if necessary.
108+
109+
@note This method should be the method used to create the PDF document, it handles unarchiving the document for you. If the document archive does not exist, a new document is created.
110+
111+
@param filePath The path of the PDF file to load.
112+
@param password The password to unlock the PDF file if necessary.
113+
114+
@return A new PDFKDocument.
115+
*/
116+
+ (PDFKDocument *)documentWithContentsOfFile:(NSString *)filePath password:(NSString *)password;
117+
/**
118+
Unarchive the stored document information and create a PDF document from the PDF file at the given path.
119+
120+
@param filePath The path of the PDF file to load.
121+
@param password The password to unlock the PDF file if necessary.
122+
123+
@return A new PDFKDocument.
124+
*/
125+
+ (PDFKDocument *)unarchiveDocumentForContentsOfFile:(NSString *)filePath password:(NSString *)password;
126+
/**
127+
Initalize a PDF document from the PDF file at the given path.
128+
129+
@param filePath The path of the PDF file to load.
130+
@param password The password to unlock the PDF file if necessary.
131+
132+
@return A new PDFKDocument.
133+
*/
134+
- (id)initWithContentsOfFile:(NSString *)filePath password:(NSString *)password;
135+
/**
136+
Save the document information to the archive.
137+
*/
138+
- (void)saveReaderDocument;
139+
/**
140+
Reload the document properties from the PDF file.
141+
*/
142+
- (void)updateProperties;
143+
144+
@end

0 commit comments

Comments
 (0)