Просмотр исходного кода

PDFSamples(iOS)- bates samples 的完成

yangliuhua 1 год назад
Родитель
Сommit
4db8546653

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

@@ -31,6 +31,7 @@
 		C97699932A5A5BAC00187038 /* Logo.png in Resources */ = {isa = PBXBuildFile; fileRef = C97699922A5A5BAC00187038 /* Logo.png */; };
 		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 */; };
 		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 */; };
@@ -99,6 +100,8 @@
 		C97699972A5AAEF900187038 /* CBackgroundViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CBackgroundViewController.m; sourceTree = "<group>"; };
 		C976999A2A5ACAAA00187038 /* CHeaderFooterViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CHeaderFooterViewController.h; sourceTree = "<group>"; };
 		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>"; };
 		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>"; };
@@ -283,6 +286,15 @@
 			path = HeaderFooter;
 			sourceTree = "<group>";
 		};
+		C976999D2A5BA19600187038 /* Bates */ = {
+			isa = PBXGroup;
+			children = (
+				C976999E2A5BA1C300187038 /* CBatesViewController.h */,
+				C976999F2A5BA1C300187038 /* CBatesViewController.m */,
+			);
+			path = Bates;
+			sourceTree = "<group>";
+		};
 		C9E361062A5273BC004D31BE = {
 			isa = PBXGroup;
 			children = (
@@ -327,6 +339,7 @@
 				C97699952A5AAEB100187038 /* Background */,
 				C976998E2A581B7600187038 /* Watermark */,
 				C97699992A5ACA7700187038 /* HeaderFooter */,
+				C976999D2A5BA19600187038 /* Bates */,
 				C9E3611B2A5273BC004D31BE /* Main.storyboard */,
 				C9E3611E2A5273BF004D31BE /* Assets.xcassets */,
 				C995A2E42A53FEAD00762127 /* Info.plist */,
@@ -444,6 +457,7 @@
 				C97699852A57E56900187038 /* CImageExtractViewController.m in Sources */,
 				C97699612A5534D500187038 /* XMLReader.m in Sources */,
 				C9E361142A5273BC004D31BE /* AppDelegate.m in Sources */,
+				C97699A02A5BA1C300187038 /* CBatesViewController.m in Sources */,
 				C976997D2A56AD3B00187038 /* CInteractiveFormsViewController.m in Sources */,
 				C97699792A569C9400187038 /* CAnnotationImportExportViewController.m in Sources */,
 				C976999C2A5ACAAB00187038 /* CHeaderFooterViewController.m in Sources */,

+ 21 - 0
Samples/Samples/Bates/CBatesViewController.h

@@ -0,0 +1,21 @@
+//
+//  CBatesViewController.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 CBatesViewController : CSamplesBaseViewController
+
+@end
+
+NS_ASSUME_NONNULL_END

+ 293 - 0
Samples/Samples/Bates/CBatesViewController.m

@@ -0,0 +1,293 @@
+//
+//  CBatesViewController.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 "CBatesViewController.h"
+
+#import <ComPDFKit/ComPDFKit.h>
+
+//-----------------------------------------------------------------------------------------
+// The sample code illustrates how to add and remove bates using API.
+//-----------------------------------------------------------------------------------------
+
+@interface CBatesViewController ()
+
+@property (nonatomic, strong) CPDFDocument *document;
+
+@property (nonatomic, assign) BOOL isRun;
+
+@property (nonatomic, strong) NSString *commandLineStr;
+
+@property (nonatomic, strong) NSURL *addCommonBatesURL;
+
+@property (nonatomic, strong) NSURL *editBatesURL;
+
+@property (nonatomic, strong) NSURL *deleteBatesURL;
+
+@end
+
+@implementation CBatesViewController
+
+#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 add and remove bates using API.", nil);
+    
+    self.commandLineTextView.text = @"";
+    self.isRun = NO;
+    self.commandLineStr = @"";
+}
+
+#pragma mark - Samples Methods
+
+- (void)addCommonBates:(CPDFDocument *)document {
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Samples 1: Insert common bates\n"];
+    
+    // Save a document in Sandbox
+    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
+    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Bates"];
+    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,@"AddCommonBatesTest"];
+    
+    if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
+        [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
+    
+    self.addCommonBatesURL = [NSURL fileURLWithPath:writeFilePath];
+    document = [[CPDFDocument alloc] initWithURL:self.addCommonBatesURL];
+    
+    CPDFBates *bates = document.bates;
+    [bates setText:@"<<#3#5#Prefix-#-Suffix>>" atIndex:0];
+    [bates setTextColor:[CPDFKitPlatformColor redColor] atIndex:0];
+    [bates setFontSize:14.0 atIndex:0];
+    [bates setText:@"<<#3#5#Prefix-#-Suffix>>" atIndex:1];
+    [bates setTextColor:[CPDFKitPlatformColor redColor] atIndex:1];
+    [bates setFontSize:14.0 atIndex:1];
+    [bates setText:@"<<#3#5#Prefix-#-Suffix>>" atIndex:2];
+    [bates setTextColor:[CPDFKitPlatformColor redColor] atIndex:2];
+    [bates setFontSize:14.0 atIndex:2];
+    [bates setText:@"<<#3#5#Prefix-#-Suffix>>" atIndex:3];
+    [bates setTextColor:[CPDFKitPlatformColor redColor] atIndex:3];
+    [bates setFontSize:14.0 atIndex:3];
+    [bates setText:@"<<#3#5#Prefix-#-Suffix>>" atIndex:4];
+    [bates setTextColor:[CPDFKitPlatformColor redColor] atIndex:4];
+    [bates setFontSize:14.0 atIndex:4];
+    [bates setText:@"<<#3#5#Prefix-#-Suffix>>" atIndex:5];
+    [bates setTextColor:[CPDFKitPlatformColor redColor] atIndex:5];
+    [bates setFontSize:14.0 atIndex:5];
+    bates.pageString = @"0-4";
+    [bates update];
+    
+    for (int i = 0; i < 6; i++) {
+        self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Text: %@\n", [bates textAtIndex:i]];
+        self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Location: %@\n\n", [self getStringFromEnumLocation:i]];
+    }
+    
+    [document writeToURL:self.addCommonBatesURL];
+    
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Done. Results saved in AddCommonBatesTest.pdf\n"];
+}
+
+- (void)editBates:(CPDFDocument *)document {
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Samples 2: Edit common bates\n"];
+    
+    // Save a document in Sandbox
+    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
+    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Bates"];
+    NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"Bates",@"AddCommonBatesTest"];
+    
+    // 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,@"EditBatesTest"];
+    
+    if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
+        [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
+    
+    self.editBatesURL = [NSURL fileURLWithPath:writeFilePath];
+    document = [[CPDFDocument alloc] initWithURL:self.editBatesURL];
+    
+    CPDFBates *bates = document.bates;
+    [bates setText:@"<<#3#5#ComPDFKit-#-ComPDFKit>>" atIndex:0];
+    [bates setTextColor:[CPDFKitPlatformColor redColor] atIndex:0];
+    [bates setFontSize:14.0 atIndex:0];
+    [bates setText:@"<<#3#5#Prefix-#-Suffix>>" atIndex:1];
+    [bates setTextColor:[CPDFKitPlatformColor redColor] atIndex:1];
+    [bates setFontSize:14.0 atIndex:1];
+    [bates setText:@"<<#3#5#Prefix-#-Suffix>>" atIndex:2];
+    [bates setTextColor:[CPDFKitPlatformColor redColor] atIndex:2];
+    [bates setFontSize:14.0 atIndex:2];
+    [bates setText:@"<<#3#5#Prefix-#-Suffix>>" atIndex:3];
+    [bates setTextColor:[CPDFKitPlatformColor redColor] atIndex:3];
+    [bates setFontSize:14.0 atIndex:3];
+    [bates setText:@"<<#3#5#Prefix-#-Suffix>>" atIndex:4];
+    [bates setTextColor:[CPDFKitPlatformColor redColor] atIndex:4];
+    [bates setFontSize:14.0 atIndex:4];
+    [bates setText:@"<<#3#5#Prefix-#-Suffix>>" atIndex:5];
+    [bates setTextColor:[CPDFKitPlatformColor redColor] atIndex:5];
+    [bates setFontSize:14.0 atIndex:5];
+    bates.pageString = @"0-4";
+    [bates update];
+    
+    for (int i = 0; i < 3; i++) {
+        self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Text: %@\n", [bates textAtIndex:i]];
+        self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Location: %@\n\n", [self getStringFromEnumLocation:i]];
+    }
+    
+    [document writeToURL:self.editBatesURL];
+    
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Done. Results saved in EditBatesTest.pdf\n"];
+}
+
+- (void)deleteBates:(CPDFDocument *)document {
+//     DeleteBatesTest
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Samples 3: Delete common bates\n"];
+    
+    // Save a document in Sandbox
+    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
+    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Bates"];
+    NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"Bates",@"AddCommonBatesTest"];
+    
+    // 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,@"DeleteBatesTest"];
+    
+    if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
+        [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
+    
+    self.deleteBatesURL = [NSURL fileURLWithPath:writeFilePath];
+    document = [[CPDFDocument alloc] initWithURL:self.deleteBatesURL];
+    
+    CPDFBates *bates = document.bates;
+    [bates clear];
+    
+    [document writeToURL:self.deleteBatesURL];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Done. Results saved in DeleteBatesTest.pdf\n"];
+}
+
+- (NSString *)getStringFromEnumLocation:(NSInteger)enums {
+    switch (enums) {
+        case 0:
+            return @"Top Left";
+            break;
+        case 1:
+            return @"Top Middle";
+            break;
+        case 2:
+            return @"Top Right";
+            break;
+        case 3:
+            return @"Button Left";
+            break;
+        case 4:
+            return @"Button Middle";
+            break;
+        case 5:
+            return @"Button Right";
+            break;
+        default:
+            return @" ";
+            break;
+    }
+}
+
+- (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 *addCommonBatesAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"   AddCommonBatesTest.pdf   ", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+            // Open CreateBookmarkTest.pdf
+            [self openFileWithURL:self.addCommonBatesURL];
+        }];
+        UIAlertAction *editBatesAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"   EditBatesTest.pdf   ", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+            // Open CreateBookmarkTest.pdf
+            [self openFileWithURL:self.editBatesURL];
+        }];
+        UIAlertAction *deleteBatesAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"   DeleteBatesTest.pdf   ", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+            // Open CreateBookmarkTest.pdf
+            [self openFileWithURL:self.deleteBatesURL];
+        }];
+        
+        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:nil];
+        
+        [alertController addAction:addCommonBatesAction];
+        [alertController addAction:editBatesAction];
+        [alertController addAction:deleteBatesAction];
+        [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 Watermark sample...\n\n"];
+    [self addCommonBates:self.document];
+    [self editBates:self.document];
+    [self deleteBates:self.document];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"\nDone!\n"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
+    
+    // Refresh commandline message
+    self.commandLineTextView.text = self.commandLineStr;
+}
+
+@end

