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