Sfoglia il codice sorgente

PDFSamples(iOS)- redact和enrypt samples 的完成

yangliuhua 1 anno fa
parent
commit
96a91b40ea

+ 32 - 0
Samples/Samples.xcodeproj/project.pbxproj

@@ -32,6 +32,9 @@
 		C97699982A5AAEF900187038 /* CBackgroundViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C97699972A5AAEF900187038 /* CBackgroundViewController.m */; };
 		C976999C2A5ACAAB00187038 /* CHeaderFooterViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C976999B2A5ACAAB00187038 /* CHeaderFooterViewController.m */; };
 		C97699A02A5BA1C300187038 /* CBatesViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C976999F2A5BA1C300187038 /* CBatesViewController.m */; };
+		C97699A42A5BAF8D00187038 /* CRedactViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C97699A32A5BAF8D00187038 /* CRedactViewController.m */; };
+		C97699A82A5BD1B400187038 /* CEncryptViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C97699A72A5BD1B400187038 /* CEncryptViewController.m */; };
+		C97699AC2A5BEB9B00187038 /* AllPasswords.pdf in Resources */ = {isa = PBXBuildFile; fileRef = C97699AB2A5BEB9B00187038 /* AllPasswords.pdf */; };
 		C9E361142A5273BC004D31BE /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = C9E361132A5273BC004D31BE /* AppDelegate.m */; };
 		C9E361172A5273BC004D31BE /* SceneDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = C9E361162A5273BC004D31BE /* SceneDelegate.m */; };
 		C9E3611A2A5273BC004D31BE /* CSamplesFuctionViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C9E361192A5273BC004D31BE /* CSamplesFuctionViewController.m */; };
