Ver Fonte

PDFSamplels(iOS)-TextSearch 模块

yangliuhua há 1 ano atrás
pai
commit
153171f47e

+ 17 - 3
Samples/Samples.xcodeproj/project.pbxproj

@@ -15,6 +15,9 @@
 		C976995D2A5534BF00187038 /* developer_guide_ios.pdf in Resources */ = {isa = PBXBuildFile; fileRef = C97699592A5534BF00187038 /* developer_guide_ios.pdf */; };
 		C97699612A5534D500187038 /* XMLReader.m in Sources */ = {isa = PBXBuildFile; fileRef = C97699602A5534D500187038 /* XMLReader.m */; };
 		C97699652A554F4800187038 /* CPDFToImageViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C97699642A554F4800187038 /* CPDFToImageViewController.m */; };
+		C97699692A556AE400187038 /* CTextSearchViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C97699682A556AE400187038 /* CTextSearchViewController.m */; };
+		C976996B2A55709A00187038 /* text.pdf in Resources */ = {isa = PBXBuildFile; fileRef = C976996A2A55709A00187038 /* text.pdf */; };
+		C976996D2A55868100187038 /* CommonFivePage.pdf in Resources */ = {isa = PBXBuildFile; fileRef = C976996C2A55868100187038 /* CommonFivePage.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 */; };
@@ -56,6 +59,10 @@
 		C97699602A5534D500187038 /* XMLReader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XMLReader.m; sourceTree = "<group>"; };
 		C97699632A554F4800187038 /* CPDFToImageViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CPDFToImageViewController.h; sourceTree = "<group>"; };
 		C97699642A554F4800187038 /* CPDFToImageViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CPDFToImageViewController.m; sourceTree = "<group>"; };
+		C97699672A556AE400187038 /* CTextSearchViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CTextSearchViewController.h; sourceTree = "<group>"; };
+		C97699682A556AE400187038 /* CTextSearchViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CTextSearchViewController.m; sourceTree = "<group>"; };
+		C976996A2A55709A00187038 /* text.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = text.pdf; sourceTree = "<group>"; };
+		C976996C2A55868100187038 /* CommonFivePage.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = CommonFivePage.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>"; };
@@ -108,6 +115,8 @@
 		C97699552A5534BF00187038 /* TestFile */ = {
 			isa = PBXGroup;
 			children = (
+				C976996C2A55868100187038 /* CommonFivePage.pdf */,
+				C976996A2A55709A00187038 /* text.pdf */,
 				C97699562A5534BF00187038 /* Form_Widgets_Test.pdf */,
 				C97699572A5534BF00187038 /* PDF32000_2008.pdf */,
 				C97699582A5534BF00187038 /* Blank Page.pdf */,
@@ -139,6 +148,8 @@
 		C97699662A5566E800187038 /* TextSearch */ = {
 			isa = PBXGroup;
 			children = (
+				C97699672A556AE400187038 /* CTextSearchViewController.h */,
+				C97699682A556AE400187038 /* CTextSearchViewController.m */,
 			);
 			path = TextSearch;
 			sourceTree = "<group>";
@@ -172,7 +183,7 @@
 				C9E361162A5273BC004D31BE /* SceneDelegate.m */,
 				C9E361182A5273BC004D31BE /* CSamplesFuctionViewController.h */,
 				C9E361192A5273BC004D31BE /* CSamplesFuctionViewController.m */,
-				C9E361362A53ACDE004D31BE /* ComPDFKit_Samples */,
+				C9E361362A53ACDE004D31BE /* SamplesBase */,
 				C97699512A5515DF00187038 /* Outline */,
 				C97699622A554F1000187038 /* PDFToImage */,
 				C97699502A54F93200187038 /* Bookmark */,
@@ -194,14 +205,14 @@
 			name = Frameworks;
 			sourceTree = "<group>";
 		};
-		C9E361362A53ACDE004D31BE /* ComPDFKit_Samples */ = {
+		C9E361362A53ACDE004D31BE /* SamplesBase */ = {
 			isa = PBXGroup;
 			children = (
 				C9E361372A53AD60004D31BE /* CSamplesBaseViewController.h */,
 				C9E361382A53AD60004D31BE /* CSamplesBaseViewController.m */,
 				C9E361392A53AD60004D31BE /* CSamplesBaseViewController.xib */,
 			);
-			path = ComPDFKit_Samples;
+			path = SamplesBase;
 			sourceTree = "<group>";
 		};
 /* End PBXGroup section */
@@ -268,9 +279,11 @@
 				C976995C2A5534BF00187038 /* Blank Page.pdf in Resources */,
 				C976995D2A5534BF00187038 /* developer_guide_ios.pdf in Resources */,
 				C976995A2A5534BF00187038 /* Form_Widgets_Test.pdf in Resources */,
+				C976996D2A55868100187038 /* CommonFivePage.pdf in Resources */,
 				C9E3613B2A53AD60004D31BE /* CSamplesBaseViewController.xib in Resources */,
 				C9E3611D2A5273BC004D31BE /* Main.storyboard in Resources */,
 				C9E361302A52E506004D31BE /* license_key_ios.xml in Resources */,
+				C976996B2A55709A00187038 /* text.pdf in Resources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -288,6 +301,7 @@
 				C9E361252A5273BF004D31BE /* main.m in Sources */,
 				C97699652A554F4800187038 /* CPDFToImageViewController.m in Sources */,
 				C97699542A55162B00187038 /* COutlineViewController.m in Sources */,
+				C97699692A556AE400187038 /* CTextSearchViewController.m in Sources */,
 				C9E361172A5273BC004D31BE /* SceneDelegate.m in Sources */,
 				C9E3613A2A53AD60004D31BE /* CSamplesBaseViewController.m in Sources */,
 			);

+ 6 - 2
Samples/Samples/AppDelegate.m

@@ -68,8 +68,9 @@ static AppDelegate *appDelegate = NULL;
     self.window = window;
     self.window.backgroundColor = [UIColor whiteColor];
     
-    NSString *filePathTest1 = [[NSBundle mainBundle] pathForResource:@"Blank Page" ofType:@"pdf"];
+    NSString *filePathTest1 = [[NSBundle mainBundle] pathForResource:@"CommonFivePage" ofType:@"pdf"];
     NSString *filePathTest2 = [[NSBundle mainBundle] pathForResource:@"PDF32000_2008" ofType:@"pdf"];
+    NSString *filePathTest3 = [[NSBundle mainBundle] pathForResource:@"text" ofType:@"pdf"];
     
     NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@", @"Documents",@"Samples"];
 
@@ -82,9 +83,12 @@ static AppDelegate *appDelegate = NULL;
     NSString * documentPathTest2 = [documentFolder stringByAppendingPathComponent:filePathTest2.lastPathComponent];
     if (![[NSFileManager defaultManager] fileExistsAtPath:documentPathTest2])
         [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:filePathTest2] toURL:[NSURL fileURLWithPath:documentPathTest2] error:nil];
+    NSString * documentPathTest3 = [documentFolder stringByAppendingPathComponent:filePathTest3.lastPathComponent];
+    if (![[NSFileManager defaultManager] fileExistsAtPath:documentPathTest3])
+        [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:filePathTest3] toURL:[NSURL fileURLWithPath:documentPathTest3] error:nil];
     
     
-    CSamplesFuctionViewController *viewVC = [[CSamplesFuctionViewController alloc] initWithFilePath:@[documentPathTest1, documentPathTest2] password:nil];
+    CSamplesFuctionViewController *viewVC = [[CSamplesFuctionViewController alloc] initWithFilePath:@[documentPathTest1, documentPathTest2, documentPathTest3] password:nil];
     UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:viewVC];
     self.window.rootViewController = navController;
     

