Browse Source

PDFTextExtract(iOS)-textExtract samples完成

yangliuhua 1 year ago
parent
commit
bcce3e9790

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

@@ -25,6 +25,7 @@
 		C976997D2A56AD3B00187038 /* CInteractiveFormsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C976997C2A56AD3B00187038 /* CInteractiveFormsViewController.m */; };
 		C97699812A579FEC00187038 /* CPDFPageViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C97699802A579FEC00187038 /* CPDFPageViewController.m */; };
 		C97699852A57E56900187038 /* CImageExtractViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C97699842A57E56900187038 /* CImageExtractViewController.m */; };
+		C97699892A5800A100187038 /* CTextExtractViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C97699882A5800A100187038 /* CTextExtractViewController.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 */; };
@@ -82,6 +83,8 @@
 		C97699802A579FEC00187038 /* CPDFPageViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CPDFPageViewController.m; sourceTree = "<group>"; };
 		C97699832A57E56900187038 /* CImageExtractViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CImageExtractViewController.h; sourceTree = "<group>"; };
 		C97699842A57E56900187038 /* CImageExtractViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CImageExtractViewController.m; sourceTree = "<group>"; };
+		C97699872A5800A100187038 /* CTextExtractViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CTextExtractViewController.h; sourceTree = "<group>"; };
+		C97699882A5800A100187038 /* CTextExtractViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CTextExtractViewController.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>"; };
@@ -220,6 +223,15 @@
 			path = ImageExtract;
 			sourceTree = "<group>";
 		};
+		C97699862A57FE5600187038 /* TextExtract */ = {
+			isa = PBXGroup;
+			children = (
+				C97699872A5800A100187038 /* CTextExtractViewController.h */,
+				C97699882A5800A100187038 /* CTextExtractViewController.m */,
+			);
+			path = TextExtract;
+			sourceTree = "<group>";
+		};
 		C9E361062A5273BC004D31BE = {
 			isa = PBXGroup;
 			children = (
@@ -259,6 +271,7 @@
 				C976997A2A56AC0300187038 /* InteractiveForms */,
 				C976997E2A579F5F00187038 /* PDFPage */,
 				C97699822A57E54700187038 /* ImageExtract */,
+				C97699862A57FE5600187038 /* TextExtract */,
 				C9E3611B2A5273BC004D31BE /* Main.storyboard */,
 				C9E3611E2A5273BF004D31BE /* Assets.xcassets */,
 				C995A2E42A53FEAD00762127 /* Info.plist */,
@@ -376,6 +389,7 @@
 				C976997D2A56AD3B00187038 /* CInteractiveFormsViewController.m in Sources */,
 				C97699792A569C9400187038 /* CAnnotationImportExportViewController.m in Sources */,
 				C9E361252A5273BF004D31BE /* main.m in Sources */,
+				C97699892A5800A100187038 /* CTextExtractViewController.m in Sources */,
 				C97699652A554F4800187038 /* CPDFToImageViewController.m in Sources */,
 				C97699542A55162B00187038 /* COutlineViewController.m in Sources */,
 				C97699712A5595DF00187038 /* CAnnotaitonViewController.m in Sources */,

+ 2 - 3
Samples/Samples/CSamplesFuctionViewController.h

@@ -27,11 +27,10 @@ typedef NS_ENUM(NSInteger, CSamplesType)  {
     CSamplesTypeWatermark,
     CSamplesTypeBackground,
     CSamplesTypeHeaderFooter,
-    CSamplesTypePDFBases,
+    CSamplesTypePDFBates,
     CSamplesTypePDFRedact,
     CSamplesTypeEncry,
-    CSamplesTypeFlattenedCopy,
-    CSamplesTypeDocumentCompare
+    CSamplesTypeFlattenedCopy
 };
 
 @interface CSamplesFuctionViewController : UIViewController

+ 9 - 3
Samples/Samples/CSamplesFuctionViewController.m

@@ -21,6 +21,7 @@
 #import "CInteractiveFormsViewController.h"
 #import "CPDFPageViewController.h"
 #import "CImageExtractViewController.h"
+#import "CTextExtractViewController.h"
 
 #import <ComPDFKit/ComPDFKit.h>
 
@@ -67,12 +68,11 @@
              NSLocalizedString(@"Watermark", nil),
              NSLocalizedString(@"Background", nil),
              NSLocalizedString(@"HeaderFooter", nil),
-             NSLocalizedString(@"Bases", nil),
+             NSLocalizedString(@"Bates", nil),
              NSLocalizedString(@"PDFRedact", nil),
              NSLocalizedString(@"Encry", nil),
              NSLocalizedString(@"PDFA", nil),
-             NSLocalizedString(@"FlattenedCopy", nil),
-             NSLocalizedString(@"DocumentCompare", nil)];
+             NSLocalizedString(@"FlattenedCopy", nil)];
     
 }
 
