Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
NinjaKittenProductions committed May 14, 2014
0 parents commit 57f2c61
Show file tree
Hide file tree
Showing 144 changed files with 8,345 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Classes/Document/CGPDFDocument.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// CGPDFDocument.h
// Reader v2.6.0
//
// Created by Julius Oklamcak on 2011-07-01.
// Copyright © 2011-2013 Julius Oklamcak. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to
// do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>

/**
Create a CGPDFDocumentRef from the PDF file at the given URL.
@param url The URL of the PDF file to load.
@param password The password to unlock the file if necessary.
@return A CGPDFDocumentRef.
*/
CGPDFDocumentRef CGPDFDocumentCreate(NSURL *url, NSString *password);

/**
Wether or not the given password will unlock the PDF file at the given URL.
@param url The URL of the PDF file to check.
@param password The password to attempt to unlock the PDF file with.
@return YES if the password unlocks the document. NO otherwise.
*/
BOOL CGPDFDocumentCanBeUnlockedWithPassword(NSURL *url, NSString *password);
120 changes: 120 additions & 0 deletions Classes/Document/CGPDFDocument.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
//
// CGPDFDocument.m
// Reader v2.6.0
//
// Created by Julius Oklamcak on 2011-07-01.
// Copyright © 2011-2013 Julius Oklamcak. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to
// do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#import "CGPDFDocument.h"

CGPDFDocumentRef CGPDFDocumentCreate(NSURL *url, NSString *password)
{
CGPDFDocumentRef docRef = NULL;

//Do we have a URL?
if (url != nil) {
docRef = CGPDFDocumentCreateWithURL((__bridge CFURLRef)url);

//Did the document load?
if (docRef != NULL) {

//Is the document password protected?
if (CGPDFDocumentIsEncrypted(docRef) == TRUE) {

//Try a blank password first, per Apple's Quartz PDF example.
if (CGPDFDocumentUnlockWithPassword(docRef, "") == FALSE) {

//Nope, now let's try the provided password to unlock the PDF
if ((password != nil) && ([password length] > 0)) {

char text[128]; // char array buffer for the string conversion
[password getCString:text maxLength:126 encoding:NSUTF8StringEncoding];

//If we can't unlock the document.
if (CGPDFDocumentUnlockWithPassword(docRef, text) == FALSE) // Log failure
{
#ifdef DEBUG
NSLog(@"CGPDFDocumentCreate: Unable to unlock [%@] with [%@]", url, password);
#endif
}
}
}
//Failed to unlock the document. Cleanup.
if (CGPDFDocumentIsUnlocked(docRef) == FALSE) {
CGPDFDocumentRelease(docRef), docRef = NULL;
}
}
} else {
#ifdef DEBUG
NSLog(@"CGPDFDocumentCreate: Unable to load PDF Document.");
#endif
}
} else {
#ifdef DEBUG
NSLog(@"CGPDFDocumentCreate: No URL Provided");
#endif
}

return docRef;
}

BOOL CGPDFDocumentCanBeUnlockedWithPassword(NSURL *url, NSString *password)
{
BOOL unlockable = NO;
//Do we have a URL
if (url != nil) {
CGPDFDocumentRef thePDFDocRef = CGPDFDocumentCreateWithURL((__bridge CFURLRef)url);

//Do we have a document?
if (thePDFDocRef != NULL)
{
//Is the document locked?
if (CGPDFDocumentIsEncrypted(thePDFDocRef) == TRUE) {

// Try a blank password first, per Apple's Quartz PDF example
if (CGPDFDocumentUnlockWithPassword(thePDFDocRef, "") == FALSE) {

// Nope, now let's try the provided password to unlock the PDF
if ((password != nil) && ([password length] > 0)) // Not blank?
{
char text[128]; // char array buffer for the string conversion

[password getCString:text maxLength:126 encoding:NSUTF8StringEncoding];

if (CGPDFDocumentUnlockWithPassword(thePDFDocRef, text) == FALSE) {
unlockable = YES;
}
} else {
unlockable = NO;
}
}
}

CGPDFDocumentRelease(thePDFDocRef); // Cleanup CGPDFDocumentRef
}
} else {
#ifdef DEBUG
NSLog(@"CGPDFDocumentCanBeUnlockedWithPassword: No URL Provided");
#endif
}

return unlockable;
}
144 changes: 144 additions & 0 deletions Classes/Document/PDFKDocument.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
//
// ReaderDocument.h
// Reader v2.6.0
//
// Created by Julius Oklamcak on 2011-07-01.
// Copyright © 2011-2013 Julius Oklamcak. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to
// do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#import <Foundation/Foundation.h>