@@ -102,6 +105,11 @@
 		C976999B2A5ACAAB00187038 /* CHeaderFooterViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CHeaderFooterViewController.m; sourceTree = "<group>"; };
 		C976999E2A5BA1C300187038 /* CBatesViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CBatesViewController.h; sourceTree = "<group>"; };
 		C976999F2A5BA1C300187038 /* CBatesViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CBatesViewController.m; sourceTree = "<group>"; };
+		C97699A22A5BAF8D00187038 /* CRedactViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CRedactViewController.h; sourceTree = "<group>"; };
+		C97699A32A5BAF8D00187038 /* CRedactViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CRedactViewController.m; sourceTree = "<group>"; };
+		C97699A62A5BD1B400187038 /* CEncryptViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CEncryptViewController.h; sourceTree = "<group>"; };
+		C97699A72A5BD1B400187038 /* CEncryptViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CEncryptViewController.m; sourceTree = "<group>"; };
+		C97699AB2A5BEB9B00187038 /* AllPasswords.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = AllPasswords.pdf; sourceTree = "<group>"; };
 		C995A2E42A53FEAD00762127 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
 		C9E3610F2A5273BC004D31BE /* Samples.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Samples.app; sourceTree = BUILT_PRODUCTS_DIR; };
 		C9E361122A5273BC004D31BE /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
@@ -154,6 +162,7 @@
 		C97699552A5534BF00187038 /* TestFile */ = {
 			isa = PBXGroup;
 			children = (
+				C97699AB2A5BEB9B00187038 /* AllPasswords.pdf */,
 				C97699922A5A5BAC00187038 /* Logo.png */,
 				C97699742A569AA400187038 /* CreateAnnotationTest.pdf */,
 				C97699722A566F3E00187038 /* Bird.wav */,
@@ -295,6 +304,24 @@
 			path = Bates;
 			sourceTree = "<group>";
 		};
+		C97699A12A5BAF4F00187038 /* Redact */ = {
+			isa = PBXGroup;
+			children = (
+				C97699A22A5BAF8D00187038 /* CRedactViewController.h */,
+				C97699A32A5BAF8D00187038 /* CRedactViewController.m */,
+			);
+			path = Redact;
+			sourceTree = "<group>";
+		};
+		C97699A52A5BD19A00187038 /* Encrypt */ = {
+			isa = PBXGroup;
+			children = (
+				C97699A62A5BD1B400187038 /* CEncryptViewController.h */,
+				C97699A72A5BD1B400187038 /* CEncryptViewController.m */,
+			);
+			path = Encrypt;
+			sourceTree = "<group>";
+		};
 		C9E361062A5273BC004D31BE = {
 			isa = PBXGroup;
 			children = (
@@ -340,6 +367,8 @@
 				C976998E2A581B7600187038 /* Watermark */,
 				C97699992A5ACA7700187038 /* HeaderFooter */,
 				C976999D2A5BA19600187038 /* Bates */,
+				C97699A12A5BAF4F00187038 /* Redact */,
+				C97699A52A5BD19A00187038 /* Encrypt */,
 				C9E3611B2A5273BC004D31BE /* Main.storyboard */,
 				C9E3611E2A5273BF004D31BE /* Assets.xcassets */,
 				C995A2E42A53FEAD00762127 /* Info.plist */,
@@ -436,6 +465,7 @@
 				C97699932A5A5BAC00187038 /* Logo.png in Resources */,
 				C97699752A569AA400187038 /* CreateAnnotationTest.pdf in Resources */,
 				C9E3613B2A53AD60004D31BE /* CSamplesBaseViewController.xib in Resources */,
+				C97699AC2A5BEB9B00187038 /* AllPasswords.pdf in Resources */,
 				C9E3611D2A5273BC004D31BE /* Main.storyboard in Resources */,
 				C9E361302A52E506004D31BE /* license_key_ios.xml in Resources */,
 				C976996B2A55709A00187038 /* text.pdf in Resources */,
@@ -464,7 +494,9 @@
 				C97699982A5AAEF900187038 /* CBackgroundViewController.m in Sources */,
 				C9E361252A5273BF004D31BE /* main.m in Sources */,
 				C97699892A5800A100187038 /* CTextExtractViewController.m in Sources */,
+				C97699A42A5BAF8D00187038 /* CRedactViewController.m in Sources */,
 				C97699652A554F4800187038 /* CPDFToImageViewController.m in Sources */,
+				C97699A82A5BD1B400187038 /* CEncryptViewController.m in Sources */,
 				C97699542A55162B00187038 /* COutlineViewController.m in Sources */,
 				C97699712A5595DF00187038 /* CAnnotaitonViewController.m in Sources */,
 				C97699692A556AE400187038 /* CTextSearchViewController.m in Sources */,

+ 1 - 1
Samples/Samples/Bates/CBatesViewController.m

@@ -279,7 +279,7 @@
 - (IBAction)buttonItemClick_run:(id)sender {
     self.isRun = YES;
     
-    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Running Watermark sample...\n\n"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Running Bates sample...\n\n"];
     [self addCommonBates:self.document];
     [self editBates:self.document];
     [self deleteBates:self.document];

+ 14 - 0
Samples/Samples/CSamplesFuctionViewController.m

@@ -27,6 +27,8 @@
 #import "CBackgroundViewController.h"
 #import "CHeaderFooterViewController.h"
 #import "CBatesViewController.h"
+#import "CRedactViewController.h"
+#import "CEncryptViewController.h"
 
 #import <ComPDFKit/ComPDFKit.h>
 
@@ -223,6 +225,18 @@
             [self.navigationController pushViewController:batesVC animated:YES];
         }
             break;
+        case CSamplesTypePDFRedact:
+        {
+            CRedactViewController *redactVC = [[CRedactViewController alloc] initWithDocument:self.document];
+            [self.navigationController pushViewController:redactVC animated:YES];
+        }
+            break;
+        case CSamplesTypeEncry:
+        {
+            CEncryptViewController *encryptVC = [[CEncryptViewController alloc] initWithDocument:self.document];
+            [self.navigationController pushViewController:encryptVC animated:YES];
+        }
+            break;
             
         default:
             break;

+ 21 - 0
Samples/Samples/Encrypt/CEncryptViewController.h

@@ -0,0 +1,21 @@
+//
+//  CEncryptViewController.h
+//  Samples
+//
+//  Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
+//
+//  THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
+//  AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
+//  UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
+//  This notice may not be removed from this file.
+//
+
+#import "CSamplesBaseViewController.h"
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface CEncryptViewController : CSamplesBaseViewController
+
+@end
+
+NS_ASSUME_NONNULL_END

+ 404 - 0
Samples/Samples/Encrypt/CEncryptViewController.m

@@ -0,0 +1,404 @@
+//
+//  CEncryptViewController.m
+//  Samples
+//
+//  Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
+//
+//  THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
+//  AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
+//  UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
+//  This notice may not be removed from this file.
+//
+#import "CEncryptViewController.h"
+
+#import <ComPDFKit/ComPDFKit.h>
+
+//-----------------------------------------------------------------------------------------
+// The sample code illustrates how to encrypt and decrypt documents using API.
+//-----------------------------------------------------------------------------------------
+
+@interface CEncryptViewController ()
+
+@property (nonatomic, strong) CPDFDocument *document;
+
+@property (nonatomic, assign) BOOL isRun;
+
+@property (nonatomic, strong) NSString *commandLineStr;
+
+@property (nonatomic, strong) NSURL *URL;
+
+@property (nonatomic, strong) NSMutableArray *userPasswordURLs;
+
+@end
+
+@implementation CEncryptViewController
+
+#pragma mark - UIViewController Methods
+
+- (void)viewDidLoad {
+    [super viewDidLoad];
+    // Do any additional setup after loading the view.
+    self.explainLabel.text = NSLocalizedString(@"The sample code illustrates how to encrypt and decrypt documents using API.", nil);
+    
+    self.commandLineTextView.text = @"";
+    self.isRun = NO;
+    self.commandLineStr = @"";
+    
+    self.userPasswordURLs = [NSMutableArray array];
+}
+
+#pragma mark - Samples Methods
+
+- (void)encryptByUserPassword:(CPDFDocument *)document {
+    {
+        self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
+        self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Samples 1: Document use RC4 encrypt done\n"];
+        
+        // Save a document in Sandbox
+        NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
+        NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Encrypt"];
+        NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"Samples",@"CommonFivePage"];
+        
+        // Copy file
+        if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
+            [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
+        NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"EncryptUserRC4Test"];
+        
+        if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
+            [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
+        
+        NSURL *encryptUserRC4URL = [NSURL fileURLWithPath:writeFilePath];
+        document = [[CPDFDocument alloc] initWithURL:encryptUserRC4URL];
+        [self.userPasswordURLs addObject:encryptUserRC4URL];
+        
+        NSDictionary *options = @{CPDFDocumentUserPasswordOption : @"User",
+                                  CPDFDocumentEncryptionLevelOption : @(CPDFDocumentEncryptionLevelRC4)};
+        [document writeToURL:encryptUserRC4URL withOptions:options];
+        
+        self.commandLineStr = [self.commandLineStr stringByAppendingString:@"User password is: User\n"];
+        self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Done. Results saved in EncryptUserRC4Test.pdf\n\n"];
+    }
+    
+    {
+        self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
+        self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Samples 2: Document use AES128 encrypt done\n"];
+        
+        // Save a document in Sandbox
+        NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
+        NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Encrypt"];
+        NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"Samples",@"CommonFivePage"];
+        
+        // Copy file
+        if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
+            [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
+        NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"EncryptUserAES128Test"];
+        
+        if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
+            [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
+        
+        NSURL *encryptUserAES128URL = [NSURL fileURLWithPath:writeFilePath];
+        document = [[CPDFDocument alloc] initWithURL:encryptUserAES128URL];
+        [self.userPasswordURLs addObject:encryptUserAES128URL];
+        
+        NSDictionary *options = @{CPDFDocumentUserPasswordOption : @"User",
+                                  CPDFDocumentEncryptionLevelOption : @(CPDFDocumentEncryptionLevelAES128)};
+        [document writeToURL:encryptUserAES128URL withOptions:options];
+        
+        self.commandLineStr = [self.commandLineStr stringByAppendingString:@"User password is: User\n"];
+        self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Done. Results saved in EncryptUserAES128Test.pdf\n\n"];
+    }
+    
+    {
+        self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
+        self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Samples 3: Document use AES256 encrypt done\n"];
+        
+        // Save a document in Sandbox
+        NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
+        NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Encrypt"];
+        NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"Samples",@"CommonFivePage"];
+        
+        // Copy file
+        if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
+            [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
+        NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"EncryptUserAES256Test"];
+        
+        if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
+            [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
+        
+        NSURL *encryptUserAES256URL = [NSURL fileURLWithPath:writeFilePath];
+        document = [[CPDFDocument alloc] initWithURL:encryptUserAES256URL];
+        [self.userPasswordURLs addObject:encryptUserAES256URL];
+        
+        NSDictionary *options = @{CPDFDocumentUserPasswordOption : @"User",
+                                  CPDFDocumentEncryptionLevelOption : @(CPDFDocumentEncryptionLevelAES256)};
+        [document writeToURL:encryptUserAES256URL withOptions:options];
+        
+        self.commandLineStr = [self.commandLineStr stringByAppendingString:@"User password is: User\n"];
+        self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Done. Results saved in EncryptUserAES256Test.pdf\n\n"];
+    }
+    
+    {
+        self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
+        self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Samples 4: Document use NoEncryptAlgo encrypt done\n"];
+        
+        // Save a document in Sandbox
+        NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
+        NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Encrypt"];
+        NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"Samples",@"CommonFivePage"];
+        
+        // Copy file
+        if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
+            [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
+        NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"EncryptUserNoEncryptAlgoTest"];
+        
+        if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
+            [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
+        
+        NSURL *encryptUserNoEncryptAlgoURL = [NSURL fileURLWithPath:writeFilePath];
+        document = [[CPDFDocument alloc] initWithURL:encryptUserNoEncryptAlgoURL];
+        [self.userPasswordURLs addObject:encryptUserNoEncryptAlgoURL];
+        
+        NSDictionary *options = @{CPDFDocumentUserPasswordOption : @"User",
+                                  CPDFDocumentEncryptionLevelOption : @(CPDFDocumentEncryptionLevelNoEncryptAlgo)};
+        [document writeToURL:encryptUserNoEncryptAlgoURL withOptions:options];
+        
+        self.commandLineStr = [self.commandLineStr stringByAppendingString:@"User password is: User\n"];
+        self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Done. Results saved in EncryptUserNoEncryptAlgoTest.pdf\n\n"];
+    }
+}
+
+- (void)encryptByOwnerPassword:(CPDFDocument *)document {
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Samples 5: Encrypt by owner password done\n"];
+    
+    // Save a document in Sandbox
+    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
+    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Encrypt"];
+    NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"Samples",@"CommonFivePage"];
+    
+    // Copy file
+    if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
+        [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
+    NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"EncryptOwnerRC4Test"];
+    
+    if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
+        [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
+    
+    NSURL *encryptOwnerRC4URL = [NSURL fileURLWithPath:writeFilePath];
+    document = [[CPDFDocument alloc] initWithURL:encryptOwnerRC4URL];
+    [self.userPasswordURLs addObject:encryptOwnerRC4URL];
+    
+    NSDictionary *options = @{CPDFDocumentOwnerPasswordOption : @"Owner",
+                              CPDFDocumentEncryptionLevelOption : @(CPDFDocumentEncryptionLevelRC4),
+                              CPDFDocumentAllowsPrintingOption : @(NO),
+                              CPDFDocumentAllowsCopyingOption : @(NO),};
+    [document writeToURL:encryptOwnerRC4URL withOptions:options];
+    
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Owner password is: Owner\n"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Done. Results saved in EncryptOwnerRC4Test.pdf\n\n"];
+}
+
+- (void)encryptByAllPasswords:(CPDFDocument *)document {
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Samples 6: Encrypt by Both user and owner passwords done\n"];
+    
+    // Save a document in Sandbox
+    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
+    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Encrypt"];
+    NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"Samples",@"CommonFivePage"];
+    
+    // Copy file
+    if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
+        [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
+    NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"EncryptAllAES256Test"];
+    
+    if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
+        [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
+    
+    NSURL *encryptAllAES256URL = [NSURL fileURLWithPath:writeFilePath];
+    document = [[CPDFDocument alloc] initWithURL:encryptAllAES256URL];
+    [self.userPasswordURLs addObject:encryptAllAES256URL];
+    
+    NSDictionary *options = @{CPDFDocumentOwnerPasswordOption : @"User",
+                              CPDFDocumentUserPasswordOption : @"Owner",
+                              CPDFDocumentEncryptionLevelOption : @(CPDFDocumentEncryptionLevelAES256),
+                              CPDFDocumentAllowsPrintingOption : @(YES),
+                              CPDFDocumentAllowsHighQualityPrintingOption : @(NO),
+                              CPDFDocumentAllowsCopyingOption : @(NO),
+                              CPDFDocumentAllowsDocumentChangesOption : @(NO),
+                              CPDFDocumentAllowsDocumentAssemblyOption : @(NO),
+                              CPDFDocumentAllowsCommentingOption : @(NO),
+                              CPDFDocumentAllowsFormFieldEntryOption : @(NO)};
+    [document writeToURL:encryptAllAES256URL withOptions:options];
+    
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"User password is: User\n"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Owner password is: Owner\n"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Done. Results saved in EncryptAllAES256Test.pdf\n\n"];
+}
+
+- (void)unlock:(CPDFDocument *)document {
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Samples 7: Unlock with owner password and user password\n"];
+    
+    // Save a document in Sandbox
+    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
+    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Encrypt"];
+    NSString *documentFolder = [[NSBundle mainBundle] pathForResource:@"AllPasswords" ofType:@"pdf"];
+    
+    // Copy file
+    if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
+        [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
+    NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"unlockAllPasswords"];
+    
+    if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
+        [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
+    
+    NSURL *unlockAllPasswordsURL = [NSURL fileURLWithPath:writeFilePath];
+    document = [[CPDFDocument alloc] initWithURL:unlockAllPasswordsURL];
+    
+    [document unlockWithPassword:@"Owner"];
+    
+    self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"AllowsPrinting:%@\n", document.allowsPrinting ? @"YES" :  @"NO"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"AllowsHighQualityPrinting:%@\n", document.allowsHighQualityPrinting ? @"YES" :  @"NO"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"AllowsCopying:%@\n", document.allowsCopying ? @"YES" :  @"NO"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"AllowsDocumentChanges:%@\n", document.allowsDocumentChanges ? @"YES" :  @"NO"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"AllowsDocumentAssembly:%@\n", document.allowsDocumentAssembly ? @"YES" :  @"NO"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"AllowsCommenting:%@\n", document.allowsPrinting ? @"YES" :  @"NO"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"AllowsFormFieldEntry:%@\n\n", document.allowsFormFieldEntry ? @"YES" :  @"NO"];
+}
+
+- (void)decrypt:(CPDFDocument *)document {
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Samples 8: Decrypt with owner password and user password\n"];
+    
+    // Save a document in Sandbox
+    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
+    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Encrypt"];
+    NSString *documentFolder = [[NSBundle mainBundle] pathForResource:@"AllPasswords" ofType:@"pdf"];
+    
+    // Copy file
+    if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
+        [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
+    NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"DecryptAllPasswordsTest"];
+    
+    if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
+        [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
+    
+    NSURL *decryptAllPasswordsURL = [NSURL fileURLWithPath:writeFilePath];
+    document = [[CPDFDocument alloc] initWithURL:decryptAllPasswordsURL];
+    [self.userPasswordURLs addObject:decryptAllPasswordsURL];
+    
+    [document unlockWithPassword:@"Owner"];
+    [document writeDecryptToURL:decryptAllPasswordsURL];
+    
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Done. Results saved in DecryptAllPasswordsTest.pdf\n\n"];
+}
+
+
+- (void)openFileWithURL:(NSURL *)url {
+    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:@[url] applicationActivities:nil];
+        activityVC.definesPresentationContext = YES;
+    if (UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM()) {
+        activityVC.popoverPresentationController.sourceView = self.openfileButton;
+        activityVC.popoverPresentationController.sourceRect = self.openfileButton.bounds;
+    }
+    [self presentViewController:activityVC animated:YES completion:nil];
+    activityVC.completionWithItemsHandler = ^(UIActivityType  _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
+
+        if (completed) {
+            NSLog(@"Success!");
+        } else {
+            NSLog(@"Failed Or Canceled!");
+        }
+    };
+}
+
+#pragma mark - Action
+
+- (IBAction)buttonItemClick_openFile:(id)sender {
+    // Determine whether to export the document
+    if (self.isRun) {
+        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Choose a file to open...", nil) message:@"" preferredStyle:UIAlertControllerStyleAlert];
+        if (UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM()) {
+            alertController.popoverPresentationController.sourceView = self.openfileButton;
+            alertController.popoverPresentationController.sourceRect = self.openfileButton.bounds;
+        }
+        
+        UIAlertAction *encryptUserRC4Action = [UIAlertAction actionWithTitle:NSLocalizedString(@"   EncryptUserRC4Test.pdf   ", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+            // Open CreateBookmarkTest.pdf
+            [self openFileWithURL:self.userPasswordURLs[0]];
+        }];
+        
+        UIAlertAction *encryptUserAES128Action = [UIAlertAction actionWithTitle:NSLocalizedString(@"   EncryptUserAES128Test.pdf   ", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+            // Open CreateBookmarkTest.pdf
+            [self openFileWithURL:self.userPasswordURLs[1]];
+        }];
+        UIAlertAction *encryptUserAES256Action = [UIAlertAction actionWithTitle:NSLocalizedString(@"   EncryptUserAES256Test.pdf   ", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+            // Open CreateBookmarkTest.pdf
+            [self openFileWithURL:self.userPasswordURLs[2]];
+        }];
+        UIAlertAction *encryptUserNoEncryptAlgoAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"   EncryptUserNoEncryptAlgoTest.pdf   ", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+            // Open CreateBookmarkTest.pdf
+            [self openFileWithURL:self.userPasswordURLs[3]];
+        }];
+        UIAlertAction *encryptOwnerRC4Action = [UIAlertAction actionWithTitle:NSLocalizedString(@"   EncryptOwnerRC4Test.pdf   ", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+            // Open CreateBookmarkTest.pdf
+            [self openFileWithURL:self.userPasswordURLs[4]];
+        }];
+        UIAlertAction *encryptAllAES256Action = [UIAlertAction actionWithTitle:NSLocalizedString(@"   EncryptAllAES256Test.pdf   ", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+            // Open CreateBookmarkTest.pdf
+            [self openFileWithURL:self.userPasswordURLs[5]];
+        }];
+        UIAlertAction *decryptAllPasswordsAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"   DecryptAllPasswordsTest.pdf   ", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+            // Open CreateBookmarkTest.pdf
+            [self openFileWithURL:self.userPasswordURLs[6]];
+        }];
+        
+        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:nil];
+        
+        [alertController addAction:encryptUserRC4Action];
+        [alertController addAction:encryptUserAES128Action];
+        [alertController addAction:encryptUserAES256Action];
+        [alertController addAction:encryptUserNoEncryptAlgoAction];
+        [alertController addAction:encryptOwnerRC4Action];
+        [alertController addAction:encryptAllAES256Action];
+        [alertController addAction:decryptAllPasswordsAction];
+        [alertController addAction:cancelAction];
+        
+        [self presentViewController:alertController animated:NO completion:nil];
+    } else {
+        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Choose a file to open...", nil) message:@"" preferredStyle:UIAlertControllerStyleAlert];
+        if (UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM()) {
+            alertController.popoverPresentationController.sourceView = self.openfileButton;
+            alertController.popoverPresentationController.sourceRect = self.openfileButton.bounds;
+        }
+        
+        UIAlertAction *noAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"No files for this sample.", nil) style:UIAlertActionStyleDefault handler:nil];
+        
+        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:nil];
+        
+        [alertController addAction:noAction];
+        [alertController addAction:cancelAction];
+        
+        [self presentViewController:alertController animated:NO completion:nil];
+    }
+}
+
+- (IBAction)buttonItemClick_run:(id)sender {
+    self.isRun = YES;
+    
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Running Bookmark sample...\n\n"];
+    [self encryptByUserPassword:self.document];
+    [self encryptByOwnerPassword:self.document];
+    [self encryptByAllPasswords:self.document];
+    [self unlock:self.document];
+    [self decrypt:self.document];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"\nDone!\n"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
+    
+    // Refresh commandline message
+    self.commandLineTextView.text = self.commandLineStr;
+}
+
+
+@end

