CPDFViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 "CPDFTextViewController.h"
  12. #import "CPDFImageViewController.h"
  13. #import "CPDFTextView.h"
  14. #import "CPDFTextPreview.h"
  15. #import "CPDFImageView.h"
  16. #import "CPDFImagePreview.h"
  17. #import "CPDFDataModel.h"
  18. #import "Masonry.h"
  19. @interface CPDFViewController () <UIActionSheetDelegate>
  20. @property (nonatomic,strong) CPDFTextViewController *textViewController;
  21. @property (nonatomic,strong) CPDFImageViewController *imageViewController;
  22. @property (nonatomic,strong) CPDFView *pdfView;
  23. @property (nonatomic,strong) CPDFDocument *pdfDocument;
  24. @property (nonatomic,strong) CPDFWatermark *textWatermark;
  25. @property (nonatomic,strong) CPDFWatermark *imageWatermark;
  26. @property (nonatomic,assign) CGSize imageSize;
  27. @end
  28. @implementation CPDFViewController
  29. - (instancetype)initWithPath:(NSString *)inPath {
  30. if (self = [super init]) {
  31. _path = inPath;
  32. }
  33. return self;
  34. }
  35. - (void)viewDidLoad {
  36. [super viewDidLoad];
  37. // Do any additional setup after loading the view.
  38. //add UIBarButtonItem in Navigation
  39. self.navigationController.toolbarHidden = YES;
  40. UIImage *btnMore = [UIImage imageNamed:@"btn_more"];
  41. UIBarButtonItem *ringhtBarItem = [[UIBarButtonItem alloc] initWithImage:btnMore style:UIBarButtonItemStylePlain target:self action:@selector(onClickedOkbtn)];
  42. self.navigationItem.rightBarButtonItem = ringhtBarItem;
  43. //setting URL
  44. NSURL *url = [NSURL fileURLWithPath:_path];
  45. _pdfDocument = [[CPDFDocument alloc] initWithURL:url];
  46. _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
  47. _pdfView.document = _pdfDocument;
  48. _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  49. [self.view addSubview:_pdfView];
  50. }
  51. - (void)createView {
  52. NSArray *segmentArray = @[@"Text",@"Image"];
  53. _segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentArray];
  54. [_segmentedControl addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged];
  55. _segmentedControl.selectedSegmentIndex = 0;
  56. [_segmentedControl setBackgroundColor:[UIColor whiteColor]];
  57. _textViewController = [[CPDFTextViewController alloc] init];
  58. _imageViewController = [[CPDFImageViewController alloc] init];
  59. [self addChildViewController:_textViewController];
  60. [self addChildViewController:_imageViewController];
  61. [self.view addSubview:_segmentedControl];
  62. [self.view addSubview:_textViewController.view];
  63. [self.view addSubview:_imageViewController.view];
  64. CPDFPage *pdfPage = [_pdfView.document pageAtIndex:0];
  65. _imageSize = [_pdfView.document pageSizeAtIndex:0];
  66. _textViewController.textPreview.documentView.image = [pdfPage thumbnailOfSize:_imageSize];
  67. _imageViewController.imagePreview.documentView.image = [pdfPage thumbnailOfSize:_imageSize];
  68. _imageViewController.view.hidden = YES;
  69. _pdfView.hidden = YES;
  70. _textWatermark = [[CPDFWatermark alloc] initWithDocument:_pdfDocument type:CPDFWatermarkTypeText];
  71. _imageWatermark = [[CPDFWatermark alloc] initWithDocument:_pdfDocument type:CPDFWatermarkTypeImage];
  72. }
  73. - (void)segmentedAction:(UISegmentedControl *)sender {
  74. switch(sender.selectedSegmentIndex) {
  75. case 0:
  76. _textViewController.view.hidden = NO;
  77. _imageViewController.view.hidden = YES;
  78. break;
  79. case 1:
  80. _textViewController.view.hidden = YES;
  81. _imageViewController.view.hidden = NO;
  82. break;
  83. default:
  84. break;
  85. }
  86. }
  87. - (void)addConstraint {
  88. [_segmentedControl mas_makeConstraints:^(MASConstraintMaker *make) {
  89. make.top.equalTo(self.navigationController.navigationBar.mas_bottom).offset(0);
  90. make.width.equalTo(self.view.mas_width);
  91. make.height.equalTo(@(self.view.bounds.size.height / 20));
  92. }];
  93. [_textViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
  94. make.top.equalTo(_segmentedControl.mas_bottom).offset(0);
  95. make.width.equalTo(_segmentedControl.mas_width);
  96. make.bottom.equalTo(self.view).offset(0);
  97. }];
  98. [_imageViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
  99. make.top.equalTo(_segmentedControl.mas_bottom).offset(0);
  100. make.width.equalTo(_segmentedControl.mas_width);
  101. make.bottom.equalTo(self.view).offset(0);
  102. }];
  103. [_textViewController.textPreview.documentView mas_makeConstraints:^(MASConstraintMaker *make) {
  104. make.centerX.equalTo(_textViewController.textPreview.mas_centerX);
  105. make.centerY.equalTo(_textViewController.textPreview.mas_centerY);
  106. make.width.equalTo(@(_imageSize.width / 2));
  107. make.height.equalTo(@(_imageSize.height / 2));
  108. }];
  109. [_imageViewController.imagePreview.documentView mas_makeConstraints:^(MASConstraintMaker *make) {
  110. make.centerX.equalTo(_imageViewController.imagePreview.mas_centerX);
  111. make.centerY.equalTo(_imageViewController.imagePreview.mas_centerY);
  112. make.width.equalTo(@(_imageSize.width / 2));
  113. make.height.equalTo(@(_imageSize.height / 2));
  114. }];
  115. }
  116. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  117. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  118. if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
  119. if (_imageSize.width > _imageSize.height) {
  120. [_textViewController.textPreview.documentView mas_makeConstraints:^(MASConstraintMaker *make) {
  121. make.centerX.equalTo(_textViewController.textPreview.mas_centerX);
  122. make.centerY.equalTo(_textViewController.textPreview.mas_centerY);
  123. make.width.equalTo(@(_imageSize.width / 3.1));
  124. make.height.equalTo(@(_imageSize.height / 2.6));
  125. }];
  126. [_imageViewController.imagePreview.documentView mas_makeConstraints:^(MASConstraintMaker *make) {
  127. make.centerX.equalTo(_imageViewController.imagePreview.mas_centerX);
  128. make.centerY.equalTo(_imageViewController.imagePreview.mas_centerY);
  129. make.width.equalTo(@(_imageSize.width / 3.1));
  130. make.height.equalTo(@(_imageSize.height / 2.6));
  131. }];
  132. } else {
  133. [_textViewController.textPreview.documentView mas_makeConstraints:^(MASConstraintMaker *make) {
  134. make.centerX.equalTo(_textViewController.textPreview.mas_centerX);
  135. make.centerY.equalTo(_textViewController.textPreview.mas_centerY);
  136. make.width.equalTo(@(_imageSize.width / 2.4));
  137. make.height.equalTo(@(_imageSize.height / 2.9));
  138. }];
  139. [_imageViewController.imagePreview.documentView mas_makeConstraints:^(MASConstraintMaker *make) {
  140. make.centerX.equalTo(_imageViewController.imagePreview.mas_centerX);
  141. make.centerY.equalTo(_imageViewController.imagePreview.mas_centerY);
  142. make.width.equalTo(@(_imageSize.width / 2.4));
  143. make.height.equalTo(@(_imageSize.height / 2.9));
  144. }];
  145. }
  146. } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
  147. [_textViewController.textPreview.documentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  148. make.centerX.equalTo(_textViewController.textPreview.mas_centerX);
  149. make.centerY.equalTo(_textViewController.textPreview.mas_centerY);
  150. make.width.equalTo(@(_imageSize.width / 2));
  151. make.height.equalTo(@(_imageSize.height / 2));
  152. }];
  153. [_imageViewController.imagePreview.documentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  154. make.centerX.equalTo(_imageViewController.imagePreview.mas_centerX);
  155. make.centerY.equalTo(_imageViewController.imagePreview.mas_centerY);
  156. make.width.equalTo(@(_imageSize.width / 2));
  157. make.height.equalTo(@(_imageSize.height / 2));
  158. }];
  159. }
  160. }
  161. - (void)onClickedOkbtn {
  162. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  163. UIAlertAction *addWatermarkAction = [UIAlertAction actionWithTitle:@"Add Watermark" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  164. [self createView];
  165. [self addConstraint];
  166. UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(onClickedDoneBtn)];
  167. self.navigationItem.rightBarButtonItem = doneBtn;
  168. }];
  169. UIAlertAction *addHeaderfooterAction = [UIAlertAction actionWithTitle:@"Add Headerfooter" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  170. NSLog(@"addHeaderfooterAction");
  171. }];
  172. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  173. }];
  174. [alertController addAction:addWatermarkAction];
  175. [alertController addAction:addHeaderfooterAction];
  176. [alertController addAction:cancelAction];
  177. [self presentViewController:alertController animated:YES completion:nil];
  178. }
  179. - (void)onClickedDoneBtn {
  180. self.navigationItem.rightBarButtonItem = nil;
  181. _pdfView.hidden = NO;
  182. _textViewController.view.hidden = YES;
  183. _imageViewController.view.hidden = YES;
  184. _segmentedControl.hidden = YES;
  185. if (_segmentedControl.selectedSegmentIndex == 0) {
  186. _textWatermark.text = _textViewController.dataModel.text;
  187. _textWatermark.textColor = _textViewController.dataModel.textColor;
  188. _textWatermark.opacity = _textViewController.dataModel.watermarkOpacity;
  189. _textWatermark.scale = _textViewController.dataModel.watermarkScale;
  190. _textWatermark.isTilePage = _textViewController.dataModel.isTile;
  191. _textWatermark.tx = _textViewController.dataModel.tx;
  192. _textWatermark.ty = _textViewController.dataModel.ty;
  193. _textWatermark.rotation = _textViewController.dataModel.watermarkRotation;
  194. if (_textWatermark.isTilePage) {
  195. _textWatermark.verticalSpacing = [_textViewController.textView.verticalField.text floatValue];
  196. _textWatermark.horizontalSpacing = [_textViewController.textView.horizontalField.text floatValue];
  197. }
  198. if (_textViewController.dataModel.pageString) {
  199. _textWatermark.pageString = _textViewController.dataModel.pageString;
  200. }
  201. [_pdfView.document addWatermark:_textWatermark];
  202. [_pdfView layoutDocumentView];
  203. // NSURL *url = [NSURL fileURLWithPath:_path];
  204. // [_pdfView.document writeToURL:url];
  205. } else {
  206. _imageWatermark.image = [UIImage imageNamed:_imageViewController.dataModel.imagePath];
  207. _imageWatermark.opacity = _imageViewController.dataModel.watermarkOpacity;
  208. _imageWatermark.scale = _imageViewController.dataModel.watermarkScale;
  209. _imageWatermark.isTilePage = _imageViewController.dataModel.isTile;
  210. _imageWatermark.tx = _imageViewController.dataModel.tx;
  211. _imageWatermark.ty = _imageViewController.dataModel.ty;
  212. _imageWatermark.rotation = _imageViewController.dataModel.watermarkRotation;
  213. if (_imageWatermark.isTilePage) {
  214. _imageWatermark.verticalSpacing = [_imageViewController.imageView.verticalField.text floatValue];
  215. _imageWatermark.horizontalSpacing = [_imageViewController.imageView.horizontalField.text floatValue];
  216. }
  217. if (_imageViewController.dataModel.pageString) {
  218. _imageWatermark.pageString = _imageViewController.dataModel.pageString;
  219. }
  220. [_pdfView.document addWatermark:_imageWatermark];
  221. [_pdfView layoutDocumentView];
  222. // NSURL *url = [NSURL fileURLWithPath:_path];
  223. // [_pdfView.document writeContentToURL:url];
  224. }
  225. }
  226. @end