/**
A object that represents a single PDF File.
*/
@interface PDFKDocument : NSObject <NSObject, NSCoding>

/**@name Document Properties*/
/**
The current/last page that the document displaied.
*/
@property (nonatomic, assign, readwrite) NSUInteger currentPage;
/**
The set of numbers defining the bookmarked pages.
*/
@property (nonatomic, strong, readonly) NSMutableIndexSet *bookmarks;
/**
The password to open the PDF file if necessary.
*/
@property (nonatomic, strong, readonly) NSString *password;
/**
The URL location of the PDF file.
*/
@property (nonatomic, strong, readonly) NSURL *fileURL;
/**
The globally unique identifier for the PDF document.
*/
@property (nonatomic, strong, readonly) NSString *guid;
/**
The last time the PDF file was opened.
*/
@property (nonatomic, strong, readwrite) NSDate *lastOpenedDate;
/**
The size of the PDF file in bytes.
*/
@property (nonatomic, assign, readonly) NSUInteger fileSize;
/**
The total number of pages in the PDF document.
*/
@property (nonatomic, assign, readonly) NSUInteger pageCount;

/**@name File Properties*/
/**
The title of the PDF document.
*/
@property (nonatomic, strong, readonly) NSString *title;
/**
The author of the PDF document.
*/
@property (nonatomic, strong, readonly) NSString *author;
/**
The subject of the document.
*/
@property (nonatomic, strong, readonly) NSString *subject;
/**
Keywords decribing the document's contents.
*/
@property (nonatomic, strong, readonly) NSString *keywords;
/**
The creator of the PDF.
*/
@property (nonatomic, strong, readonly) NSString *creator;
/**
The producer of the PDF.
*/
@property (nonatomic, strong, readonly) NSString *producer;
/**
The last time the PDF file was modified.
*/
@property (nonatomic, strong, readonly) NSDate *modificationDate;
/**
The date the PDF was created.
*/
@property (nonatomic, strong, readonly) NSDate *creationDate;
/**
The PDF version.
*/
@property (nonatomic, assign, readonly) CGFloat version;

/**@name Creation*/
/**
Creates a PDF document from the PDF file at the given path. Unarchiving it if necessary.
@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.
@param filePath The path of the PDF file to load.
@param password The password to unlock the PDF file if necessary.
@return A new PDFKDocument.
*/
+ (PDFKDocument *)documentWithContentsOfFile:(NSString *)filePath password:(NSString *)password;
/**
Unarchive the stored document information and create a PDF document from the PDF file at the given path.
@param filePath The path of the PDF file to load.
@param password The password to unlock the PDF file if necessary.
@return A new PDFKDocument.
*/
+ (PDFKDocument *)unarchiveDocumentForContentsOfFile:(NSString *)filePath password:(NSString *)password;
/**
Initalize a PDF document from the PDF file at the given path.
@param filePath The path of the PDF file to load.
@param password The password to unlock the PDF file if necessary.
@return A new PDFKDocument.
*/
- (id)initWithContentsOfFile:(NSString *)filePath password:(NSString *)password;
/**
Save the document information to the archive.
*/
- (void)saveReaderDocument;
/**
Reload the document properties from the PDF file.
*/
- (void)updateProperties;

@end
Loading

0 comments on commit 57f2c61

Please sign in to comment.