+ 1 - 1
Samples/Samples/HeaderFooter/CHeaderFooterViewController.m

@@ -304,7 +304,7 @@
 - (IBAction)buttonItemClick_run:(id)sender {
     self.isRun = YES;
     
-    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Running Watermark sample...\n\n"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Running HeaderFooter sample...\n\n"];
     [self addCommonHeaderFooter:self.document];
     [self addPageHeaderFooter:self.document];
     [self editHeaderFooter:self.document];

+ 21 - 0
Samples/Samples/Redact/CRedactViewController.h

@@ -0,0 +1,21 @@
+//
+//  CRedactViewController.h
+//  Samples
+//
+//  Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
+//
+//  THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
+//  AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
+//  UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
+//  This notice may not be removed from this file.
+//
+
+#import "CSamplesBaseViewController.h"
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface CRedactViewController : CSamplesBaseViewController
+
+@end
+
+NS_ASSUME_NONNULL_END

File diff suppressed because it is too large
+ 171 - 0
Samples/Samples/Redact/CRedactViewController.m


+ 0 - 4
Samples/Samples/Watermark/CWatermarkViewController.m

@@ -89,8 +89,6 @@
     watermark.ty = 0.0; // The translation relative to the vertical position. Positive numbers are shifted downwards, negative numbers are shifted upwards.
     watermark.isFront = YES; // Set watermark to locate in front of the content.
     watermark.isTilePage = NO; // Set tiled watermark for the page(image watermark does not work).
-//    watermark.verticalSpacing = 10; // Set the vertical spacing for the tiled watermark.
-//    watermark.horizontalSpacing = 10; // Set the horizontal spacing for the tiled watermark.
     watermark.pageString = @"0-4";
     [document addWatermark:watermark];
     [document updateWatermark:watermark];
@@ -145,8 +143,6 @@
     watermark.tx = 0.0; // The translation relative to the horizontal position. Positive numbers are shifted to the right, negative numbers are shifted to the left.
     watermark.ty = 0.0; // The translation relative to the vertical position. Positive numbers are shifted downwards, negative numbers are shifted upwards.
     watermark.isFront = YES; // Set watermark to locate in front of the content.
-//    watermark.verticalSpacing = 10; // Set the vertical spacing for the tiled watermark.
-//    watermark.horizontalSpacing = 10; // Set the horizontal spacing for the tiled watermark.
     watermark.pageString = @"0-4";
     [document addWatermark:watermark];
 //    [document updateWatermark:watermark];

BIN
TestFile/AllPasswords.pdf