PDFViewController.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 "PDFWatermarkTextPreview.h"
  12. #import "PDFWatermarkImagePreview.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 "PDFHeaderFooterSettingView.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) PDFHeaderFooterSettingView *headerFooterSetting;
  27. @property (nonatomic,strong) PDFHeaderFooterSettingView *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 = [[PDFHeaderFooterSettingView alloc] init];
  57. [_headerFooterSetting setText];
  58. _batesSetting = [[PDFHeaderFooterSettingView 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:NSLocalizedString(@"Add Watermark",nil) 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:NSLocalizedString(@"Setting Headerfooter", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  71. [self PDFViewPerformSettingHeaderFooter];
  72. }];
  73. // Seeting bates action
  74. UIAlertAction *settingBatesAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Setting Bates",nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  75. [self PDFViewPerformSettingBates];
  76. }];
  77. UIAlertAction *addBackgroundAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Add Background", nil) 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:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:nil];
  83. [alertController addAction:addWatermarkAction];
  84. [alertController addAction:settingHeaderFooter];
  85. [alertController addAction:settingBatesAction];
  86. [alertController addAction:addBackgroundAction];
  87. [alertController addAction:cancelAction];
  88. [self presentViewController:alertController animated:YES completion:nil];
  89. }
  90. #pragma mark - PDFViewer - More :Add / Delete images HeaderFooter and Bates
  91. - (void)PDFViewPerformSettingHeaderFooter {
  92. [self.view addSubview:_headerFooterSetting];
  93. [_headerFooterSetting mas_remakeConstraints:^(MASConstraintMaker *make) {
  94. make.bottom.equalTo(self.view.mas_bottom);
  95. make.left.equalTo(self.view.mas_left);
  96. make.right.equalTo(self.view.mas_right);
  97. make.height.mas_equalTo(100);
  98. }];
  99. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  100. self.headerFooterSetting.center = CGPointMake(self.headerFooterSetting.center.x, self.headerFooterSetting.center.y - 100);
  101. } completion:nil];
  102. [_headerFooterSetting.addButton addTarget:self action:@selector(PDFViewPerformAddHeaderFooter:) forControlEvents:UIControlEventTouchUpInside];
  103. [_headerFooterSetting.deleteButton addTarget:self action:@selector(PDFViewPerformDeleteHeaderFooter:) forControlEvents:UIControlEventTouchUpInside];
  104. }
  105. - (void)PDFViewPerformSettingBates {
  106. [self.view addSubview:_batesSetting];
  107. [_batesSetting mas_remakeConstraints:^(MASConstraintMaker *make) {
  108. make.bottom.equalTo(self.view.mas_bottom);
  109. make.left.equalTo(self.view.mas_left);
  110. make.right.equalTo(self.view.mas_right);
  111. make.height.mas_equalTo(100);
  112. }];
  113. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  114. self.batesSetting.center = CGPointMake(self.batesSetting.center.x, self.batesSetting.center.y - 100);
  115. } completion:nil];
  116. [_batesSetting.addButton addTarget:self action:@selector(PDFViewPerformAddBates:) forControlEvents:UIControlEventTouchUpInside];
  117. [_batesSetting.deleteButton addTarget:self action:@selector(PDFViewPerformDeleteBates:) forControlEvents:UIControlEventTouchUpInside];
  118. }
  119. - (void)PDFViewPerformAddHeaderFooter:(UIButton *)btn {
  120. PDFHeaderFooterAddController *headerFooterControl = [[PDFHeaderFooterAddController alloc] initWithImage:self.image];
  121. headerFooterControl.delegate = self;
  122. [self.navigationController pushViewController:headerFooterControl animated:NO];
  123. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  124. self.headerFooterSetting.center = CGPointMake(self.headerFooterSetting.center.x, self.headerFooterSetting.center.y + 100);
  125. } completion:nil];
  126. }
  127. - (void)PDFViewPerformDeleteHeaderFooter:(UIButton *)btn {
  128. [self PDFViewPerformDeleteHeaderFooterAction];
  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)PDFViewPerformAddBates:(UIButton *)btn {
  134. PDFBatesAddViewController *batesAddControl = [[PDFBatesAddViewController alloc] initWithImage:self.image];
  135. batesAddControl.delegate = self;
  136. [self.navigationController pushViewController:batesAddControl animated:NO];
  137. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  138. self.batesSetting.center = CGPointMake(self.batesSetting.center.x, self.batesSetting.center.y + 100);
  139. } completion:nil];
  140. }
  141. - (void)PDFViewPerformDeleteBates:(UIButton *)btn {
  142. [self PDFViewPerformDeleteBatesAction];
  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. // Detele headerfootr action
  148. - (void)PDFViewPerformDeleteHeaderFooterAction {
  149. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Delete Page Number", nil) message:NSLocalizedString(@"Are you sure delete all pages number", nil) preferredStyle:UIAlertControllerStyleAlert];
  150. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  151. [self PDFViewPerformDeleteHeaderFooter];
  152. }];
  153. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel",nil) style:UIAlertActionStyleCancel handler:nil];
  154. [alertController addAction:okAction];
  155. [alertController addAction:cancelAction];
  156. [self presentViewController:alertController animated:YES completion:nil];
  157. }
  158. // Delete bates action
  159. - (void)PDFViewPerformDeleteBatesAction {
  160. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Delete Bates", nil) message:NSLocalizedString(@"Are you sure delete all Bates",nil) preferredStyle:UIAlertControllerStyleAlert];
  161. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  162. [self PDFViewPerformDeleteBates];
  163. }];
  164. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:nil];
  165. [alertController addAction:okAction];
  166. [alertController addAction:cancelAction];
  167. [self presentViewController:alertController animated:YES completion:nil];
  168. }
  169. #pragma mark - CPDFModelDataDelegate
  170. // Get headerfooter' model to setting page headerfooter's add functional property
  171. - (void)PDFViewPerformChangeHeaderFooter:(PDFHeaderFooterModel *)modelData {
  172. CPDFHeaderFooter *headerFooter = [self.pdfDocument headerFooter];
  173. NSString *pageIndex = [NSString stringWithFormat:@"0-%lu",self.pdfDocument.pageCount - 1];
  174. NSString *text = [[NSString alloc] init];
  175. switch (modelData.fontSelcet) {
  176. case 0:
  177. text = [NSString stringWithFormat:@"<<%@>>",modelData.pageStart];
  178. break;
  179. case 1:
  180. text = [NSString stringWithFormat:@"page <<%@>>",modelData.pageStart];
  181. break;
  182. case 2:
  183. text = [NSString stringWithFormat:@"<<%@>>/%lu",modelData.pageStart,self.pdfDocument.pageCount];
  184. break;
  185. case 3:
  186. text = [NSString stringWithFormat:@"<<%@>> of %lu",modelData.pageStart,self.pdfDocument.pageCount];
  187. break;
  188. case 4:
  189. text = [NSString stringWithFormat:@"page <<%@>> of %lu",modelData.pageStart,self.pdfDocument.pageCount];
  190. break;
  191. default:
  192. break;
  193. }
  194. headerFooter.pageString = pageIndex;
  195. [headerFooter setText:text atIndex:modelData.fontPosition];
  196. [headerFooter setFontSize:modelData.fontSize atIndex:modelData.fontPosition];
  197. [headerFooter setTextColor:modelData.fontColor atIndex:modelData.fontPosition];
  198. [headerFooter update];
  199. [self pdfViewUpdate];
  200. }
  201. // Get bates' model to setting page bates's add functional property
  202. - (void)PDFViewPerformChangBates:(PDFHeaderFooterModel *)modelData {
  203. PDFHeaderFooterModel *modelBatesData = modelData;
  204. CPDFBates *bates = [self.pdfDocument bates];
  205. NSString *pageIndex = [NSString stringWithFormat:@"%@-%lu",modelBatesData.pageStart,self.pdfDocument.pageCount - 1];
  206. bates.pageString = pageIndex;
  207. NSString *text = [NSString stringWithFormat:@"<<%@>>",modelBatesData.fontText];
  208. [bates setText:text atIndex:modelBatesData.fontPosition];
  209. [bates setFontName:modelBatesData.fontName atIndex:modelBatesData.fontPosition];
  210. [bates setFontSize:15.0 atIndex:modelBatesData.fontPosition];
  211. [bates setTextColor:modelBatesData.fontColor atIndex:modelBatesData.fontPosition];
  212. [bates update];
  213. [self pdfViewUpdate];
  214. }
  215. #pragma mark - PDF View Update
  216. - (void)pdfViewUpdate {
  217. [self.pdfView.document writeToURL:self.pdfView.document.documentURL];
  218. [self loadDocumentWithFilePath:self.pdfView.document.documentURL.path completion:nil];
  219. }
  220. - (void)pdfViewDelete {
  221. NSURL *url = [NSURL fileURLWithPath:_path];
  222. [_pdfView.document writeToURL:url];
  223. _pdfDocument = [[CPDFDocument alloc] initWithURL:url];
  224. _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
  225. _pdfView.document = _pdfDocument;
  226. _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  227. [self.view addSubview:_pdfView];
  228. }
  229. - (void)loadDocumentWithFilePath:(NSString *)filePath completion:(void (^)(BOOL result))completion {
  230. self.title = [[filePath lastPathComponent] stringByDeletingPathExtension];
  231. self.pdfView.document = nil;
  232. [self.navigationController.view setUserInteractionEnabled:NO];
  233. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  234. NSURL *url = [NSURL fileURLWithPath:self.path];
  235. CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
  236. dispatch_async(dispatch_get_main_queue(), ^{
  237. [self.navigationController.view setUserInteractionEnabled:YES];
  238. if (document.error && document.error.code != CPDFDocumentPasswordError) {
  239. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  240. [self.navigationController popViewControllerAnimated:YES];
  241. }];
  242. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
  243. message:NSLocalizedString(@"Sorry PDF Reader Can't open this pdf file!", nil)
  244. preferredStyle:UIAlertControllerStyleAlert];
  245. [alert addAction:okAction];
  246. [self presentViewController:alert animated:YES completion:nil];
  247. if (completion) {
  248. completion(NO);
  249. }
  250. } else {
  251. self.pdfView.document = document;
  252. if (completion) {
  253. completion(YES);
  254. }
  255. }
  256. });
  257. });
  258. }
  259. #pragma mark - Delete Methods
  260. // Get headerfooter' model to setting page headerfooter's delete functional property
  261. - (void)PDFViewPerformDeleteHeaderFooter {
  262. CPDFHeaderFooter *headerFooter = [self.pdfDocument headerFooter];
  263. [headerFooter clear];
  264. [self pdfViewDelete];
  265. }
  266. // Get bates' model to setting page bates's delete functional property
  267. - (void)PDFViewPerformDeleteBates {
  268. CPDFBates *bates = [self.pdfDocument bates];
  269. [bates clear];
  270. [self pdfViewDelete];
  271. }
  272. #pragma mark - WaterMarkModelDataDelegate
  273. - (void)PDFViewPerformChangeTextWatermark:(PDFDataModel *)dataModel {
  274. CPDFWatermark *textWatermark = [[CPDFWatermark alloc] initWithDocument:_pdfDocument type:CPDFWatermarkTypeText];
  275. textWatermark.text = dataModel.text;
  276. textWatermark.textColor = dataModel.textColor;
  277. textWatermark.opacity = dataModel.watermarkOpacity;
  278. textWatermark.scale = dataModel.watermarkScale * 2.5;
  279. textWatermark.isTilePage = dataModel.isTile;
  280. textWatermark.tx = dataModel.tx * (_image.size.width / 3) / self.addWaterMarkViewController.textViewController.textPreview.documentImageView.frame.size.width;
  281. textWatermark.ty = dataModel.ty * (_image.size.height / 3) / self.addWaterMarkViewController.textViewController.textPreview.documentImageView.frame.size.height;
  282. textWatermark.rotation = dataModel.watermarkRotation;
  283. if (textWatermark.isTilePage) {
  284. textWatermark.verticalSpacing = dataModel.verticalSpacing;
  285. textWatermark.horizontalSpacing = dataModel.horizontalSpacing;
  286. }
  287. if (dataModel.pageString) {
  288. textWatermark.pageString = dataModel.pageString;
  289. }
  290. [_pdfView.document addWatermark:textWatermark];
  291. [self pdfViewUpdate];
  292. }
  293. - (void)PDFViewPerformChangeImageWatermark:(PDFDataModel *)dataModel {
  294. CPDFWatermark *imageWatermark = [[CPDFWatermark alloc] initWithDocument:_pdfDocument type:CPDFWatermarkTypeImage];
  295. imageWatermark.image = dataModel.image;
  296. imageWatermark.opacity = dataModel.watermarkOpacity;
  297. imageWatermark.scale = dataModel.watermarkScale * 2;
  298. imageWatermark.isTilePage = dataModel.isTile;
  299. imageWatermark.tx = dataModel.tx * (_image.size.width / 3) / self.addWaterMarkViewController.imageViewController.imagePreview.documentImageView.frame.size.width;
  300. imageWatermark.ty = dataModel.ty * (_image.size.height / 3) / self.addWaterMarkViewController.imageViewController.imagePreview.documentImageView.frame.size.height;
  301. imageWatermark.rotation = dataModel.watermarkRotation;
  302. if (imageWatermark.isTilePage) {
  303. imageWatermark.verticalSpacing = dataModel.verticalSpacing;
  304. imageWatermark.horizontalSpacing = dataModel.horizontalSpacing;
  305. }
  306. if (dataModel.pageString) {
  307. imageWatermark.pageString = dataModel.pageString;
  308. }
  309. [_pdfView.document addWatermark:imageWatermark];
  310. [self pdfViewUpdate];
  311. }
  312. - (void)PDFViewPerformDeleteWatermark:(CPDFWatermark *)waterMark {
  313. [self.pdfDocument removeWatermark:waterMark];
  314. [self pdfViewDelete];
  315. }
  316. #pragma mark - BackgroundDataModelDelegate
  317. - (void)PDFViewPerformChangeBackground:(PDFBackgroundModel *)dataModel {
  318. CPDFBackground *pageBackground = [_pdfDocument background];
  319. pageBackground.color = dataModel.backgroundColor;
  320. pageBackground.scale = dataModel.backgroudScale;
  321. pageBackground.rotation = dataModel.backgroundRotation;
  322. pageBackground.opacity = dataModel.backgroundOpacity;
  323. pageBackground.pageString = dataModel.pageString;
  324. pageBackground.xOffset = dataModel.horizontalSpacing;
  325. pageBackground.yOffset = dataModel.verticalSpacing;
  326. pageBackground.isAllowsView = YES;
  327. pageBackground.isAllowsPrint = YES;
  328. [pageBackground setImage:dataModel.image];
  329. [pageBackground update];
  330. [self pdfViewUpdate];
  331. }
  332. - (void)PDFViewPerformDeleteBackground {
  333. CPDFBackground *pageBackground = [_pdfDocument background];
  334. [pageBackground clear];
  335. [self pdfViewDelete];
  336. }
  337. @end