CPDFViewController.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "CPDFHeaderFooterDeleteController.h"
  12. #import "CPDFBatesSettingController.h"
  13. #import "CPDFHeaderFooterSettingController.h"
  14. @interface CPDFViewController () <UIActionSheetDelegate>
  15. @property (nonatomic,strong) CPDFKitPlatformImage *image;
  16. @property (nonatomic,assign) CGSize size;
  17. @property (nonatomic,strong) CPDFHeaderFooter *headerFooter;
  18. @property (nonatomic,strong) CPDFDocument *document;
  19. @property (nonatomic,strong) CPDFView *pdfView;
  20. @property (nonatomic,strong) CPDFHeaderFooterSettingController *headefooterControl;
  21. @property (nonatomic,strong) CPDFModelData *modelData;
  22. @end
  23. @implementation CPDFViewController
  24. - (instancetype)initWithPath:(NSString *)inPath {
  25. if (self = [super init]) {
  26. _path = inPath;
  27. }
  28. return self;
  29. }
  30. - (void)viewDidLoad {
  31. [super viewDidLoad];
  32. // Do any additional setup after loading the view.
  33. //add UIBarButtonItem in Navigation
  34. self.navigationController.toolbarHidden = YES;
  35. UIImage *btnMore = [UIImage imageNamed:@"btn_more"];
  36. UIBarButtonItem *ringhtBarItem = [[UIBarButtonItem alloc] initWithImage:btnMore style:UIBarButtonItemStylePlain target:self action:@selector(onClickedOkbtn)];
  37. self.navigationItem.rightBarButtonItem = ringhtBarItem;
  38. //setting URL
  39. NSURL *url = [NSURL fileURLWithPath:_path];
  40. _document = [[CPDFDocument alloc] initWithURL:url];
  41. //get document frist image
  42. CPDFPage *page = [_document pageAtIndex:0];
  43. _size = [_document pageSizeAtIndex:0];
  44. _image = [page thumbnailOfSize:_size];
  45. //get document view
  46. _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
  47. _pdfView.document = _document;
  48. _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  49. [self.view addSubview:_pdfView];
  50. }
  51. - (void)onClickedOkbtn {
  52. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  53. //add watermark action
  54. UIAlertAction *addWatermarkAction = [UIAlertAction actionWithTitle:@"Add Watermark" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  55. NSLog(@"addWatermarkAction");
  56. }];
  57. //setting headerfooter action
  58. UIAlertAction *settingHeaderFooter = [UIAlertAction actionWithTitle:@"Setting Headerfooter" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  59. self.headefooterControl = [[CPDFHeaderFooterSettingController alloc] initWithIamge:self.image WithSize:self.size WithDocument:self.document WithView:self.pdfView];
  60. [self addChildViewController:self.headefooterControl];
  61. [self.view addSubview:self.headefooterControl.view];
  62. [self.navigationController pushViewController:self.headefooterControl animated:NO];
  63. }];
  64. //seeting bates action
  65. UIAlertAction *settingBatesAction = [UIAlertAction actionWithTitle:@"Setting Bates" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  66. CPDFBatesSettingController *batesBatesControl = [[CPDFBatesSettingController alloc] initWithIamge:self.image WithSize:self.size];
  67. [self addChildViewController:batesBatesControl];
  68. [self.view addSubview:batesBatesControl.view];
  69. [self.navigationController pushViewController:batesBatesControl animated:NO];
  70. }];
  71. //cancel action
  72. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  73. NSLog(@"Cancel Action");
  74. }];
  75. [alertController addAction:addWatermarkAction];
  76. [alertController addAction:settingHeaderFooter];
  77. [alertController addAction:settingBatesAction];
  78. [alertController addAction:cancelAction];
  79. [self presentViewController:alertController animated:YES completion:nil];
  80. self.pdfView = self.headefooterControl.pdfView;
  81. [self.view addSubview:self.pdfView];
  82. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getModel:) name:@"getSettingModel" object:nil];
  83. }
  84. - (void)getModel:(NSNotification *)text {
  85. self.modelData = self.headefooterControl.modelData;
  86. self.headerFooter = [self.document headerFooter];
  87. self.headerFooter.pageString = @"1";
  88. [self.headerFooter setText:@"1" atIndex:self.modelData.fontPosition];
  89. [self.headerFooter setTextColor:self.modelData.fontColor atIndex:self.modelData.fontPosition];
  90. [self.headerFooter update];
  91. NSURL *url = [NSURL fileURLWithPath:self.path];
  92. [self.document writeToURL:url];
  93. _document = [[CPDFDocument alloc] initWithURL:url];
  94. _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
  95. _pdfView.document = _document;
  96. _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  97. [self.view addSubview:_pdfView];
  98. }
  99. @end