CPDFViewController.m 11 KB

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