+ 5 - 5
Samples/Samples/Bookmark/CBookmarkViewController.m

@@ -57,10 +57,10 @@
         self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Go to page 2\n"];
     }
       
-    // Save a document in Sandbox path
+    // Save a document in Sandbox 
     NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
     NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Bookmark"];
-    NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"Samples",@"Blank Page"];
+    NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"Samples",@"CommonFivePage"];
     
     // Copy file
     if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
@@ -101,20 +101,20 @@
 - (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:UIAlertControllerStyleActionSheet];
+        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(@"CreateBookmarkTest.pdf", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+        UIAlertAction *textSearchAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"   CreateBookmarkTest.pdf   ", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
             // Open CreateBookmarkTest.pdf
             [self openFile];
         }];
         
         UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:nil];
         
-        [alertController addAction:noAction];
+        [alertController addAction:textSearchAction];
         [alertController addAction:cancelAction];
         
         [self presentViewController:alertController animated:NO completion:nil];

+ 12 - 4
Samples/Samples/CSamplesFuctionViewController.m

@@ -15,6 +15,7 @@
 #import "CBookmarkViewController.h"
 #import "COutlineViewController.h"
 #import "CPDFToImageViewController.h"
+#import "CTextSearchViewController.h"
 
 #import <ComPDFKit/ComPDFKit.h>
 
