CHeaderFooterViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. //
  2. // CHeaderFooterViewController.m
  3. // Samples
  4. //
  5. // Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. #import "CHeaderFooterViewController.h"
  13. #import <ComPDFKit/ComPDFKit.h>
  14. //-----------------------------------------------------------------------------------------
  15. // The sample code illustrates how to add and remove header footer susing API.
  16. //-----------------------------------------------------------------------------------------
  17. @interface CHeaderFooterViewController ()
  18. @property (nonatomic, strong) CPDFDocument *document;
  19. @property (nonatomic, assign) BOOL isRun;
  20. @property (nonatomic, strong) NSString *commandLineStr;
  21. @property (nonatomic, strong) NSURL *addCommonHeaderFooterURL;
  22. @property (nonatomic, strong) NSURL *addPageHeaderFooterURL;
  23. @property (nonatomic, strong) NSURL *editHeaderFooterURL;
  24. @property (nonatomic, strong) NSURL *deleteHeaderFooterURL;
  25. @end
  26. @implementation CHeaderFooterViewController
  27. #pragma mark - UIViewController Methods
  28. - (void)viewDidLoad {
  29. [super viewDidLoad];
  30. // Do any additional setup after loading the view.
  31. self.explainLabel.text = NSLocalizedString(@"The sample code illustrates how to add and remove header footer using API.", nil);
  32. self.commandLineTextView.text = @"";
  33. self.isRun = NO;
  34. self.commandLineStr = @"";
  35. }
  36. #pragma mark - Samples Methods
  37. - (void)addCommonHeaderFooter:(CPDFDocument *)document {
  38. self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
  39. self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Samples 1: Insert common header footer\n"];
  40. // Save a document in Sandbox
  41. NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
  42. NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"HeaderFoooter"];
  43. NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"Samples",@"CommonFivePage"];
  44. // Copy file
  45. if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
  46. [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
  47. NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"AddCommonHeaderFooterTest"];
  48. if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
  49. [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
  50. self.addCommonHeaderFooterURL = [NSURL fileURLWithPath:writeFilePath];
  51. document = [[CPDFDocument alloc] initWithURL:self.addCommonHeaderFooterURL];
  52. CPDFHeaderFooter *headerFooter = document.headerFooter;
  53. [headerFooter setText:@"ComPDFKit" atIndex:0];
  54. [headerFooter setText:@"ComPDFKit" atIndex:1];
  55. [headerFooter setText:@"ComPDFKit" atIndex:2];
  56. [headerFooter setTextColor:[CPDFKitPlatformColor redColor] atIndex:0];
  57. [headerFooter setFontSize:14.0 atIndex:0];
  58. [headerFooter setTextColor:[CPDFKitPlatformColor redColor] atIndex:1];
  59. [headerFooter setFontSize:14.0 atIndex:1];
  60. [headerFooter setTextColor:[CPDFKitPlatformColor redColor] atIndex:2];
  61. [headerFooter setFontSize:14.0 atIndex:2];
  62. headerFooter.pageString = @"0-4";
  63. [headerFooter update];
  64. for (int i = 0; i < 3; i++) {
  65. self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Text: %@\n", [headerFooter textAtIndex:i]];
  66. self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Location: %@\n\n", [self getStringFromEnumLocation:i]];
  67. }
  68. [document writeToURL:self.addCommonHeaderFooterURL];
  69. self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Done. Results saved in AddCommonHeaderFooterTest.pdf\n"];
  70. }
  71. - (void)addPageHeaderFooter:(CPDFDocument *)document {
  72. self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
  73. self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Samples 2: Insert page header footer\n"];
  74. // Save a document in Sandbox
  75. NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
  76. NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"HeaderFoooter"];
  77. NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"Samples",@"CommonFivePage"];
  78. // Copy file
  79. if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
  80. [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
  81. NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"AddPageHeaderFooterTest"];
  82. if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
  83. [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
  84. self.addPageHeaderFooterURL = [NSURL fileURLWithPath:writeFilePath];
  85. document = [[CPDFDocument alloc] initWithURL:self.addPageHeaderFooterURL];
  86. CPDFHeaderFooter *headerFooter = document.headerFooter;
  87. [headerFooter setText:@"<<1,2>>" atIndex:0];
  88. [headerFooter setText:@"<<1,2>>" atIndex:1];
  89. [headerFooter setText:@"<<1,2>>" atIndex:2];
  90. [headerFooter setTextColor:[CPDFKitPlatformColor redColor] atIndex:0];
  91. [headerFooter setFontSize:14.0 atIndex:0];
  92. [headerFooter setTextColor:[CPDFKitPlatformColor redColor] atIndex:0];
  93. [headerFooter setFontSize:14.0 atIndex:1];
  94. [headerFooter setTextColor:[CPDFKitPlatformColor redColor] atIndex:0];
  95. [headerFooter setFontSize:14.0 atIndex:2];
  96. headerFooter.pageString = @"0-4";
  97. [headerFooter update];
  98. for (int i = 0; i < 3; i++) {
  99. self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Text: %@\n", [headerFooter textAtIndex:i]];
  100. self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Location: %@\n\n", [self getStringFromEnumLocation:i]];
  101. }
  102. [document writeToURL:self.addPageHeaderFooterURL];
  103. self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Done. Results saved in AddPageHeaderFooterTest.pdf\n"];
  104. }
  105. - (void)editHeaderFooter:(CPDFDocument *)document {
  106. self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
  107. self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Samples 3: Edit header footer\n"];
  108. // Save a document in Sandbox
  109. NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
  110. NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"HeaderFoooter"];
  111. NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"HeaderFoooter",@"AddCommonHeaderFooterTest"];
  112. // Copy file
  113. if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
  114. [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
  115. NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"EditHeaderFooterTest"];
  116. if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
  117. [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
  118. self.editHeaderFooterURL = [NSURL fileURLWithPath:writeFilePath];
  119. document = [[CPDFDocument alloc] initWithURL:self.editHeaderFooterURL];
  120. CPDFHeaderFooter *headerFooter = document.headerFooter;
  121. [headerFooter setText:@"ComPDFKit Samples" atIndex:0];
  122. [headerFooter setText:@"ComPDFKit" atIndex:1];
  123. [headerFooter setText:@"ComPDFKit" atIndex:2];
  124. [headerFooter update];
  125. for (int i = 0; i < 3; i++) {
  126. self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Text: %@\n", [headerFooter textAtIndex:i]];
  127. self.commandLineStr = [self.commandLineStr stringByAppendingFormat:@"Location: %@\n\n", [self getStringFromEnumLocation:i]];
  128. }
  129. [document writeToURL:self.editHeaderFooterURL];
  130. self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Done. Results saved in EditHeaderFooterTest.pdf\n"];
  131. }
  132. - (void)deleteHeaderFooter:(CPDFDocument *)document {
  133. self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
  134. self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Samples 4: Delete header footer\n"];
  135. // Save a document in Sandbox
  136. NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
  137. NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"HeaderFoooter"];
  138. NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@/%@.pdf", @"Documents",@"HeaderFoooter",@"AddCommonHeaderFooterTest"];
  139. // Copy file
  140. if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
  141. [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
  142. NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"DeleteHeaderFooterTest"];
  143. if ([[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
  144. [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:documentFolder] toURL:[NSURL fileURLWithPath:writeFilePath] error:nil];
  145. self.deleteHeaderFooterURL = [NSURL fileURLWithPath:writeFilePath];
  146. document = [[CPDFDocument alloc] initWithURL:self.deleteHeaderFooterURL];
  147. CPDFHeaderFooter *headerFooter = document.headerFooter;
  148. [headerFooter clear];
  149. [document writeToURL:self.deleteHeaderFooterURL];
  150. self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Done. Results saved in DeleteHeaderFooterTest.pdf\n"];
  151. }
  152. - (NSString *)getStringFromEnumLocation:(NSInteger)enums {
  153. switch (enums) {
  154. case 0:
  155. return @"Top Left";
  156. break;
  157. case 1:
  158. return @"Top Middle";
  159. break;
  160. case 2:
  161. return @"Top Right";
  162. break;
  163. case 3:
  164. return @"Button Left";
  165. break;
  166. case 4:
  167. return @"Button Middle";
  168. break;
  169. case 5:
  170. return @"Button Right";
  171. break;
  172. default:
  173. return @" ";
  174. break;
  175. }
  176. }
  177. - (void)openFileWithURL:(NSURL *)url {
  178. UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:@[url] applicationActivities:nil];
  179. activityVC.definesPresentationContext = YES;
  180. if (UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM()) {
  181. activityVC.popoverPresentationController.sourceView = self.openfileButton;
  182. activityVC.popoverPresentationController.sourceRect = self.openfileButton.bounds;
  183. }
  184. [self presentViewController:activityVC animated:YES completion:nil];
  185. activityVC.completionWithItemsHandler = ^(UIActivityType _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
  186. if (completed) {
  187. NSLog(@"Success!");
  188. } else {
  189. NSLog(@"Failed Or Canceled!");
  190. }
  191. };
  192. }
  193. #pragma mark - Action
  194. - (IBAction)buttonItemClick_openFile:(id)sender {
  195. // Determine whether to export the document
  196. if (self.isRun) {
  197. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Choose a file to open...", nil) message:@"" preferredStyle:UIAlertControllerStyleAlert];
  198. if (UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM()) {
  199. alertController.popoverPresentationController.sourceView = self.openfileButton;
  200. alertController.popoverPresentationController.sourceRect = self.openfileButton.bounds;
  201. }
  202. UIAlertAction *commonHeaderFooterAction = [UIAlertAction actionWithTitle:NSLocalizedString(@" AddCommonHeaderFooterTest.pdf ", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  203. // Open CreateBookmarkTest.pdf
  204. [self openFileWithURL:self.addCommonHeaderFooterURL];
  205. }];
  206. UIAlertAction *pageHeaderFooterAction = [UIAlertAction actionWithTitle:NSLocalizedString(@" AddPageHeaderFooterTest.pdf ", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  207. // Open CreateBookmarkTest.pdf
  208. [self openFileWithURL:self.addPageHeaderFooterURL];
  209. }];
  210. UIAlertAction *editHeaderFooterAction = [UIAlertAction actionWithTitle:NSLocalizedString(@" EditHeaderFooterTest.pdf ", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  211. // Open CreateBookmarkTest.pdf
  212. [self openFileWithURL:self.editHeaderFooterURL];
  213. }];
  214. UIAlertAction *deleteHeaderFooterAction = [UIAlertAction actionWithTitle:NSLocalizedString(@" DeleteHeaderFooterTest.pdf ", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  215. // Open CreateBookmarkTest.pdf
  216. [self openFileWithURL:self.deleteHeaderFooterURL];
  217. }];
  218. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:nil];
  219. [alertController addAction:commonHeaderFooterAction];
  220. [alertController addAction:pageHeaderFooterAction];
  221. [alertController addAction:editHeaderFooterAction];
  222. [alertController addAction:deleteHeaderFooterAction];
  223. [alertController addAction:cancelAction];
  224. [self presentViewController:alertController animated:NO completion:nil];
  225. } else {
  226. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Choose a file to open...", nil) message:@"" preferredStyle:UIAlertControllerStyleAlert];
  227. if (UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM()) {
  228. alertController.popoverPresentationController.sourceView = self.openfileButton;
  229. alertController.popoverPresentationController.sourceRect = self.openfileButton.bounds;
  230. }
  231. UIAlertAction *noAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"No files for this sample.", nil) style:UIAlertActionStyleDefault handler:nil];
  232. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:nil];
  233. [alertController addAction:noAction];
  234. [alertController addAction:cancelAction];
  235. [self presentViewController:alertController animated:NO completion:nil];
  236. }
  237. }
  238. - (IBAction)buttonItemClick_run:(id)sender {
  239. self.isRun = YES;
  240. self.commandLineStr = [self.commandLineStr stringByAppendingString:@"Running Watermark sample...\n\n"];
  241. [self addCommonHeaderFooter:self.document];
  242. [self addPageHeaderFooter:self.document];
  243. [self editHeaderFooter:self.document];
  244. [self deleteHeaderFooter:self.document];
  245. self.commandLineStr = [self.commandLineStr stringByAppendingString:@"\nDone!\n"];
  246. self.commandLineStr = [self.commandLineStr stringByAppendingString:@"-------------------------------------\n"];
  247. // Refresh commandline message
  248. self.commandLineTextView.text = self.commandLineStr;
  249. }
  250. @end