@@ -182,6 +182,12 @@
             [self.navigationController pushViewController:imageExtractVC animated:YES];
         }
             break;
+        case CSamplesTypeTextExtract:
+        {
+            CTextExtractViewController *textExtractVC = [[CTextExtractViewController alloc] initWithDocument:self.document];
+            [self.navigationController pushViewController:textExtractVC animated:YES];
+        }
+            break;
             
         default:
             break;

+ 1 - 1
Samples/Samples/ImageExtract/CImageExtractViewController.m

@@ -63,8 +63,8 @@
         [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
     
     NSIndexSet *pages = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, document.pageCount)];
-    [document extractImageFromPages:pages toPath:writeDirectoryPath];
     
+    [document extractImageFromPages:pages toPath:writeDirectoryPath];
     NSArray *fileArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:writeDirectoryPath error:nil];
     for (NSString *fileName in fileArray) {
         NSString *filePath = [writeDirectoryPath stringByAppendingPathComponent:fileName];

+ 21 - 0
Samples/Samples/TextExtract/CTextExtractViewController.h

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

+ 133 - 0
Samples/Samples/TextExtract/CTextExtractViewController.m

@@ -0,0 +1,133 @@
+//
+//  CTextExtractViewController.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 "CTextExtractViewController.h"
+
+#import <ComPDFKit/ComPDFKit.h>
+
+//-----------------------------------------------------------------------------------------
+// The sample code illustrates How to extract all text from PDF document using API.
+//-----------------------------------------------------------------------------------------
+
+@interface CTextExtractViewController ()
+
+@property (nonatomic, strong) CPDFDocument *document;
+
+@property (nonatomic, assign) BOOL isRun;
+
+@property (nonatomic, strong) NSString *commandLineStr;
+
+@property (nonatomic, strong) NSURL *bookmarkURL;
+
+@end
+
+@implementation CTextExtractViewController
+
+#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 extract all text from PDF document using API.", nil);
+    
+    self.commandLineTextView.text = @"";
+    self.isRun = NO;
+    self.commandLineStr = @"";
+}
+
+#pragma mark - Samples Methods
+
+- (void)extractPageText:(CPDFDocument *)document {
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Samples 1: Extract all text content in the specified page\n"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Opening the Samples PDF File\n"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"The text content of the first page of the document:\n"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Text:\n"];
+    
+    CPDFPage *page = [document pageAtIndex:0];
+    self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"%@\n", page.string];
+    
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"\nDone!\n"];
+}
+
+- (void)extractAllPageText:(CPDFDocument *)document {
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Samples 1: Extract all text content in the specified page\n"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Opening the Samples PDF File\n"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"The text content of the first page of the document:\n"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Text:\n"];
+    
+    for (int i = 0; i < document.pageCount; i++) {
+        CPDFPage *page = [document pageAtIndex:i];
+        self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"%@\n", page.string];
+    }
+    
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"\nDone!\n"];
+}
+
+- (void)extractRectRangeText:(CPDFDocument *)document {
+    
+}
+
+#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 *textSearchAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"   CreateBookmarkTest.pdf   ", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+            // Open CreateBookmarkTest.pdf
+        }];
+        
+        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];
+    }
+}
+
+- (IBAction)buttonItemClick_run:(id)sender {
+    self.isRun = NO;
+    
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Running Bookmark sample...\n\n"];
+    [self extractPageText:self.document];
+    [self extractAllPageText:self.document];
+    [self extractRectRangeText:self.document];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
+    
+    // Refresh commandline message
+    self.commandLineTextView.text = self.commandLineStr;
+}
+
+@end