CPDFViewController.m 19 KB

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