CPDFViewController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. //
  2. // CPDFViewController.m
  3. // PDFViewer
  4. //
  5. // Created by kdan on 2022/11/15.
  6. //
  7. #import <ComPDFKit/ComPDFKit.h>
  8. #import <ComPDFKit/CPDFWatermark.h>
  9. #import <ComPDFKit/CPDFHeaderFooter.h>
  10. #import "CPDFViewController.h"
  11. #import "CPDFTextPreview.h"
  12. #import "CPDFImagePreview.h"
  13. #import "CPDFTextView.h"
  14. #import "CPDFImageView.h"
  15. #import "CPDFDataModel.h"
  16. #import "CPDFBackgroundModel.h"
  17. #import "Masonry.h"
  18. #import "CPDFAddViewController.h"
  19. #import "CPDFBackgroundSettingViewController.h"
  20. @interface CPDFViewController () <UIActionSheetDelegate,WaterMarkModelDataDelegate,BackgroundDataModelDelegate>
  21. @property (nonatomic,strong) CPDFView *pdfView;
  22. @property (nonatomic,strong) CPDFDocument *pdfDocument;
  23. @property (nonatomic,strong) CPDFWatermark *textWatermark;
  24. @property (nonatomic,strong) CPDFWatermark *imageWatermark;
  25. @property (nonatomic,strong) CPDFAddViewController *addWaterMarkViewController;
  26. @property (nonatomic,strong) CPDFBackgroundSettingViewController * addBackgroundViewController;
  27. @property (nonatomic,strong) UIImage *image;
  28. @property (nonatomic,assign) CGSize imageSize;
  29. @end
  30. @implementation CPDFViewController
  31. - (instancetype)initWithPath:(NSString *)inPath {
  32. if (self = [super init]) {
  33. _path = inPath;
  34. }
  35. return self;
  36. }
  37. - (void)viewDidLoad {
  38. [super viewDidLoad];
  39. // Do any additional setup after loading the view.
  40. //add UIBarButtonItem in Navigation
  41. self.navigationController.toolbarHidden = YES;
  42. UIImage *btnMore = [UIImage imageNamed:@"btn_more"];
  43. UIBarButtonItem *ringhtBarItem = [[UIBarButtonItem alloc] initWithImage:btnMore style:UIBarButtonItemStylePlain target:self action:@selector(onClickedOkbtn)];
  44. self.navigationItem.rightBarButtonItem = ringhtBarItem;
  45. // Setting URL
  46. NSURL *url = [NSURL fileURLWithPath:_path];
  47. _document = [[CPDFDocument alloc] initWithURL:url];
  48. // Get document frist image
  49. CPDFPage *page = [_document pageAtIndex:0];
  50. CGSize size = [_document pageSizeAtIndex:0];
  51. _image = [page thumbnailOfSize:size];
  52. // Get document view
  53. _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
  54. _pdfView.document = _document;
  55. _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  56. [self.view addSubview:_pdfView];
  57. _headerFooterSetting = [[CPDFSettingView alloc] init];
  58. [_headerFooterSetting setText];
  59. _batesSetting = [[CPDFSettingView alloc] init];
  60. [_batesSetting setText];
  61. }
  62. #pragma mark - CPDFViewController Actions
  63. - (void)onClickedOkbtn {
  64. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  65. // Add watermark action
  66. UIAlertAction *addWatermarkAction = [UIAlertAction actionWithTitle:@"Add Watermark" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  67. self.addWaterMarkViewController = [[CPDFAddViewController alloc] initWithDocument:self.pdfDocument];
  68. self.addWaterMarkViewController.delegate = self;
  69. [self.navigationController pushViewController:self.addWaterMarkViewController animated:YES];
  70. }];
  71. // Setting headerfooter action
  72. UIAlertAction *settingHeaderFooter = [UIAlertAction actionWithTitle:@"Setting Headerfooter" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  73. [self settingHeaderFooter];
  74. }];
  75. // Seeting bates action
  76. UIAlertAction *settingBatesAction = [UIAlertAction actionWithTitle:@"Setting Bates" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  77. [self settingBates];
  78. }];
  79. // Cancel action
  80. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  81. NSLog(@"Cancel Action");
  82. }];
  83. // Add controller
  84. [alertController addAction:addWatermarkAction];
  85. [alertController addAction:settingHeaderFooter];
  86. [alertController addAction:settingBatesAction];
  87. [alertController addAction:addBackgroundAction];
  88. [alertController addAction:cancelAction];
  89. [self presentViewController:alertController animated:YES completion:nil];
  90. }
  91. #pragma mark - Setting Pop
  92. - (void)settingHeaderFooter {
  93. [self.view addSubview:_headerFooterSetting];
  94. [_headerFooterSetting mas_remakeConstraints:^(MASConstraintMaker *make) {
  95. make.bottom.equalTo(self.view.mas_bottom);
  96. make.left.equalTo(self.view.mas_left);
  97. make.right.equalTo(self.view.mas_right);
  98. make.height.mas_equalTo(100);
  99. }];
  100. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  101. self.headerFooterSetting.center = CGPointMake(self.headerFooterSetting.center.x, self.headerFooterSetting.center.y - 100);
  102. } completion:nil];
  103. [_headerFooterSetting.addButton addTarget:self action:@selector(addHeaderFooter:) forControlEvents:UIControlEventTouchUpInside];
  104. [_headerFooterSetting.deleteButton addTarget:self action:@selector(deleteHeaderFooter:) forControlEvents:UIControlEventTouchUpInside];
  105. }
  106. - (void)settingBates {
  107. [self.view addSubview:_batesSetting];
  108. [_batesSetting mas_remakeConstraints:^(MASConstraintMaker *make) {
  109. make.bottom.equalTo(self.view.mas_bottom);
  110. make.left.equalTo(self.view.mas_left);
  111. make.right.equalTo(self.view.mas_right);
  112. make.height.mas_equalTo(100);
  113. }];
  114. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  115. self.batesSetting.center = CGPointMake(self.batesSetting.center.x, self.batesSetting.center.y - 100);
  116. } completion:nil];
  117. [_batesSetting.addButton addTarget:self action:@selector(addBates:) forControlEvents:UIControlEventTouchUpInside];
  118. [_batesSetting.deleteButton addTarget:self action:@selector(deleteBates:) forControlEvents:UIControlEventTouchUpInside];
  119. }
  120. #pragma mark - Actions
  121. - (void)addHeaderFooter:(UIButton *)btn {
  122. CPDFHeaderFooterAddController *headerFooterControl = [[CPDFHeaderFooterAddController alloc] initWithImage:self.image];
  123. headerFooterControl.delegate = self;
  124. [self.navigationController pushViewController:headerFooterControl animated:NO];
  125. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  126. self.headerFooterSetting.center = CGPointMake(self.headerFooterSetting.center.x, self.headerFooterSetting.center.y + 100);
  127. } completion:nil];
  128. }
  129. - (void)deleteHeaderFooter:(UIButton *)btn {
  130. [self deleteHeaderFooterAction];
  131. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  132. self.headerFooterSetting.center = CGPointMake(self.headerFooterSetting.center.x, self.headerFooterSetting.center.y + 100);
  133. } completion:nil];
  134. }
  135. - (void)addBates:(UIButton *)btn {
  136. CPDFBatesAddViewController *batesAddControl = [[CPDFBatesAddViewController alloc] initWithImage:self.image];
  137. batesAddControl.delegate = self;
  138. [self.navigationController pushViewController:batesAddControl animated:NO];
  139. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  140. self.batesSetting.center = CGPointMake(self.batesSetting.center.x, self.batesSetting.center.y + 100);
  141. } completion:nil];
  142. }
  143. - (void)deleteBates:(UIButton *)btn {
  144. [self deleteBatesAction];
  145. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  146. self.batesSetting.center = CGPointMake(self.batesSetting.center.x, self.batesSetting.center.y + 100);
  147. } completion:nil];
  148. }
  149. #pragma mark - Delete Alert
  150. // Detele headerfootr action
  151. - (void)deleteHeaderFooterAction {
  152. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Delete Page Number" message:@"Are you sure delete all pages number" preferredStyle:UIAlertControllerStyleAlert];
  153. UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  154. [self deleteHeaderFooter];
  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. // Delete bates action
  164. - (void)deleteBatesAction {
  165. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Delete Bates" message:@"Are you sure delete all Bates" preferredStyle:UIAlertControllerStyleAlert];
  166. UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  167. [self deleteBates];
  168. }];
  169. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  170. NSLog(@"Cancel Action");
  171. }];
  172. [alertController addAction:okAction];
  173. [alertController addAction:cancelAction];
  174. [self presentViewController:alertController animated:YES completion:nil];
  175. }
  176. #pragma mark - CPDFModelDataDelegate
  177. // Get headerfooter' model to setting page headerfooter's add functional property
  178. - (void)changeHeaderFooterModelData:(CPDFHeaderFooterModel *)modelData {
  179. CPDFHeaderFooter *headerFooter = [self.document headerFooter];
  180. NSString *pageIndex = [NSString stringWithFormat:@"0-%lu",self.document.pageCount - 1];
  181. NSString *text = [[NSString alloc] init];
  182. switch (modelData.fontSelcet) {
  183. case 0:
  184. text = [NSString stringWithFormat:@"<<%@>>",modelData.pageStart];
  185. break;
  186. case 1:
  187. text = [NSString stringWithFormat:@"page <<%@>>",modelData.pageStart];
  188. break;
  189. case 2:
  190. text = [NSString stringWithFormat:@"<<%@>>/%lu",modelData.pageStart,self.document.pageCount];
  191. break;
  192. case 3:
  193. text = [NSString stringWithFormat:@"<<%@>> of %lu",modelData.pageStart,self.document.pageCount];
  194. break;
  195. case 4:
  196. text = [NSString stringWithFormat:@"page <<%@>> of %lu",modelData.pageStart,self.document.pageCount];
  197. break;
  198. default:
  199. break;
  200. }
  201. headerFooter.pageString = pageIndex;
  202. [headerFooter setText:text atIndex:modelData.fontPosition];
  203. [headerFooter setFontSize:modelData.fontSize atIndex:modelData.fontPosition];
  204. [headerFooter setTextColor:modelData.fontColor atIndex:modelData.fontPosition];
  205. [headerFooter update];
  206. NSURL *url = [NSURL fileURLWithPath:self.path];
  207. [self.document writeToURL:url];
  208. _document = [[CPDFDocument alloc] initWithURL:url];
  209. _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
  210. _pdfView.document = _document;
  211. _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  212. [self.view addSubview:_pdfView];
  213. }
  214. // Get bates' model to setting page bates's add functional property
  215. - (void)changBatesModelData:(CPDFHeaderFooterModel *)modelData {
  216. CPDFHeaderFooterModel *modelBatesData = modelData;
  217. CPDFBates *bates = [self.document bates];
  218. NSString *pageIndex = [NSString stringWithFormat:@"%@-%lu",modelBatesData.pageStart,self.document.pageCount - 1];
  219. bates.pageString = pageIndex;
  220. NSString *text = [NSString stringWithFormat:@"<<%@>>",modelBatesData.fontText];
  221. [bates setText:text atIndex:modelBatesData.fontPosition];
  222. [bates setFontName:modelBatesData.fontName atIndex:modelBatesData.fontPosition];
  223. [bates setFontSize:15.0 atIndex:modelBatesData.fontPosition];
  224. [bates setTextColor:modelBatesData.fontColor atIndex:modelBatesData.fontPosition];
  225. [bates update];
  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. #pragma mark - Delete Methods
  235. // Get headerfooter' model to setting page headerfooter's delete functional property
  236. - (void)deleteHeaderFooter {
  237. CPDFHeaderFooter *headerFooter = [self.document headerFooter];
  238. [headerFooter clear];
  239. NSURL *url = [NSURL fileURLWithPath:self.path];
  240. [self.document writeToURL:url];
  241. _document = [[CPDFDocument alloc] initWithURL:url];
  242. _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
  243. _pdfView.document = _document;
  244. _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  245. [self.view addSubview:_pdfView];
  246. }
  247. // Get bates' model to setting page bates's delete functional property
  248. - (void)deleteBates {
  249. CPDFBates *bates = [self.document bates];
  250. [bates clear];
  251. NSURL *url = [NSURL fileURLWithPath:self.path];
  252. [self.document writeToURL:url];
  253. _document = [[CPDFDocument alloc] initWithURL:url];
  254. _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
  255. _pdfView.document = _document;
  256. _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  257. [self.view addSubview:_pdfView];
  258. #pragma mark - WaterMarkModelDataDelegate
  259. - (void)changeTextModel:(CPDFDataModel *)dataModel {
  260. _textWatermark = [[CPDFWatermark alloc] initWithDocument:_pdfDocument type:CPDFWatermarkTypeText];
  261. _textWatermark.text = dataModel.text;
  262. _textWatermark.textColor = dataModel.textColor;
  263. _textWatermark.opacity = dataModel.watermarkOpacity;
  264. _textWatermark.scale = dataModel.watermarkScale * 2.5;
  265. _textWatermark.isTilePage = dataModel.isTile;
  266. _textWatermark.tx = dataModel.tx * _imageSize.width / self.addWaterMarkViewController.textViewController.textPreview.documentView.frame.size.width;
  267. _textWatermark.ty = dataModel.ty * _imageSize.height / self.addWaterMarkViewController.textViewController.textPreview.documentView.frame.size.height;
  268. _textWatermark.rotation = dataModel.watermarkRotation;
  269. if (_textWatermark.isTilePage) {
  270. _textWatermark.verticalSpacing = dataModel.verticalSpacing;
  271. _textWatermark.horizontalSpacing = dataModel.horizontalSpacing;
  272. }
  273. if (dataModel.pageString) {
  274. _textWatermark.pageString = dataModel.pageString;
  275. }
  276. [_pdfView.document addWatermark:_textWatermark];
  277. [_pdfView layoutDocumentView];
  278. NSURL *url = [NSURL fileURLWithPath:_path];
  279. [_pdfView.document writeToURL:url];
  280. }
  281. - (void)changeImageModel:(CPDFDataModel *)dataModel {
  282. _imageWatermark = [[CPDFWatermark alloc] initWithDocument:_pdfDocument type:CPDFWatermarkTypeImage];
  283. _imageWatermark.image = dataModel.image;
  284. _imageWatermark.opacity = dataModel.watermarkOpacity;
  285. _imageWatermark.scale = dataModel.watermarkScale * 2;
  286. _imageWatermark.isTilePage = dataModel.isTile;
  287. _imageWatermark.tx = dataModel.tx * _imageSize.width / self.addWaterMarkViewController.imageViewController.imagePreview.documentView.frame.size.width;
  288. _imageWatermark.ty = dataModel.ty * _imageSize.height / self.addWaterMarkViewController.imageViewController.imagePreview.documentView.frame.size.height;
  289. _imageWatermark.rotation = dataModel.watermarkRotation;
  290. if (_imageWatermark.isTilePage) {
  291. _imageWatermark.verticalSpacing = dataModel.verticalSpacing;
  292. _imageWatermark.horizontalSpacing = dataModel.horizontalSpacing;
  293. }
  294. if (dataModel.pageString) {
  295. _imageWatermark.pageString = dataModel.pageString;
  296. }
  297. [_pdfView.document addWatermark:_imageWatermark];
  298. [_pdfView layoutDocumentView];
  299. NSURL *url = [NSURL fileURLWithPath:_path];
  300. [_pdfView.document writeContentToURL:url];
  301. }
  302. - (void)deleteTextModel:(CPDFWatermark *)waterMark {
  303. [self.pdfDocument removeWatermark:waterMark];
  304. NSURL *url = [NSURL fileURLWithPath:_path];
  305. [_pdfView.document writeToURL:url];
  306. _pdfDocument = [[CPDFDocument alloc] initWithURL:url];
  307. _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
  308. _pdfView.document = _pdfDocument;
  309. _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  310. [self.view addSubview:_pdfView];
  311. }
  312. #pragma mark - BackgroundDataModelDelegate
  313. - (void)changeBackgroundModel:(CPDFBackgroundModel *)dataModel {
  314. CPDFBackground *pageBackground = [_pdfDocument background];
  315. pageBackground.color = dataModel.backgroundColor;
  316. pageBackground.scale = dataModel.backgroudScale;
  317. pageBackground.rotation = dataModel.backgroundRotation;
  318. pageBackground.opacity = dataModel.backgroundOpacity;
  319. pageBackground.pageString = dataModel.pageString;
  320. pageBackground.xOffset = dataModel.horizontalSpacing;
  321. pageBackground.yOffset = dataModel.verticalSpacing;
  322. pageBackground.isAllowsView = YES;
  323. pageBackground.isAllowsPrint = YES;
  324. [pageBackground setImage:dataModel.image];
  325. [pageBackground update];
  326. NSURL *url = [NSURL fileURLWithPath:_path];
  327. [_pdfView.document writeToURL:url];
  328. _pdfDocument = [[CPDFDocument alloc] initWithURL:url];
  329. _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
  330. _pdfView.document = _pdfDocument;
  331. _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  332. [self.view addSubview:_pdfView];
  333. }
  334. - (void)deleteBackgroundModel {
  335. CPDFBackground *pageBackground = [_pdfDocument background];
  336. [pageBackground clear];
  337. NSURL *url = [NSURL fileURLWithPath:_path];
  338. [_pdfView.document writeToURL:url];
  339. _pdfDocument = [[CPDFDocument alloc] initWithURL:url];
  340. _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
  341. _pdfView.document = _pdfDocument;
  342. _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  343. [self.view addSubview:_pdfView];
  344. }
  345. @end