CPDFViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. //
  2. // CPDFViewController.m
  3. // PDFViewer
  4. //
  5. // Created by kdan on 2022/11/15.
  6. //
  7. #import <ComPDFKit/ComPDFKit.h>
  8. #import <ComPDFKit/CPDFHeaderFooter.h>
  9. #import "CPDFViewController.h"
  10. #import "CPDFHeaderFooterAddController.h"
  11. #import "CPDFHeaderFooterDeleteController.h"
  12. #import "CPDFBatesDeleteViewController.h"
  13. #import "CPDFBatesAddViewController.h"
  14. @interface CPDFViewController () <UIActionSheetDelegate>
  15. @property (nonatomic,strong) CPDFKitPlatformImage *image;
  16. @property (nonatomic,assign) CGSize size;
  17. @property (nonatomic,strong) CPDFDocument *document;
  18. @property (nonatomic,strong) CPDFView *pdfView;
  19. @end
  20. @implementation CPDFViewController
  21. - (instancetype)initWithPath:(NSString *)inPath {
  22. if (self = [super init]) {
  23. _path = inPath;
  24. }
  25. return self;
  26. }
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. // Do any additional setup after loading the view.
  30. // Add UIBarButtonItem in Navigation
  31. self.navigationController.toolbarHidden = YES;
  32. UIImage *btnMore = [UIImage imageNamed:@"btn_more"];
  33. UIBarButtonItem *ringhtBarItem = [[UIBarButtonItem alloc] initWithImage:btnMore style:UIBarButtonItemStylePlain target:self action:@selector(onClickedOkbtn)];
  34. self.navigationItem.rightBarButtonItem = ringhtBarItem;
  35. // Setting URL
  36. NSURL *url = [NSURL fileURLWithPath:_path];
  37. _document = [[CPDFDocument alloc] initWithURL:url];
  38. // Get document frist image
  39. CPDFPage *page = [_document pageAtIndex:0];
  40. _size = [_document pageSizeAtIndex:0];
  41. _image = [page thumbnailOfSize:_size];
  42. // Get document view
  43. _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
  44. _pdfView.document = _document;
  45. _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  46. [self.view addSubview:_pdfView];
  47. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getHeaderFooterModel:) name:@"getAddHeaderFooterModel" object:nil];
  48. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deleteHeaderFooter:) name:@"deleteHeaderFooter" object:nil];
  49. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getBatesMdel:) name:@"getAddBatesModel" object:nil];
  50. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deleteBates:) name:@"deleteBatesModel" object:nil];
  51. }
  52. #pragma mark - Actions
  53. - (void)onClickedOkbtn {
  54. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  55. // Add watermark action
  56. UIAlertAction *addWatermarkAction = [UIAlertAction actionWithTitle:@"Add Watermark" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  57. NSLog(@"addWatermarkAction");
  58. }];
  59. // Setting headerfooter action
  60. UIAlertAction *settingHeaderFooter = [UIAlertAction actionWithTitle:@"Setting Headerfooter" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  61. [self settingHeaterFooterAction];
  62. }];
  63. // Seeting bates action
  64. UIAlertAction *settingBatesAction = [UIAlertAction actionWithTitle:@"Setting Bates" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  65. [self settingBatesAction];
  66. }];
  67. // Cancel action
  68. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  69. NSLog(@"Cancel Action");
  70. }];
  71. // Add controller
  72. [alertController addAction:addWatermarkAction];
  73. [alertController addAction:settingHeaderFooter];
  74. [alertController addAction:settingBatesAction];
  75. [alertController addAction:cancelAction];
  76. [self presentViewController:alertController animated:YES completion:nil];
  77. }
  78. // Setting headerfooter action
  79. - (void)settingHeaterFooterAction {
  80. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  81. // Add headerfooter action
  82. UIAlertAction *addHeaderfooterAction = [UIAlertAction actionWithTitle:@"Add Headerfooter" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  83. CPDFHeaderFooterAddController *headerFooterControl = [[CPDFHeaderFooterAddController alloc] initWithIamge:self.image WithSize:self.size];
  84. [self.navigationController pushViewController:headerFooterControl animated:NO];
  85. }];
  86. // Delete headerfooter action
  87. UIAlertAction *deleteHeaderfooterAction = [UIAlertAction actionWithTitle:@"Delete Headerfooter" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  88. CPDFHeaderFooterDeleteController *headerFooterDeleteControl = [[CPDFHeaderFooterDeleteController alloc] init];
  89. [self addChildViewController:headerFooterDeleteControl];
  90. [self.view addSubview:headerFooterDeleteControl.view];
  91. }];
  92. // Cancel Action
  93. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  94. NSLog(@"Cancel Action");
  95. }];
  96. [alertController addAction:addHeaderfooterAction];
  97. [alertController addAction:deleteHeaderfooterAction];
  98. [alertController addAction:cancelAction];
  99. [self presentViewController:alertController animated:YES completion:nil];
  100. }
  101. // Seeting bates action
  102. - (void)settingBatesAction {
  103. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  104. UIAlertAction *addBatesAction = [UIAlertAction actionWithTitle:@"Add Bates" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  105. CPDFBatesAddViewController *batesAddControl = [[CPDFBatesAddViewController alloc] initWithIamge:self.image WithSize:self.size];
  106. [self.navigationController pushViewController:batesAddControl animated:NO];
  107. }];
  108. UIAlertAction *delereBatesAction = [UIAlertAction actionWithTitle:@"Delete Bates" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  109. CPDFBatesDeleteViewController *batesDeleteControl = [[CPDFBatesDeleteViewController alloc] init];
  110. [self addChildViewController:batesDeleteControl];
  111. [self.view addSubview:batesDeleteControl.view];
  112. }];
  113. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  114. NSLog(@"Cancel Action");
  115. }];
  116. [alertController addAction:addBatesAction];
  117. [alertController addAction:delereBatesAction];
  118. [alertController addAction:cancelAction];
  119. [self presentViewController:alertController animated:YES completion:nil];
  120. }
  121. #pragma mark - Function
  122. // Get headerfooter' model to setting page headerfooter's add functional property
  123. - (void)getHeaderFooterModel:(NSNotification *)addSign {
  124. CPDFHeaderFooterModel *modelData = addSign.userInfo[@"model"];
  125. CPDFHeaderFooter *headerFooter = [self.document headerFooter];
  126. NSString *pageIndex = [NSString stringWithFormat:@"0-%lu",self.document.pageCount - 1];
  127. NSString *text = [[NSString alloc] init];
  128. switch (modelData.fontSelcet) {
  129. case 0:
  130. text = [NSString stringWithFormat:@"<<%@>>",modelData.pageStart];
  131. break;
  132. case 1:
  133. text = [NSString stringWithFormat:@"page <<%@>>",modelData.pageStart];
  134. break;
  135. case 2:
  136. text = [NSString stringWithFormat:@"<<%@>>/%lu",modelData.pageStart,self.document.pageCount];
  137. break;
  138. case 3:
  139. text = [NSString stringWithFormat:@"<<%@>> of %lu",modelData.pageStart,self.document.pageCount];
  140. break;
  141. case 4:
  142. text = [NSString stringWithFormat:@"page <<%@>> of %lu",modelData.pageStart,self.document.pageCount];
  143. break;
  144. default:
  145. break;
  146. }
  147. headerFooter.pageString = pageIndex;
  148. [headerFooter setText:text atIndex:modelData.fontPosition];
  149. [headerFooter setFontSize:modelData.fontSize atIndex:modelData.fontPosition];
  150. [headerFooter setTextColor:modelData.fontColor atIndex:modelData.fontPosition];
  151. [headerFooter update];
  152. NSURL *url = [NSURL fileURLWithPath:self.path];
  153. [self.document writeToURL:url];
  154. _document = [[CPDFDocument alloc] initWithURL:url];
  155. _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
  156. _pdfView.document = _document;
  157. _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  158. [self.view addSubview:_pdfView];
  159. }
  160. // Get bates' model to setting page bates's add functional property
  161. - (void)getBatesMdel:(NSNotification *)addBatesSign {
  162. CPDFHeaderFooterModel *modelBatesData = addBatesSign.userInfo[@"batesModel"];
  163. CPDFBates *bates = [self.document bates];
  164. NSString *pageIndex = [NSString stringWithFormat:@"%@-%lu",modelBatesData.pageStart,self.document.pageCount - 1];
  165. bates.pageString = pageIndex;
  166. NSString *text = [NSString stringWithFormat:@"<<%@>>",modelBatesData.fontText];
  167. [bates setText:text atIndex:modelBatesData.fontPosition];
  168. [bates setFontName:modelBatesData.fontName atIndex:modelBatesData.fontPosition];
  169. [bates setFontSize:15.0 atIndex:modelBatesData.fontPosition];
  170. [bates setTextColor:modelBatesData.fontColor atIndex:modelBatesData.fontPosition];
  171. [bates update];
  172. NSURL *url = [NSURL fileURLWithPath:self.path];
  173. [self.document writeToURL:url];
  174. _document = [[CPDFDocument alloc] initWithURL:url];
  175. _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
  176. _pdfView.document = _document;
  177. _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  178. [self.view addSubview:_pdfView];
  179. }
  180. // Get headerfooter' model to setting page headerfooter's delete functional property
  181. - (void)deleteHeaderFooter:(NSNotification *)deleteSign {
  182. CPDFHeaderFooter *headerFooter = [self.document headerFooter];
  183. [headerFooter clear];
  184. NSURL *url = [NSURL fileURLWithPath:self.path];
  185. [self.document writeToURL:url];
  186. _document = [[CPDFDocument alloc] initWithURL:url];
  187. _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
  188. _pdfView.document = _document;
  189. _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  190. [self.view addSubview:_pdfView];
  191. }
  192. // Get bates' model to setting page bates's delete functional property
  193. - (void)deleteBates:(NSNotification *)deleteBates {
  194. CPDFBates *bates = [self.document bates];
  195. [bates clear];
  196. NSURL *url = [NSURL fileURLWithPath:self.path];
  197. [self.document writeToURL:url];
  198. _document = [[CPDFDocument alloc] initWithURL:url];
  199. _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
  200. _pdfView.document = _document;
  201. _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  202. [self.view addSubview:_pdfView];
  203. }
  204. @end