123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- //
- // CHeaderFooterViewController.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 "CHeaderFooterViewController.h"
- #import <ComPDFKit/ComPDFKit.h>
- //-----------------------------------------------------------------------------------------
- // The sample code illustrates how to add and remove header footer susing API.
- //-----------------------------------------------------------------------------------------
- @interface CHeaderFooterViewController ()
- @property (nonatomic, strong) CPDFDocument *document;
- @property (nonatomic, assign) BOOL isRun;
- @property (nonatomic, strong) NSString *commandLineStr;
- @property (nonatomic, strong) NSURL *addCommonHeaderFooterURL;
- @property (nonatomic, strong) NSURL *addPageHeaderFooterURL;
- @property (nonatomic, strong) NSURL *editHeaderFooterURL;
- @property (nonatomic, strong) NSURL *deleteHeaderFooterURL;
- @end
- @implementation CHeaderFooterViewController
- #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 add and remove header footer using API.", nil);
-
- self.commandLineTextView.text = @"";
- self.isRun = NO;
- self.commandLineStr = @"";
- }
- #pragma mark - Samples Methods
- - (void)addCommonHeaderFooter:(CPDFDocument *)document {
- self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
- self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Samples 1: Insert common header footer\n"];
-
- // Save a document in Sandbox
- NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
- NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"HeaderFoooter"];
- NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"Samples",@"CommonFivePage"];
-
- // 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,@"AddCommonHeaderFooterTest"];
-
- if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
- [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
-
- self.addCommonHeaderFooterURL = [NSURL fileURLWithPath:writeFilePath];
- document = [[CPDFDocument alloc] initWithURL:self.addCommonHeaderFooterURL];
-
- CPDFHeaderFooter *headerFooter = document.headerFooter;
- [headerFooter setText:@"ComPDFKit" atIndex:0];
- [headerFooter setText:@"ComPDFKit" atIndex:1];
- [headerFooter setText:@"ComPDFKit" atIndex:2];
- [headerFooter setTextColor:[CPDFKitPlatformColor redColor] atIndex:0];
- [headerFooter setFontSize:14.0 atIndex:0];
- [headerFooter setTextColor:[CPDFKitPlatformColor redColor] atIndex:1];
- [headerFooter setFontSize:14.0 atIndex:1];
- [headerFooter setTextColor:[CPDFKitPlatformColor redColor] atIndex:2];
- [headerFooter setFontSize:14.0 atIndex:2];
- headerFooter.pageString = @"0-4";
-
- [headerFooter update];
-
- for (int i = 0; i < 3; i++) {
- self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Text: %@\n", [headerFooter textAtIndex:i]];
- self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Location: %@\n\n", [self getStringFromEnumLocation:i]];
- }
-
- [document writeToURL:self.addCommonHeaderFooterURL];
- self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Done. Results saved in AddCommonHeaderFooterTest.pdf\n"];
- }
- - (void)addPageHeaderFooter:(CPDFDocument *)document {
- self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
- self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Samples 2: Insert page header footer\n"];
-
- // Save a document in Sandbox
- NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
- NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"HeaderFoooter"];
- NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"Samples",@"CommonFivePage"];
-
- // 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,@"AddPageHeaderFooterTest"];
-
- if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
- [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
-
- self.addPageHeaderFooterURL = [NSURL fileURLWithPath:writeFilePath];
- document = [[CPDFDocument alloc] initWithURL:self.addPageHeaderFooterURL];
-
- CPDFHeaderFooter *headerFooter = document.headerFooter;
- [headerFooter setText:@"<<1,2>>" atIndex:0];
- [headerFooter setText:@"<<1,2>>" atIndex:1];
- [headerFooter setText:@"<<1,2>>" atIndex:2];
- [headerFooter setTextColor:[CPDFKitPlatformColor redColor] atIndex:0];
- [headerFooter setFontSize:14.0 atIndex:0];
- [headerFooter setTextColor:[CPDFKitPlatformColor redColor] atIndex:0];
- [headerFooter setFontSize:14.0 atIndex:1];
- [headerFooter setTextColor:[CPDFKitPlatformColor redColor] atIndex:0];
- [headerFooter setFontSize:14.0 atIndex:2];
- headerFooter.pageString = @"0-4";
-
- [headerFooter update];
-
- for (int i = 0; i < 3; i++) {
- self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Text: %@\n", [headerFooter textAtIndex:i]];
- self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Location: %@\n\n", [self getStringFromEnumLocation:i]];
- }
-
- [document writeToURL:self.addPageHeaderFooterURL];
- self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Done. Results saved in AddPageHeaderFooterTest.pdf\n"];
- }
- - (void)editHeaderFooter:(CPDFDocument *)document {
- self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
- self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Samples 3: Edit header footer\n"];
-
- // Save a document in Sandbox
- NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
- NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"HeaderFoooter"];
- NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"HeaderFoooter",@"AddCommonHeaderFooterTest"];
-
- // 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,@"EditHeaderFooterTest"];
-
- if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
- [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
-
- self.editHeaderFooterURL = [NSURL fileURLWithPath:writeFilePath];
- document = [[CPDFDocument alloc] initWithURL:self.editHeaderFooterURL];
-
- CPDFHeaderFooter *headerFooter = document.headerFooter;
- [headerFooter setText:@"ComPDFKit Samples" atIndex:0];
- [headerFooter setText:@"ComPDFKit" atIndex:1];
- [headerFooter setText:@"ComPDFKit" atIndex:2];
- [headerFooter update];
-
- for (int i = 0; i < 3; i++) {
- self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Text: %@\n", [headerFooter textAtIndex:i]];
- self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Location: %@\n\n", [self getStringFromEnumLocation:i]];
- }
-
- [document writeToURL:self.editHeaderFooterURL];
-
- self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Done. Results saved in EditHeaderFooterTest.pdf\n"];
- }
- - (void)deleteHeaderFooter:(CPDFDocument *)document {
- self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
- self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Samples 4: Delete header footer\n"];
-
- // Save a document in Sandbox
- NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
- NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"HeaderFoooter"];
- NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"HeaderFoooter",@"AddCommonHeaderFooterTest"];
-
- // 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,@"DeleteHeaderFooterTest"];
-
- if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
- [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
-
- self.deleteHeaderFooterURL = [NSURL fileURLWithPath:writeFilePath];
- document = [[CPDFDocument alloc] initWithURL:self.deleteHeaderFooterURL];
-
- CPDFHeaderFooter *headerFooter = document.headerFooter;
- [headerFooter clear];
-
- [document writeToURL:self.deleteHeaderFooterURL];
- self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Done. Results saved in DeleteHeaderFooterTest.pdf\n"];
- }
- - (NSString *)getStringFromEnumLocation:(NSInteger)enums {
- switch (enums) {
- case 0:
- return @"Top Left";
- break;
- case 1:
- return @"Top Middle";
- break;
- case 2:
- return @"Top Right";
- break;
- case 3:
- return @"Button Left";
- break;
- case 4:
- return @"Button Middle";
- break;
- case 5:
- return @"Button Right";
- break;
- default:
- return @" ";
- break;
- }
- }
- - (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 *commonHeaderFooterAction = [UIAlertAction actionWithTitle:NSLocalizedString(@" AddCommonHeaderFooterTest.pdf ", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- // Open CreateBookmarkTest.pdf
- [self openFileWithURL:self.addCommonHeaderFooterURL];
- }];
- UIAlertAction *pageHeaderFooterAction = [UIAlertAction actionWithTitle:NSLocalizedString(@" AddPageHeaderFooterTest.pdf ", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- // Open CreateBookmarkTest.pdf
- [self openFileWithURL:self.addPageHeaderFooterURL];
- }];
- UIAlertAction *editHeaderFooterAction = [UIAlertAction actionWithTitle:NSLocalizedString(@" EditHeaderFooterTest.pdf ", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- // Open CreateBookmarkTest.pdf
- [self openFileWithURL:self.editHeaderFooterURL];
- }];
- UIAlertAction *deleteHeaderFooterAction = [UIAlertAction actionWithTitle:NSLocalizedString(@" DeleteHeaderFooterTest.pdf ", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- // Open CreateBookmarkTest.pdf
- [self openFileWithURL:self.deleteHeaderFooterURL];
- }];
-
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:nil];
-
- [alertController addAction:commonHeaderFooterAction];
- [alertController addAction:pageHeaderFooterAction];
- [alertController addAction:editHeaderFooterAction];
- [alertController addAction:deleteHeaderFooterAction];
- [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 Watermark sample...\n\n"];
- [self addCommonHeaderFooter:self.document];
- [self addPageHeaderFooter:self.document];
- [self editHeaderFooter:self.document];
- [self deleteHeaderFooter:self.document];
- self.commandLineStr = [self.commandLineStr stringByAppendingString:@"\nDone!\n"];
- self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
-
- // Refresh commandline message
- self.commandLineTextView.text = self.commandLineStr;
- }
- @end
|