CPDFViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 "CPDFBatesAddViewController.h"
  12. #import "CPDFSettingView.h"
  13. @interface CPDFViewController () <UIActionSheetDelegate,CPDFModelDataDelegate>
  14. @property (nonatomic,strong) CPDFKitPlatformImage *image;
  15. @property (nonatomic,strong) CPDFDocument *document;
  16. @property (nonatomic,strong) CPDFView *pdfView;
  17. @property (nonatomic,strong) CPDFSettingView *headerFooterSetting;
  18. @property (nonatomic,strong) CPDFSettingView *batesSetting;
  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. CGSize 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. }
  48. #pragma mark - CPDFViewController Actions
  49. - (void)onClickedOkbtn {
  50. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  51. // Add watermark action
  52. UIAlertAction *addWatermarkAction = [UIAlertAction actionWithTitle:@"Add Watermark" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  53. NSLog(@"addWatermarkAction");
  54. }];
  55. // Setting headerfooter action
  56. UIAlertAction *settingHeaderFooter = [UIAlertAction actionWithTitle:@"Setting Headerfooter" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  57. [self settingHeaderFooter];
  58. }];
  59. // Seeting bates action
  60. UIAlertAction *settingBatesAction = [UIAlertAction actionWithTitle:@"Setting Bates" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  61. [self settingBates];
  62. }];
  63. // Cancel action
  64. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  65. NSLog(@"Cancel Action");
  66. }];
  67. // Add controller
  68. [alertController addAction:addWatermarkAction];
  69. [alertController addAction:settingHeaderFooter];
  70. [alertController addAction:settingBatesAction];
  71. [alertController addAction:cancelAction];
  72. [self presentViewController:alertController animated:YES completion:nil];
  73. }
  74. #pragma mark - Setting Pop
  75. - (void)settingHeaderFooter {
  76. _headerFooterSetting = [[CPDFSettingView alloc] init];
  77. [_headerFooterSetting setText];
  78. [self.view addSubview:_headerFooterSetting];
  79. [_headerFooterSetting mas_makeConstraints:^(MASConstraintMaker *make) {
  80. make.bottom.equalTo(self.view.mas_bottom);
  81. make.left.equalTo(self.view.mas_left);
  82. make.right.equalTo(self.view.mas_right);
  83. make.height.mas_equalTo(100);
  84. }];
  85. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  86. self.headerFooterSetting.center = CGPointMake(self.headerFooterSetting.center.x, self.headerFooterSetting.center.y - 100);
  87. } completion:nil];
  88. [_headerFooterSetting.addButton addTarget:self action:@selector(addHeaderFooter:) forControlEvents:UIControlEventTouchUpInside];
  89. [_headerFooterSetting.deleteButton addTarget:self action:@selector(deleteHeaderFooter:) forControlEvents:UIControlEventTouchUpInside];
  90. }
  91. - (void)settingBates {
  92. _batesSetting = [[CPDFSettingView alloc] init];
  93. [_batesSetting setText];
  94. [self.view addSubview:_batesSetting];
  95. [_batesSetting mas_makeConstraints:^(MASConstraintMaker *make) {
  96. make.bottom.equalTo(self.view.mas_bottom);
  97. make.left.equalTo(self.view.mas_left);
  98. make.right.equalTo(self.view.mas_right);
  99. make.height.mas_equalTo(100);
  100. }];
  101. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  102. self.batesSetting.center = CGPointMake(self.batesSetting.center.x, self.batesSetting.center.y - 100);
  103. } completion:nil];
  104. [_batesSetting.addButton addTarget:self action:@selector(addBates:) forControlEvents:UIControlEventTouchUpInside];
  105. [_batesSetting.deleteButton addTarget:self action:@selector(deleteBates:) forControlEvents:UIControlEventTouchUpInside];
  106. }
  107. #pragma mark - Actions
  108. - (void)addHeaderFooter:(UIButton *)btn {
  109. CPDFHeaderFooterAddController *headerFooterControl = [[CPDFHeaderFooterAddController alloc] initWithImage:self.image];
  110. headerFooterControl.delegate = self;
  111. [self.navigationController pushViewController:headerFooterControl animated:NO];
  112. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  113. self.headerFooterSetting.center = CGPointMake(self.headerFooterSetting.center.x, self.headerFooterSetting.center.y + 100);
  114. } completion:nil];
  115. }
  116. - (void)deleteHeaderFooter:(UIButton *)btn {
  117. [self deleteHeaderFooterAction];
  118. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  119. self.headerFooterSetting.center = CGPointMake(self.headerFooterSetting.center.x, self.headerFooterSetting.center.y + 100);
  120. } completion:nil];
  121. }
  122. - (void)addBates:(UIButton *)btn {
  123. CPDFBatesAddViewController *batesAddControl = [[CPDFBatesAddViewController alloc] initWithImage:self.image];
  124. batesAddControl.delegate = self;
  125. [self.navigationController pushViewController:batesAddControl animated:NO];
  126. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  127. self.batesSetting.center = CGPointMake(self.batesSetting.center.x, self.batesSetting.center.y + 100);
  128. } completion:nil];
  129. }
  130. - (void)deleteBates:(UIButton *)btn {
  131. [self deleteBatesAction];
  132. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  133. self.batesSetting.center = CGPointMake(self.batesSetting.center.x, self.batesSetting.center.y + 100);
  134. } completion:nil];
  135. }
  136. #pragma mark - Delete Alert
  137. // Detele headerfootr action
  138. - (void)deleteHeaderFooterAction {
  139. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Delete Page Number" message:@"Are you sure delete all pages number" preferredStyle:UIAlertControllerStyleAlert];
  140. UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  141. [self deleteHeaderFooter];
  142. }];
  143. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  144. NSLog(@"Cancel Action");
  145. }];
  146. [alertController addAction:okAction];
  147. [alertController addAction:cancelAction];
  148. [self presentViewController:alertController animated:YES completion:nil];
  149. }
  150. // Delete bates action
  151. - (void)deleteBatesAction {
  152. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Delete Bates" message:@"Are you sure delete all Bates" preferredStyle:UIAlertControllerStyleAlert];
  153. UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  154. [self deleteBates];
  155. }];
  156. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  157. NSLog(@"Cancel Action");
  158. }];
  159. [alertController addAction:okAction];
  160. [alertController addAction:cancelAction];
  161. [self presentViewController:alertController animated:YES completion:nil];
  162. }
  163. #pragma mark - CPDFModelDataDelegate
  164. // Get headerfooter' model to setting page headerfooter's add functional property
  165. - (void)changeHeaderFooterModelData:(CPDFHeaderFooterModel *)modelData {
  166. CPDFHeaderFooter *headerFooter = [self.document headerFooter];
  167. NSString *pageIndex = [NSString stringWithFormat:@"0-%lu",self.document.pageCount - 1];
  168. NSString *text = [[NSString alloc] init];
  169. switch (modelData.fontSelcet) {
  170. case 0:
  171. text = [NSString stringWithFormat:@"<<%@>>",modelData.pageStart];
  172. break;
  173. case 1:
  174. text = [NSString stringWithFormat:@"page <<%@>>",modelData.pageStart];
  175. break;
  176. case 2:
  177. text = [NSString stringWithFormat:@"<<%@>>/%lu",modelData.pageStart,self.document.pageCount];
  178. break;
  179. case 3:
  180. text = [NSString stringWithFormat:@"<<%@>> of %lu",modelData.pageStart,self.document.pageCount];
  181. break;
  182. case 4:
  183. text = [NSString stringWithFormat:@"page <<%@>> of %lu",modelData.pageStart,self.document.pageCount];
  184. break;
  185. default:
  186. break;
  187. }
  188. headerFooter.pageString = pageIndex;
  189. [headerFooter setText:text atIndex:modelData.fontPosition];
  190. [headerFooter setFontSize:modelData.fontSize atIndex:modelData.fontPosition];
  191. [headerFooter setTextColor:modelData.fontColor atIndex:modelData.fontPosition];
  192. [headerFooter update];
  193. NSURL *url = [NSURL fileURLWithPath:self.path];
  194. [self.document writeToURL:url];
  195. _document = [[CPDFDocument alloc] initWithURL:url];
  196. _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
  197. _pdfView.document = _document;
  198. _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  199. [self.view addSubview:_pdfView];
  200. }
  201. // Get bates' model to setting page bates's add functional property
  202. - (void)changBatesModelData:(CPDFHeaderFooterModel *)modelData {
  203. CPDFHeaderFooterModel *modelBatesData = modelData;
  204. CPDFBates *bates = [self.document bates];
  205. NSString *pageIndex = [NSString stringWithFormat:@"%@-%lu",modelBatesData.pageStart,self.document.pageCount - 1];
  206. bates.pageString = pageIndex;
  207. NSString *text = [NSString stringWithFormat:@"<<%@>>",modelBatesData.fontText];
  208. [bates setText:text atIndex:modelBatesData.fontPosition];
  209. [bates setFontName:modelBatesData.fontName atIndex:modelBatesData.fontPosition];
  210. [bates setFontSize:15.0 atIndex:modelBatesData.fontPosition];
  211. [bates setTextColor:modelBatesData.fontColor atIndex:modelBatesData.fontPosition];
  212. [bates update];
  213. NSURL *url = [NSURL fileURLWithPath:self.path];
  214. [self.document writeToURL:url];
  215. _document = [[CPDFDocument alloc] initWithURL:url];
  216. _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
  217. _pdfView.document = _document;
  218. _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  219. [self.view addSubview:_pdfView];
  220. }
  221. #pragma mark - Delete Methods
  222. // Get headerfooter' model to setting page headerfooter's delete functional property
  223. - (void)deleteHeaderFooter {
  224. CPDFHeaderFooter *headerFooter = [self.document headerFooter];
  225. [headerFooter clear];
  226. NSURL *url = [NSURL fileURLWithPath:self.path];
  227. [self.document writeToURL:url];
  228. _document = [[CPDFDocument alloc] initWithURL:url];
  229. _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
  230. _pdfView.document = _document;
  231. _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  232. [self.view addSubview:_pdfView];
  233. }
  234. // Get bates' model to setting page bates's delete functional property
  235. - (void)deleteBates {
  236. CPDFBates *bates = [self.document bates];
  237. [bates clear];
  238. NSURL *url = [NSURL fileURLWithPath:self.path];
  239. [self.document writeToURL:url];
  240. _document = [[CPDFDocument alloc] initWithURL:url];
  241. _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
  242. _pdfView.document = _document;
  243. _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  244. [self.view addSubview:_pdfView];
  245. }
  246. @end