+ 7 - 0
Samples/Samples/CSamplesFuctionViewController.m

@@ -26,6 +26,7 @@
 #import "CWatermarkViewController.h"
 #import "CBackgroundViewController.h"
 #import "CHeaderFooterViewController.h"
+#import "CBatesViewController.h"
 
 #import <ComPDFKit/ComPDFKit.h>
 
@@ -216,6 +217,12 @@
             [self.navigationController pushViewController:headerfooterVC animated:YES];
         }
             break;
+        case CSamplesTypePDFBates:
+        {
+            CBatesViewController *batesVC = [[CBatesViewController alloc] initWithDocument:self.document];
+            [self.navigationController pushViewController:batesVC animated:YES];
+        }
+            break;
             
         default:
             break;

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

@@ -16,7 +16,7 @@
 #import <ComPDFKit/ComPDFKit.h>
 
 //-----------------------------------------------------------------------------------------
-// The sample code illustrates how to add and remove header footer susing API.
+// The sample code illustrates how to add and remove header footer using API.
 //-----------------------------------------------------------------------------------------
 
 @interface CHeaderFooterViewController ()
@@ -228,7 +228,6 @@
     }
 }
 
-
 - (void)openFileWithURL:(NSURL *)url {
     UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:@[url] applicationActivities:nil];
         activityVC.definesPresentationContext = YES;
@@ -316,4 +315,5 @@
     // Refresh commandline message
     self.commandLineTextView.text = self.commandLineStr;
 }
+
 @end