@@ -123,10 +124,8 @@
             break;
         case CSamplesTypeOutline:
         {
-            NSString *filePath = self.filePaths[1];
-            NSURL *url = [NSURL fileURLWithPath:filePath];
-            CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
-            COutlineViewController *outlineVC = [[COutlineViewController alloc] initWithDocument:document];
+            
+            COutlineViewController *outlineVC = [[COutlineViewController alloc] initWithDocument:self.document];
             [self.navigationController pushViewController:outlineVC animated:NO];
         }
             break;
@@ -136,6 +135,15 @@
             [self.navigationController pushViewController:PDFToImageVC animated:NO];
         }
             break;
+        case CSamplesTypeTextSearch:
+        {
+            NSString *filePath = self.filePaths[2];
+            NSURL *url = [NSURL fileURLWithPath:filePath];
+            CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
+            CTextSearchViewController *textSearchVC = [[CTextSearchViewController alloc] initWithDocument:document];
+            [self.navigationController pushViewController:textSearchVC animated:NO];
+        }
+            break;
             
         default:
             break;

+ 108 - 15
Samples/Samples/Outline/COutlineViewController.m

@@ -14,7 +14,8 @@
 #import <ComPDFKit/ComPDFKit.h>
 
 //-----------------------------------------------------------------------------------------
-// The sample code illustrates how to obtain document outline information using API.
+// The sample code illustrates how to create outline and obtain document outline
+// information using API.
 //-----------------------------------------------------------------------------------------
 
 @interface COutlineViewController ()
@@ -46,8 +47,58 @@
 
 #pragma mark - Samples Methods
 
+// Create outline
+- (void)createOutline:(CPDFDocument *)document {
+    // Create outline root
+    [document setNewOutlineRoot];
+    
+    // Get root from PDF document
+    CPDFOutline *outline = [document outlineRoot];
+    
+    // To insert a new outline and set outline text
+    CPDFOutline *outlinePage1 = [outline insertChildAtIndex:0];
+    outlinePage1.label = @"1. page1";
+    
+    CPDFOutline *outlinePage2 = [outline insertChildAtIndex:1];
+    outlinePage2.label = @"2. page2";
+    
+    CPDFOutline *outlinePage3 = [outline insertChildAtIndex:2];
+    outlinePage3.label = @"3. page3";
+    
+    CPDFOutline *outlinePage4 = [outline insertChildAtIndex:3];
+    outlinePage4.label = @"4. page4";
+    
+    CPDFOutline *outlinePage5 = [outline insertChildAtIndex:4];
+    outlinePage5.label = @"5. page5";
+    
+    // Insert secondary directory and set outline text
+    CPDFOutline *outlinePage1_1 = [outlinePage1 insertChildAtIndex:0];
+    outlinePage1_1.label = @"1.1 page1_1";
+    
+    // Save a document in Sandbox
+    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
+    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Outline"];
+    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,@"CreateOutlineTest"];
+    
+    if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
+        [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
+    
+    // Save the document in the PDF file
+    self.outlineURL = [NSURL fileURLWithPath:writeFilePath];
+    [document writeToURL:self.outlineURL];
+    
+    // Refresh document
+    self.document = [[CPDFDocument alloc] initWithURL:self.outlineURL];
+}
+    
+
 // Print document outline information
--(void)printOutline:(CPDFDocument *)document {
+- (void)printOutline:(CPDFDocument *)document {
     // Get root from PDF document
     CPDFOutline *outline = [document outlineRoot];
     
@@ -79,30 +130,72 @@
     }
 }
 
+- (void)openFile {
+    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:@[self.outlineURL] 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 {
     
-    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;
+    // 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 *textSearchAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"     CreateOutlineTest.pdf      ", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+            // Open CreateBookmarkTest.pdf
+            [self openFile];
+        }];
+        
+        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:nil];
+        
+        [alertController addAction:textSearchAction];
+        [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];
     }
-    
-    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;
     
     // Create bookmark and go to the page
     self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Running OutlineText sample...\n\n"];
+    [self createOutline:self.document];
     [self printOutline:self.document];
     self.commandLineStr = [self.commandLineStr stringByAppendingString:@"\nDone!\n"];
     self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];

+ 1 - 1
Samples/Samples/PDFToImage/CPDFToImageViewController.m

@@ -104,7 +104,7 @@
 - (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:UIAlertControllerStyleActionSheet];
+        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;

Samples/Samples/ComPDFKit_Samples/CSamplesBaseViewController.h → Samples/Samples/SamplesBase/CSamplesBaseViewController.h


Samples/Samples/ComPDFKit_Samples/CSamplesBaseViewController.m → Samples/Samples/SamplesBase/CSamplesBaseViewController.m


