|
@@ -0,0 +1,174 @@
|
|
|
+//
|
|
|
+// CPDFAViewController.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 "CPDFAViewController.h"
|
|
|
+
|
|
|
+#import <ComPDFKit/ComPDFKit.h>
|
|
|
+
|
|
|
+//-----------------------------------------------------------------------------------------
|
|
|
+// The sample code illustrates how to convert PDF to PDFA format,
|
|
|
+// including PDFA1a, PDFA1b using API.
|
|
|
+//-----------------------------------------------------------------------------------------
|
|
|
+
|
|
|
+@interface CPDFAViewController ()
|
|
|
+
|
|
|
+@property (nonatomic, strong) CPDFDocument *document;
|
|
|
+
|
|
|
+@property (nonatomic, assign) BOOL isRun;
|
|
|
+
|
|
|
+@property (nonatomic, strong) NSString *commandLineStr;
|
|
|
+
|
|
|
+@property (nonatomic, strong) NSURL *convertToPDFA1aURL;
|
|
|
+
|
|
|
+@property (nonatomic, strong) NSURL *convertToPDFA1bURL;
|
|
|
+
|
|
|
+@end
|
|
|
+
|
|
|
+@implementation CPDFAViewController
|
|
|
+
|
|
|
+#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 convert PDF to PDFA format, including PDFA1a, PDFA1b using API.", nil);
|
|
|
+
|
|
|
+ self.commandLineTextView.text = @"";
|
|
|
+ self.isRun = NO;
|
|
|
+ self.commandLineStr = @"";
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - Samples Methods
|
|
|
+
|
|
|
+- (void)convertToPDFA1a:(CPDFDocument *)document {
|
|
|
+ self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
|
|
|
+ self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Samples 1: Convert PDF to PDFA1a format\n"];
|
|
|
+
|
|
|
+ // Save a document in Sandbox
|
|
|
+ NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
|
|
|
+ NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"PDFA"];
|
|
|
+
|
|
|
+ // 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,@"ConvertToPDFA1aTest"];
|
|
|
+
|
|
|
+ self.convertToPDFA1aURL = [NSURL fileURLWithPath:writeFilePath];
|
|
|
+ [document writeToURL:self.convertToPDFA1aURL];
|
|
|
+
|
|
|
+ document = [[CPDFDocument alloc] initWithURL:self.convertToPDFA1aURL];
|
|
|
+
|
|
|
+ [document writePDFAToURL:self.convertToPDFA1aURL withType:CPDFTypePDFA1a];
|
|
|
+
|
|
|
+ self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Done. Results saved in ConvertToPDFA1aTest.pdf\n\n"];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)convertToPDFA1b:(CPDFDocument *)document {
|
|
|
+ self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
|
|
|
+ self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Samples 1: Convert PDF to PDFA1b format\n"];
|
|
|
+
|
|
|
+ // Save a document in Sandbox
|
|
|
+ NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
|
|
|
+ NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"PDFA"];
|
|
|
+
|
|
|
+ // 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,@"ConvertToPDFA1bTest"];
|
|
|
+
|
|
|
+ self.convertToPDFA1bURL = [NSURL fileURLWithPath:writeFilePath];
|
|
|
+ [document writeToURL:self.convertToPDFA1bURL];
|
|
|
+
|
|
|
+ document = [[CPDFDocument alloc] initWithURL:self.convertToPDFA1bURL];
|
|
|
+
|
|
|
+ [document writePDFAToURL:self.convertToPDFA1bURL withType:CPDFTypePDFA1a];
|
|
|
+
|
|
|
+ self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Done. Results saved in ConvertToPDFA1bTest.pdf\n\n"];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)openFileWithURL:(NSURL *)url {
|
|
|
+ UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:@[url] 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 *convertToPDFA1aAction = [UIAlertAction actionWithTitle:NSLocalizedString(@" ConvertToPDFA1aTest.pdf ", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
|
|
|
+ // Open CreateBookmarkTest.pdf
|
|
|
+ [self openFileWithURL:self.convertToPDFA1aURL];
|
|
|
+ }];
|
|
|
+ UIAlertAction *convertToPDFA1bAction = [UIAlertAction actionWithTitle:NSLocalizedString(@" ConvertToPDFA1bTest.pdf ", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
|
|
|
+ // Open CreateBookmarkTest.pdf
|
|
|
+ [self openFileWithURL:self.convertToPDFA1bURL];
|
|
|
+ }];
|
|
|
+
|
|
|
+ UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:nil];
|
|
|
+
|
|
|
+ [alertController addAction:convertToPDFA1aAction];
|
|
|
+ [alertController addAction:convertToPDFA1bAction];
|
|
|
+ [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 PDFA sample...\n\n"];
|
|
|
+ [self convertToPDFA1a:self.document];
|
|
|
+ [self convertToPDFA1b:self.document];
|
|
|
+ self.commandLineStr = [self.commandLineStr stringByAppendingString:@"\nDone!\n"];
|
|
|
+ self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
|
|
|
+
|
|
|
+ // Refresh commandline message
|
|
|
+ self.commandLineTextView.text = self.commandLineStr;
|
|
|
+}
|
|
|
+
|
|
|
+@end
|