Browse Source

PDFSamples(iOS)- imageExtract smaples的完成

yangliuhua 1 năm trước cách đây
mục cha
commit
b650c62e46

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

@@ -24,6 +24,7 @@
 		C97699792A569C9400187038 /* CAnnotationImportExportViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C97699782A569C9400187038 /* CAnnotationImportExportViewController.m */; };
 		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 */; };
 		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 */; };
@@ -79,6 +80,8 @@
 		C976997C2A56AD3B00187038 /* CInteractiveFormsViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CInteractiveFormsViewController.m; sourceTree = "<group>"; };
 		C976997F2A579FEC00187038 /* CPDFPageViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CPDFPageViewController.h; sourceTree = "<group>"; };
 		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>"; };
 		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>"; };
@@ -208,6 +211,15 @@
 			path = PDFPage;
 			sourceTree = "<group>";
 		};
+		C97699822A57E54700187038 /* ImageExtract */ = {
+			isa = PBXGroup;
+			children = (
+				C97699832A57E56900187038 /* CImageExtractViewController.h */,
+				C97699842A57E56900187038 /* CImageExtractViewController.m */,
+			);
+			path = ImageExtract;
+			sourceTree = "<group>";
+		};
 		C9E361062A5273BC004D31BE = {
 			isa = PBXGroup;
 			children = (
@@ -246,6 +258,7 @@
 				C97699762A569C4700187038 /* AnnotationImportExport */,
 				C976997A2A56AC0300187038 /* InteractiveForms */,
 				C976997E2A579F5F00187038 /* PDFPage */,
+				C97699822A57E54700187038 /* ImageExtract */,
 				C9E3611B2A5273BC004D31BE /* Main.storyboard */,
 				C9E3611E2A5273BF004D31BE /* Assets.xcassets */,
 				C995A2E42A53FEAD00762127 /* Info.plist */,
@@ -357,6 +370,7 @@
 				C97699812A579FEC00187038 /* CPDFPageViewController.m in Sources */,
 				C97699462A540D4200187038 /* CBookmarkViewController.m in Sources */,
 				C9E3611A2A5273BC004D31BE /* CSamplesFuctionViewController.m in Sources */,
+				C97699852A57E56900187038 /* CImageExtractViewController.m in Sources */,
 				C97699612A5534D500187038 /* XMLReader.m in Sources */,
 				C9E361142A5273BC004D31BE /* AppDelegate.m in Sources */,
 				C976997D2A56AD3B00187038 /* CInteractiveFormsViewController.m in Sources */,

+ 13 - 8
Samples/Samples/Bookmark/CBookmarkViewController.m

@@ -49,14 +49,6 @@
 
 // Create bookmark and go to page that had a bookmark
 - (void)createBookmark:(CPDFDocument *)document {
-    // Add bookmark
-    [document addBookmark:@"my bookmark" forPageIndex:1];
-    
-    // Go to page
-    if ([document bookmarkForPageIndex:1]) {
-        self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Go to page 2\n"];
-    }
-      
     // Save a document in Sandbox 
     NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
     NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"Bookmark"];
@@ -67,8 +59,21 @@
         [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
     NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"CreateBookmarkTest"];
     
+    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.bookmarkURL = [NSURL fileURLWithPath:writeFilePath];
+    document = [[CPDFDocument alloc] initWithURL:self.bookmarkURL];
+    
+    // Add bookmark
+    [document addBookmark:@"my bookmark" forPageIndex:1];
+    
+    // Go to page
+    if ([document bookmarkForPageIndex:1]) {
+        self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Go to page 2\n"];
+    }
+    
     [document writeToURL:self.bookmarkURL];
      
     self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Done.\n"];

+ 7 - 0
Samples/Samples/CSamplesFuctionViewController.m

@@ -20,6 +20,7 @@
 #import "CAnnotationImportExportViewController.h"
 #import "CInteractiveFormsViewController.h"
 #import "CPDFPageViewController.h"
+#import "CImageExtractViewController.h"
 
 #import <ComPDFKit/ComPDFKit.h>
 
@@ -175,6 +176,12 @@
             [self.navigationController pushViewController:PDFPageVC animated:YES];
         }
             break;
+        case CSamplesTypeImageExtract:
+        {
+            CImageExtractViewController *imageExtractVC = [[CImageExtractViewController alloc] initWithDocument:self.document];
+            [self.navigationController pushViewController:imageExtractVC animated:YES];
+        }
+            break;
             
         default:
             break;

+ 21 - 0
Samples/Samples/ImageExtract/CImageExtractViewController.h

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

+ 150 - 0
Samples/Samples/ImageExtract/CImageExtractViewController.m

@@ -0,0 +1,150 @@
+//
+//  CImageExtractViewController.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 "CImageExtractViewController.h"
+
+#import <ComPDFKit/ComPDFKit.h>
+
+//-----------------------------------------------------------------------------------------
+// The sample code illustrates how to extract all images from PDF documents
+//-----------------------------------------------------------------------------------------
+
+@interface CImageExtractViewController ()
+
+@property (nonatomic, strong) CPDFDocument *document;
+
+@property (nonatomic, assign) BOOL isRun;
+
+@property (nonatomic, strong) NSString *commandLineStr;
+
+@property (nonatomic, strong) NSMutableArray *imageFilePaths;
+
+@property (nonatomic, strong) NSMutableArray *imageNames;
+
+@end
+
+@implementation CImageExtractViewController
+
+- (void)viewDidLoad {
+    [super viewDidLoad];
+    // Do any additional setup after loading the view.
+    
+    self.explainLabel.text = NSLocalizedString(@"The sample code illustrates how to extract all images from PDF documents using API.", nil);
+    
+    self.commandLineTextView.text = @"";
+    self.isRun = NO;
+    self.commandLineStr = @"";
+    
+    self.imageFilePaths = [NSMutableArray array];
+    self.imageNames = [NSMutableArray array];
+}
+
+#pragma mark - Samples Methods
+
+- (void)imageExtract:(CPDFDocument *)document {
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Samples 1: Extract all images in the documentn"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Opening the Samples PDF File\n"];
+    
+    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
+    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"ImageExtract"];
+    
+    // Copy file
+    if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
+        [[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];
+    
+    NSArray *fileArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:writeDirectoryPath error:nil];
+    for (NSString *fileName in fileArray) {
+        NSString *filePath = [writeDirectoryPath stringByAppendingPathComponent:fileName];
+        self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"%@\n", fileName];
+        [self.imageFilePaths addObject:filePath];
+        [self.imageNames addObject:fileName];
+    }
+}
+
+- (void)openFile:(NSString *)imageFilePath {
+    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:@[[NSURL fileURLWithPath:imageFilePath]] 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;
+        }
+        
+        for (int i = 0; i < self.imageFilePaths.count; i++) {
+            NSString *imageName = [NSString stringWithFormat:@"%@", self.imageNames[i]];
+            UIAlertAction *imageAction = [UIAlertAction actionWithTitle:NSLocalizedString(imageName, nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+                // Open CreateBookmarkTest.pdf
+                [self openFile:self.imageFilePaths[i]];
+            }];
+            [alertController addAction:imageAction];
+        }
+        
+        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:nil];
+        
+        [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 ImageExtract sample...\n\n"];
+    [self imageExtract:self.document];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"\nDone!\n"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
+    
+    // Refresh commandline message
+    self.commandLineTextView.text = self.commandLineStr;
+}
+
+@end