CPDFViewController.m 9.3 KB

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