|
@@ -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
|