Ver Fonte

PDFSamples(iOS) - DocumentInfo samples 的完成

yangliuhua há 1 ano atrás
pai
commit
86956e8d49

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

@@ -26,6 +26,7 @@
 		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 */; };
+		C976998D2A580D0400187038 /* CDocumentInfoViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C976998C2A580D0400187038 /* CDocumentInfoViewController.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 */; };
@@ -85,6 +86,8 @@
 		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>"; };
+		C976998B2A580D0400187038 /* CDocumentInfoViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CDocumentInfoViewController.h; sourceTree = "<group>"; };
+		C976998C2A580D0400187038 /* CDocumentInfoViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CDocumentInfoViewController.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>"; };
@@ -232,6 +235,15 @@
 			path = TextExtract;
 			sourceTree = "<group>";
 		};
+		C976998A2A580CBE00187038 /* DocumentInfo */ = {
+			isa = PBXGroup;
+			children = (
+				C976998B2A580D0400187038 /* CDocumentInfoViewController.h */,
+				C976998C2A580D0400187038 /* CDocumentInfoViewController.m */,
+			);
+			path = DocumentInfo;
+			sourceTree = "<group>";
+		};
 		C9E361062A5273BC004D31BE = {
 			isa = PBXGroup;
 			children = (
@@ -272,6 +284,7 @@
 				C976997E2A579F5F00187038 /* PDFPage */,
 				C97699822A57E54700187038 /* ImageExtract */,
 				C97699862A57FE5600187038 /* TextExtract */,
+				C976998A2A580CBE00187038 /* DocumentInfo */,
 				C9E3611B2A5273BC004D31BE /* Main.storyboard */,
 				C9E3611E2A5273BF004D31BE /* Assets.xcassets */,
 				C995A2E42A53FEAD00762127 /* Info.plist */,
@@ -383,6 +396,7 @@
 				C97699812A579FEC00187038 /* CPDFPageViewController.m in Sources */,
 				C97699462A540D4200187038 /* CBookmarkViewController.m in Sources */,
 				C9E3611A2A5273BC004D31BE /* CSamplesFuctionViewController.m in Sources */,
+				C976998D2A580D0400187038 /* CDocumentInfoViewController.m in Sources */,
 				C97699852A57E56900187038 /* CImageExtractViewController.m in Sources */,
 				C97699612A5534D500187038 /* XMLReader.m in Sources */,
 				C9E361142A5273BC004D31BE /* AppDelegate.m in Sources */,

+ 7 - 0
Samples/Samples/CSamplesFuctionViewController.m

@@ -22,6 +22,7 @@
 #import "CPDFPageViewController.h"
 #import "CImageExtractViewController.h"
 #import "CTextExtractViewController.h"
+#import "CDocumentInfoViewController.h"
 
 #import <ComPDFKit/ComPDFKit.h>
 
@@ -188,6 +189,12 @@
             [self.navigationController pushViewController:textExtractVC animated:YES];
         }
             break;
+        case CSamplesTypeDocumentInfo:
+        {
+            CDocumentInfoViewController *documentInfoVC = [[CDocumentInfoViewController alloc] initWithDocument:self.document];
+            [self.navigationController pushViewController:documentInfoVC animated:YES];
+        }
+            break;
             
         default:
             break;

+ 34 - 0
Samples/Samples/DocumentInfo/CDocumentInfoViewController.h

@@ -0,0 +1,34 @@
+//
+//  CDocumentInfoViewController.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"
+
+#import <ComPDFKit/ComPDFKit.h>
+
+//-----------------------------------------------------------------------------------------
+// The sample code illustrates how to create new bookmarks and use bookmark go to
+// the page using API.
+//-----------------------------------------------------------------------------------------
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface CDocumentInfoViewController : CSamplesBaseViewController
+
+@property (nonatomic, strong) CPDFDocument *document;
+
+@property (nonatomic, assign) BOOL isRun;
+
+@property (nonatomic, strong) NSString *commandLineStr;
+
+@end
+
+NS_ASSUME_NONNULL_END

+ 164 - 0
Samples/Samples/DocumentInfo/CDocumentInfoViewController.m

@@ -0,0 +1,164 @@
+//
+//  CDocumentInfoViewController.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 "CDocumentInfoViewController.h"
+
+#import <ComPDFKit/ComPDFKit.h>
+
+//-----------------------------------------------------------------------------------------
+// The sample code illustrates how to extract PDF document information such as: date using API.
+//-----------------------------------------------------------------------------------------
+
+@interface CDocumentInfoViewController ()
+
+@end
+
+@implementation CDocumentInfoViewController
+
+- (void)viewDidLoad {
+    [super viewDidLoad];
+    // Do any additional setup after loading the view.
+    self.explainLabel.text = NSLocalizedString(@"The sample code illustrates how to extract PDF document information such as: date using API.", nil);
+    
+    self.commandLineTextView.text = @"";
+    self.isRun = NO;
+    self.commandLineStr = @"";
+}
+
+#pragma mark - Samples Methods
+
+- (void)printDocumentInfo:(CPDFDocument *)document {
+    NSDictionary *documentAttributes = [document documentAttributes];
+    self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"File Name: %@\n", document.documentURL.lastPathComponent];
+    self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"File Szie: %@\n", [self fileSizeStr]];
+    self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Title: %@\n", documentAttributes[CPDFDocumentTitleAttribute]];
+    self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Author: %@\n", documentAttributes[CPDFDocumentAuthorAttribute]];
+    self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Subject: %@\n", documentAttributes[CPDFDocumentSubjectAttribute]];
+    self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Keywords: %@\n", documentAttributes[CPDFDocumentKeywordsAttribute]];
+    NSString * versionString = [NSString stringWithFormat:@"%ld.%ld",(long)document.majorVersion,(long)document.minorVersion];
+    self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Version: %@\n", versionString];
+    self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Pages: %zd\n", document.pageCount];
+    self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Creator: %@\n", documentAttributes[CPDFDocumentCreatorAttribute]];
+    
+    {
+        if (documentAttributes[CPDFDocumentCreationDateAttribute]) {
+            NSMutableString* mSting = [NSMutableString string];
+            NSString*    tstring = [NSString stringWithFormat:@"%@",documentAttributes[CPDFDocumentCreationDateAttribute]];
+            if (tstring.length >= 16) {
+                NSRange        range;
+                range.location = 2;range.length=4;
+                [mSting appendString:[tstring substringWithRange:range]];
+                range.location = 6;range.length=2;
+                [mSting appendFormat:@"-%@",[tstring substringWithRange:range]];
+                range.location = 8;range.length=2;
+                [mSting appendFormat:@"-%@",[tstring substringWithRange:range]];
+                
+                range.location = 10;range.length=2;
+                [mSting appendFormat:@" %@",[tstring substringWithRange:range]];
+                range.location = 12;range.length=2;
+                [mSting appendFormat:@":%@",[tstring substringWithRange:range]];
+                range.location = 14;range.length=2;
+                [mSting appendFormat:@":%@",[tstring substringWithRange:range]];
+                
+                self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Creation Date: %@\n", mSting];
+            }
+        }
+    }
+    
+    {
+        if (documentAttributes[CPDFDocumentModificationDateAttribute]){
+            NSMutableString* mSting = [NSMutableString string];
+            NSString*    tstring = [NSString stringWithFormat:@"%@",documentAttributes[CPDFDocumentModificationDateAttribute]];
+
+            if (tstring.length >= 16) {
+                NSRange        range;
+                range.location = 2;range.length=4;
+                [mSting appendString:[tstring substringWithRange:range]];
+                range.location = 6;range.length=2;
+                [mSting appendFormat:@"-%@",[tstring substringWithRange:range]];
+                range.location = 8;range.length=2;
+                [mSting appendFormat:@"-%@",[tstring substringWithRange:range]];
+                
+                range.location = 10;range.length=2;
+                [mSting appendFormat:@" %@",[tstring substringWithRange:range]];
+                range.location = 12;range.length=2;
+                [mSting appendFormat:@":%@",[tstring substringWithRange:range]];
+                range.location = 14;range.length=2;
+                [mSting appendFormat:@":%@",[tstring substringWithRange:range]];
+                
+                self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Modification Date: %@\n", mSting];
+            }
+        }
+    }
+    
+    self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Printing: %@\n", document.allowsPrinting ? @"true" : @"flase"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Content Copying:: %@\n", document.allowsCopying ? @"true" : @"flase"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Document Change: %@\n", document.allowsDocumentChanges ? @"true" : @"flase"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Document Assembly: %@\n", document.allowsDocumentAssembly ? @"true" : @"flase"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Commenting: %@\n", document.allowsCommenting ? @"true" : @"flase"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Filling of Form Field: %@\n", document.allowsFormFieldEntry ? @"true" : @"flase"];
+}
+
+- (NSString *)fileSizeStr {
+
+    NSFileManager *defaultManager = [NSFileManager defaultManager];
+    if (![defaultManager fileExistsAtPath:self.document.documentURL.path]) {
+        return @"";
+    }
+    
+    NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:self.document.documentURL.path error:nil];
+    float tFileSize   = [[attrib objectForKey:NSFileSize] floatValue];
+    
+    float fileSize = tFileSize / 1024;
+    float size = fileSize >= 1024 ?(fileSize < 1048576 ? fileSize/1024.0 : fileSize/1048576.0) : fileSize;
+    char  unit = fileSize >= 1024 ? (fileSize < 1048576 ? 'M':'G'):'K';
+    return [NSString stringWithFormat:@"%0.1f%c", size, unit];
+}
+
+
+#pragma mark - Action
+
+- (IBAction)buttonItemClick_openFile:(id)sender {
+    // Determine whether to export the document
+   
+    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 DocumentInfo sample...\n\n"];
+    [self printDocumentInfo:self.document];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"\nDone!\n"];
+    self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
+    
+    // Refresh commandline message
+    self.commandLineTextView.text = self.commandLineStr;
+    NSLog(@"%@", self.commandLineStr);
+}
+
+
+@end

+ 17 - 36
Samples/Samples/TextExtract/CTextExtractViewController.m

@@ -15,7 +15,7 @@
 #import <ComPDFKit/ComPDFKit.h>
 
 //-----------------------------------------------------------------------------------------
-// The sample code illustrates How to extract all text from PDF document using API.
+// The sample code illustrates how to extract all text from PDF document using API.
 //-----------------------------------------------------------------------------------------
 
 @interface CTextExtractViewController ()
@@ -26,8 +26,6 @@
 
 @property (nonatomic, strong) NSString *commandLineStr;
 
-@property (nonatomic, strong) NSURL *bookmarkURL;
-
 @end
 
 @implementation CTextExtractViewController
@@ -37,7 +35,7 @@
 - (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.explainLabel.text = NSLocalizedString(@"The sample code illustrates how to extract all text from PDF document using API.", nil);
     
     self.commandLineTextView.text = @"";
     self.isRun = NO;
@@ -82,39 +80,22 @@
 
 - (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];
+   
+    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 {