CPDFViewController.m 12 KB

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