Samples/Samples/ComPDFKit_Samples/CSamplesBaseViewController.xib → Samples/Samples/SamplesBase/CSamplesBaseViewController.xib


+ 19 - 0
Samples/Samples/TextSearch/CTextSearchViewController.h

@@ -0,0 +1,19 @@
+//
+//  CTextSearchViewController.h
+//  Samples
+//
+//  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 CTextSearchViewController : CSamplesBaseViewController
+
+@end
+
+NS_ASSUME_NONNULL_END

+ 166 - 0
Samples/Samples/TextSearch/CTextSearchViewController.m

@@ -0,0 +1,166 @@
+//
+//  CTextSearchViewController.m
+//  Samples
+//
+//  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 "CTextSearchViewController.h"
+
+#import <ComPDFKit/ComPDFKit.h>
+
+//-----------------------------------------------------------------------------------------
+// The sample shows how to use TextSearch to search text on PDF pages using regular
+// expressions. TextSearch utility class bulids on functionality available in TextExtractor
+// to simplify most common search operations
+//-----------------------------------------------------------------------------------------
+
+@interface CTextSearchViewController ()
+
+@property (nonatomic, strong) CPDFDocument *document;
+
+@property (nonatomic, assign) BOOL isRun;
+
+@property (nonatomic, strong) NSString *commandLineStr;
+
+@property (nonatomic, strong) NSURL *textSearchURL;
+
+@end
+
+@implementation CTextSearchViewController
+
+#pragma mark - UIViewController Methods
+
+- (void)viewDidLoad {
+    [super viewDidLoad];
+    // Do any additional setup after loading the view.
+    self.explainLabel.text = NSLocalizedString(@"The sample shows how to use TextSearch to search text on PDF pages using regular expressions. TextSearch utility class bulids on functionality available in TextExtractor to simplify most common search operations", nil);
+    
+    self.commandLineTextView.text = @"";
+    self.isRun = NO;
+    self.commandLineStr = @"";
+}
+
+#pragma mark - Samples Methods
+
+- (void)searchText:(CPDFDocument *)document {
+    // Get array of search result
+    NSArray *resultArray = [document findString:@"PDF" withOptions:CPDFSearchCaseInsensitive];
+    
+    // Get the first page of search resultsGet the first page of search results
+    NSArray *selections = [resultArray objectAtIndex:0];
+    self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"the key PDF have %lu results", selections.count];
+    
+    // Get the first search result on the first page
+    CPDFSelection *selection = [selections objectAtIndex:0];
+    
+    // Set text hignlinght
+    NSMutableArray *quadrilateralPoints = [NSMutableArray array];
+    CPDFPage *page = [document pageAtIndex:0];
+    
+    CGRect bounds = selection.bounds;
+    [quadrilateralPoints addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMinX(bounds), CGRectGetMaxY(bounds))]];
+    [quadrilateralPoints addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMaxX(bounds), CGRectGetMaxY(bounds))]];
+    [quadrilateralPoints addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMinX(bounds), CGRectGetMinY(bounds))]];
+    [quadrilateralPoints addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMaxX(bounds), CGRectGetMinY(bounds))]];
+    
+    CPDFMarkupAnnotation *highlight = [[CPDFMarkupAnnotation alloc] initWithDocument:document markupType:CPDFMarkupTypeHighlight];
+    highlight.color = [UIColor yellowColor];
+    highlight.quadrilateralPoints = quadrilateralPoints;
+    [page addAnnotation:highlight];
+    
+    // Save TextSearchTest.pdf
+    // Save a document in Sandbox
+    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
+    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"TextSearch"];
+    NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"Samples",@"text"];
+    
+    // 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,@"TextSearchTest"];
+    
+    if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
+        [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
+    
+    // Save the document in the PDF file
+    self.textSearchURL = [NSURL fileURLWithPath:writeFilePath];
+    [document writeToURL:self.textSearchURL];
+}
+
+- (void)openFile {
+    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:@[self.textSearchURL] 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 *noAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"   TextSearchTest.pdf  ", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+            // Open TextSearchTest.pdf
+            [self openFile];
+        }];
+        
+        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:nil];
+        
+        [alertController addAction:noAction];
+        [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 TextSearchTest sample...\n\n"];
+    [self searchText:self.document];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"\nDone!\n"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
+    
+    // Refresh commandline message
+    self.commandLineTextView.text = self.commandLineStr;
+}
+
+
+@end

BIN
TestFile/CommonFivePage.pdf


BIN
TestFile/PDF32000_2008.pdf


BIN
TestFile/text.pdf