PDFAddWaterMarkViewController.m 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. //
  2. // CPDFAddViewController.m
  3. // PDFViewer
  4. //
  5. // Created by kdanmobile_2 on 2022/12/15.
  6. //
  7. #import "PDFAddWaterMarkViewController.h"
  8. #import <ComPDFKit/ComPDFKit.h>
  9. #import <ComPDFKit/CPDFWatermark.h>
  10. #import "PDFViewController.h"
  11. #import "PDFWatermarkTextView.h"
  12. #import "PDFWatermarkTextPreview.h"
  13. #import "PDFWatermarkImageView.h"
  14. #import "PDFWatermarkImagePreview.h"
  15. #import "PDFWatermarkDataModel.h"
  16. #import "PDFWatermarkEditView.h"
  17. #import "PDFWatermarkDrawView.h"
  18. #import "PDFWatermarkClipView.h"
  19. #import "Masonry.h"
  20. @interface PDFAddWaterMarkViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>
  21. @property (nonatomic,assign) CGSize imageSize;
  22. @property (nonatomic,strong) UIImage *image;
  23. @property (nonatomic,strong) PDFWatermarkDataModel *dataModel;
  24. @property (nonatomic,strong) NSMutableArray *dataArray;
  25. @property (nonatomic,strong) CPDFDocument *document;
  26. @property (nonatomic,strong) UIAlertController *alertController;
  27. @property (nonatomic,assign) CGAffineTransform transform1;
  28. @property (nonatomic,assign) CGAffineTransform transform2;
  29. @property (nonatomic,assign) CGRect normalWatermarkRect;
  30. @property (nonatomic,strong) UILabel *shadowWaterLabel;
  31. @property (nonatomic,assign) CGRect normalImageWatermarkRect;
  32. @property (nonatomic,strong) UILabel *shadowImageWaterLabel;
  33. @property (retain, nonatomic) IBOutlet PDFWatermarkEditView *editView;
  34. @property (retain, nonatomic) IBOutlet UITableView *editTableView;
  35. @property (retain, nonatomic) IBOutlet UISegmentedControl *segmentedControl;
  36. @property (retain, nonatomic) IBOutlet UIImageView *textDocumentView;
  37. @property (retain, nonatomic) IBOutlet UIImageView *imageDocumentView;
  38. @property (retain, nonatomic) IBOutlet UIButton *colorBtn1;
  39. @property (retain, nonatomic) IBOutlet UIButton *colorBtn2;
  40. @property (retain, nonatomic) IBOutlet UIButton *colorBtn3;
  41. @property (retain, nonatomic) IBOutlet UIButton *colorBtn4;
  42. @property (retain, nonatomic) IBOutlet UIButton *colorBtn5;
  43. @property (retain, nonatomic) IBOutlet UIButton *colorBtn6;
  44. @property (retain, nonatomic) IBOutlet UIButton *colorBtn7;
  45. @property (retain, nonatomic) IBOutlet UIButton *colorBtn8;
  46. @property (retain, nonatomic) IBOutlet UILabel *preLabel;
  47. @property (retain, nonatomic) IBOutlet UISlider *textOpacitySlider;
  48. @property (retain, nonatomic) IBOutlet UISlider *textScaleSlider;
  49. @property (retain, nonatomic) IBOutlet UIButton *textPageBtn;
  50. @property (retain, nonatomic) IBOutlet UISwitch *textTitlSwitch;
  51. @property (retain, nonatomic) IBOutlet UITextField *textVerticalField;
  52. @property (retain, nonatomic) IBOutlet UITextField *textHorizontalField;
  53. @property (retain, nonatomic) IBOutlet UIButton *textRotationBtn;
  54. @property (retain, nonatomic) IBOutlet UIView *textPreview;
  55. @property (retain, nonatomic) IBOutlet PDFWatermarkClipView *textClipView;
  56. @property (retain, nonatomic) IBOutlet PDFWatermarkDrawView *textDrawView;
  57. @property (retain, nonatomic) IBOutlet PDFWatermarkDrawView *imageDrawView;
  58. @property (retain, nonatomic) IBOutlet PDFWatermarkClipView *imageClipView;
  59. @property (retain, nonatomic) IBOutlet UIView *imagePreView;
  60. @property (retain, nonatomic) IBOutlet UIImageView *preImage;
  61. @property (retain, nonatomic) IBOutlet UISlider *imageOpacitySlider;
  62. @property (retain, nonatomic) IBOutlet UISlider *imageScaleSlider;
  63. @property (retain, nonatomic) IBOutlet UIButton *imagePageBtn;
  64. @property (retain, nonatomic) IBOutlet UISwitch *imageTitlSwitch;
  65. @property (retain, nonatomic) IBOutlet UITextField *imageVerticalField;
  66. @property (retain, nonatomic) IBOutlet UITextField *imageHorizontalField;
  67. @property (retain, nonatomic) IBOutlet UIButton *imageSelect;
  68. @property (retain, nonatomic) IBOutlet UIButton *imageRotationBtn;
  69. @property (retain, nonatomic) IBOutlet UIView *imageView;
  70. @property (retain, nonatomic) IBOutlet UIView *textView;
  71. @end
  72. @implementation PDFAddWaterMarkViewController
  73. #pragma mark - UIViewController Methods
  74. - (void)viewDidLoad {
  75. [super viewDidLoad];
  76. // Do any additional setup after loading the view.
  77. UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(onClickedDoneBtn)];
  78. UIBarButtonItem *editBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editClick)];
  79. UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelClick)];
  80. self.navigationItem.rightBarButtonItems = @[doneBtn,editBtn,cancelBtn];
  81. _textDataModel = [[PDFWatermarkDataModel alloc] init];
  82. _imagedataModel = [[PDFWatermarkDataModel alloc] init];
  83. [self createView];
  84. [self initDataModel];
  85. _shadowWaterLabel = [[UILabel alloc] init];
  86. _shadowWaterLabel.text = _preLabel.text;
  87. _shadowImageWaterLabel = [[UILabel alloc] init];
  88. // [self addConstraint];
  89. }
  90. - (void)viewDidLayoutSubviews {
  91. [super viewDidLayoutSubviews];
  92. _textHorizontalField.delegate = self;
  93. _textVerticalField.delegate = self;
  94. _imageHorizontalField.delegate = self;
  95. _imageVerticalField.delegate = self;
  96. [self createGestureRecognizer];
  97. [self createImageGestureRecognizer];
  98. }
  99. #pragma mark - Initializers
  100. - (void)initDataModel {
  101. self.textDataModel.textColor = UIColor.blackColor;
  102. self.textDataModel.watermarkOpacity = 1;
  103. self.textDataModel.text = @"Watermark";
  104. self.textDataModel.watermarkScale = 17.0 / 24.0;
  105. self.textDataModel.isTile = NO;
  106. self.textDataModel.watermarkRotation = 0;
  107. self.textDataModel.verticalSpacing = 100;
  108. self.textDataModel.horizontalSpacing = 100;
  109. self.imagedataModel.textColor = UIColor.blackColor;
  110. self.imagedataModel.watermarkOpacity = 1;
  111. self.imagedataModel.watermarkScale = 17.0 / 24.0;
  112. self.imagedataModel.isTile = NO;
  113. self.imagedataModel.watermarkRotation = 0;
  114. self.imagedataModel.verticalSpacing = 100;
  115. self.imagedataModel.horizontalSpacing = 100;
  116. self.imagedataModel.imagePath = @"btn_selected";
  117. self.imagedataModel.image = [UIImage imageNamed:self.imagedataModel.imagePath];
  118. }
  119. - (instancetype)initWithDocument:(CPDFDocument *)document {
  120. self = [super init];
  121. if (self) {
  122. _document = document;
  123. CPDFPage *pdfPage = [document pageAtIndex:0];
  124. CGSize imageSize = [document pageSizeAtIndex:0];
  125. _image = [pdfPage thumbnailOfSize:imageSize];
  126. _imageSize = _image.size;
  127. }
  128. return self;
  129. }
  130. - (void)createView {
  131. [_segmentedControl addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged];
  132. _imageDocumentView.image = _image;
  133. _textDocumentView.image = _image;
  134. [_colorBtn1 setTitle:@" " forState:UIControlStateNormal];
  135. [_colorBtn2 setTitle:@" " forState:UIControlStateNormal];
  136. [_colorBtn3 setTitle:@" " forState:UIControlStateNormal];
  137. [_colorBtn4 setTitle:@" " forState:UIControlStateNormal];
  138. [_colorBtn5 setTitle:@" " forState:UIControlStateNormal];
  139. [_colorBtn6 setTitle:@" " forState:UIControlStateNormal];
  140. [_colorBtn7 setTitle:@" " forState:UIControlStateNormal];
  141. [_colorBtn8 setTitle:@" " forState:UIControlStateNormal];
  142. [_textRotationBtn setTitle:@" " forState:UIControlStateNormal];
  143. [_imageRotationBtn setTitle:@" " forState:UIControlStateNormal];
  144. // _editView = [[PDFWatermarkEditView alloc] init];
  145. _editTableView.dataSource = self;
  146. _editTableView.delegate = self;
  147. _editTableView.backgroundColor = [UIColor systemGrayColor];
  148. _editTableView.rowHeight = 50;
  149. _editTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  150. _editTableView.tableFooterView = [[UIView alloc] init];
  151. [_editTableView setEditing:YES animated:YES];
  152. _editTableView.allowsMultipleSelection = NO;
  153. _editTableView.allowsSelectionDuringEditing = NO;
  154. _editTableView.allowsMultipleSelectionDuringEditing = NO;
  155. _imageViewController.hidden = YES;
  156. _textVerticalField.enabled = NO;
  157. _textHorizontalField.enabled = NO;
  158. _imageVerticalField.enabled = NO;
  159. _imageHorizontalField.enabled = NO;
  160. _textHorizontalField.text = @"100";
  161. _textVerticalField.text = @"100";
  162. _imageVerticalField.text = @"100";
  163. _imageHorizontalField.text = @"100";
  164. _alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Watermark Content", nil) message:nil preferredStyle:UIAlertControllerStyleAlert];
  165. __block PDFAddWaterMarkViewController *strongBlock = self;
  166. [_alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  167. }];
  168. [_alertController addAction:[UIAlertAction actionWithTitle:@"Done" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  169. strongBlock.textDataModel.text = strongBlock.alertController.textFields.firstObject.text;
  170. strongBlock.preLabel.text = strongBlock.alertController.textFields.firstObject.text;
  171. [strongBlock.preLabel sizeToFit];
  172. }]];
  173. [_alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  174. strongBlock.alertController.view.hidden = YES;
  175. }]];
  176. }
  177. #pragma mark - UI Functions
  178. - (IBAction)onColorBtnClicked:(UIButton *)sender {
  179. self.preLabel.textColor = sender.backgroundColor;
  180. [self.textDataModel setTextColor:sender.backgroundColor];
  181. }
  182. - (IBAction)onImageOpacityChanged:(UISlider *)sender {
  183. _preImage.alpha = 1 - sender.value;
  184. [self.textDataModel setWatermarkOpacity:1 - sender.value];
  185. }
  186. - (IBAction)onOpacityChanged:(UISlider *)sender {
  187. _preLabel.alpha = 1 - sender.value;
  188. [self.textDataModel setWatermarkOpacity:1 - sender.value];
  189. }
  190. - (IBAction)onImageScaleChanged:(UISlider *)sender {
  191. sender.minimumValue = 0.5;
  192. sender.maximumValue = 2;
  193. if (self.imagedataModel.watermarkRotation) {
  194. _preImage.transform = CGAffineTransformScale(self.transform1, sender.value, sender.value);
  195. } else {
  196. _preImage.transform = CGAffineTransformMakeScale(sender.value, sender.value);
  197. }
  198. _transform2 = CGAffineTransformMakeScale(sender.value, sender.value);
  199. _shadowImageWaterLabel.transform = CGAffineTransformMakeScale(sender.value, sender.value);
  200. [self.imagedataModel setWatermarkScale:sender.value];
  201. _normalImageWatermarkRect = _shadowImageWaterLabel.frame;
  202. }
  203. - (IBAction)onTileSwitchChanged:(UISwitch *)sender {
  204. if ([sender isOn]) {
  205. self.textDataModel.isTile = YES;
  206. _textHorizontalField.enabled = YES;
  207. _textVerticalField
  208. .enabled = YES;
  209. _textScaleSlider.enabled = NO;
  210. _textDrawView.hidden = NO;
  211. _textClipView.hidden = NO;
  212. if (_shadowWaterLabel.frame.size.height == 0) {
  213. _shadowWaterLabel.frame = _preLabel.frame;
  214. }
  215. _normalWatermarkRect = _shadowWaterLabel.frame;
  216. [_textDrawView setDataModel:self.textDataModel];
  217. [_textDrawView setWaterLabelRect:self.normalWatermarkRect];
  218. [_textDrawView setDocumentViewRect:self.textDocumentView.frame];
  219. [_textClipView setDocumentRect:_textDocumentView.frame];
  220. [_textDrawView setNeedsDisplay];
  221. [_textClipView setNeedsDisplay];
  222. } else {
  223. self.textDataModel.isTile = NO;
  224. _textHorizontalField.enabled = NO;
  225. _textVerticalField.enabled = NO;
  226. _textScaleSlider.enabled = YES;
  227. _textDrawView.hidden = YES;
  228. _textClipView.hidden = YES;
  229. }
  230. }
  231. - (IBAction)onImageTileSwitchChanged:(UISwitch *)sender {
  232. if ([sender isOn]) {
  233. self.imagedataModel.isTile = YES;
  234. _imageHorizontalField.enabled = YES;
  235. _imageVerticalField.enabled = YES;
  236. _imageScaleSlider.enabled = NO;
  237. // _drawView = [[PDFWatermarkDrawView alloc] initWithFrame:self.imagePreview.bounds];
  238. _imageDrawView.hidden = NO;
  239. _imageClipView.hidden = NO;
  240. if (_shadowImageWaterLabel.frame.size.height == 0) {
  241. _shadowImageWaterLabel.frame = _preImage.frame;
  242. _normalImageWatermarkRect = _shadowImageWaterLabel.frame;
  243. }
  244. [_imageDrawView setDataModel:self.imagedataModel];
  245. [_imageDrawView setDocumentViewRect:self.imageDocumentView.frame];
  246. [_imageDrawView setWaterLabelRect:self.normalImageWatermarkRect];
  247. [_imageClipView setDocumentRect:_imageDocumentView.frame];
  248. [_imageDrawView setNeedsDisplay];
  249. [_imageClipView setNeedsDisplay];
  250. } else {
  251. self.imagedataModel.isTile = NO;
  252. _imageHorizontalField.enabled = NO;
  253. _imageVerticalField.enabled = NO;
  254. _imageScaleSlider.enabled = YES;
  255. _imageDrawView.hidden = YES;
  256. _imageClipView.hidden = YES;
  257. }
  258. }
  259. - (IBAction)onTextScaleChanged:(UISlider *)sender {
  260. if (!self.textDataModel.isTile) {
  261. sender.minimumValue = 12;
  262. sender.maximumValue = 36;
  263. _preLabel.font = [_preLabel.font fontWithSize:sender.value];
  264. self.shadowWaterLabel.font = [_preLabel.font fontWithSize:sender.value];
  265. self.shadowWaterLabel.center = _preLabel.center;
  266. [_preLabel sizeToFit];
  267. [self.shadowWaterLabel sizeToFit];
  268. [self.textDataModel setWatermarkScale:sender.value / 24];
  269. }
  270. self.normalWatermarkRect = self.shadowWaterLabel.frame;
  271. }
  272. - (IBAction)horizontalChange:(UITextField *)sender {
  273. [self.textDrawView setHorizontal:[sender.text floatValue]];
  274. self.textDataModel.horizontalSpacing = [sender.text floatValue];
  275. [self.textDrawView setNeedsDisplay];
  276. }
  277. - (IBAction)verticalChage:(UITextField *)sender {
  278. [self.textDrawView setVertical:[sender.text floatValue]];
  279. self.textDataModel.verticalSpacing = [sender.text floatValue];
  280. [self.textDrawView setNeedsDisplay];
  281. }
  282. - (IBAction)imageHorizontalChange:(UITextField *)sender {
  283. [_imageDrawView setHorizontal:[sender.text floatValue]];
  284. self.imagedataModel.horizontalSpacing = [sender.text floatValue];
  285. [_imageDrawView setNeedsDisplay];
  286. }
  287. - (IBAction)imageVerticalChage:(UITextField *)sender {
  288. [_imageDrawView setVertical:[sender.text floatValue]];
  289. self.imagedataModel.verticalSpacing = [sender.text floatValue];
  290. [_imageDrawView setNeedsDisplay];
  291. }
  292. - (IBAction)onSelectPageRange:(UIButton *)sender {
  293. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  294. UIAlertAction *defaultRange = [UIAlertAction actionWithTitle:NSLocalizedString(@"All Pages", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  295. }];
  296. UIAlertAction *customRange = [UIAlertAction actionWithTitle:NSLocalizedString(@"Custom Page Range", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  297. [self createCustomRangeAlert];
  298. }];
  299. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  300. }];
  301. [alertController addAction:defaultRange];
  302. [alertController addAction:customRange];
  303. [alertController addAction:cancelAction];
  304. [self presentViewController:alertController animated:YES completion:nil];
  305. }
  306. - (void)createCustomRangeAlert {
  307. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Custom Page Range", nil) message:nil preferredStyle:UIAlertControllerStyleAlert];
  308. [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  309. textField.placeholder = NSLocalizedString(@"such as:1,3-5,10", nil);
  310. }];
  311. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Done", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  312. self.imagedataModel.pageString = alertController.textFields.firstObject.text;
  313. self.textDataModel.pageString = alertController.textFields.firstObject.text;
  314. }]];
  315. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  316. }]];
  317. [self presentViewController:alertController animated:YES completion:nil];
  318. }
  319. - (IBAction)onSelectBtnClicked:(UIButton *)sender {
  320. UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
  321. imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  322. imagePicker.delegate = self;
  323. imagePicker.allowsEditing = YES;
  324. [self presentViewController:imagePicker animated:YES completion:nil];
  325. }
  326. #pragma mark - Gesture
  327. - (void)createGestureRecognizer {
  328. [_textDocumentView setUserInteractionEnabled:YES];
  329. [_preLabel setUserInteractionEnabled:YES];
  330. [_textRotationBtn setUserInteractionEnabled:YES];
  331. UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapWatermarkLabel:)];
  332. [_preLabel addGestureRecognizer:tapGestureRecognizer];
  333. UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panWatermarkLabel:)];
  334. [_preLabel addGestureRecognizer:panRecognizer];
  335. UIPanGestureRecognizer *panRotationBtnRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(rotationWatermarkLabel:)];
  336. [_textRotationBtn addGestureRecognizer:panRotationBtnRecognizer];
  337. }
  338. - (void)tapWatermarkLabel:(UITapGestureRecognizer *)recognizer {
  339. _alertController.view.hidden = NO;
  340. _alertController.textFields.firstObject.text = self.preLabel.text;
  341. [self presentViewController:_alertController animated:true completion:nil];
  342. }
  343. - (void)panWatermarkLabel:(UIPanGestureRecognizer *)recognizer {
  344. CGPoint point = [recognizer translationInView:_textPreview];
  345. CGRect documentFrame = _textPreview.frame;
  346. documentFrame.origin.x -= _textPreview.frame.origin.x;
  347. documentFrame.origin.y -= _textPreview.frame.origin.y;
  348. [_preLabel setCenter:CGPointMake(_preLabel.center.x + point.x, _preLabel.center.y + point.y)];
  349. [_textRotationBtn setCenter:CGPointMake(_textRotationBtn.center.x + point.x, _textRotationBtn.center.y + point.y)];
  350. if (!CGRectContainsRect(documentFrame,_preLabel.frame)) {
  351. [_preLabel setCenter:CGPointMake(_preLabel.center.x - point.x, _preLabel.center.y - point.y)];
  352. [_textRotationBtn setCenter:CGPointMake(_textRotationBtn.center.x - point.x, _textRotationBtn.center.y - point.y)];
  353. }
  354. self.textDataModel.tx = _preLabel.center.x - (_textDocumentView.center.x - _textDocumentView.frame.origin.x);
  355. self.textDataModel.ty = _textDocumentView.center.y - _textDocumentView.frame.origin.y - _preLabel.center.y;
  356. [recognizer setTranslation:CGPointZero inView:_textPreview];
  357. self.shadowWaterLabel.center = _preLabel.center;
  358. //
  359. self.normalWatermarkRect = self.shadowWaterLabel.frame;
  360. }
  361. - (void)rotationWatermarkLabel:(UIPanGestureRecognizer *)recognizer {
  362. CGPoint point = [recognizer translationInView:_textPreview];
  363. CGFloat radian = atan2(point.x + _textPreview.center.x - _preLabel.center.x, point.y + _textPreview.center.y - _preLabel.center.y);
  364. _preLabel.transform = CGAffineTransformMakeRotation(-radian);
  365. self.textDataModel.watermarkRotation = (-radian) * 180 / M_PI;
  366. }
  367. - (void)createImageGestureRecognizer {
  368. [_imageDocumentView setUserInteractionEnabled:YES];
  369. [_preImage setMultipleTouchEnabled:YES];
  370. [_preImage setUserInteractionEnabled:YES];
  371. [_imageRotationBtn setUserInteractionEnabled:YES];
  372. UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panImageWatermarkView:)];
  373. [_preImage addGestureRecognizer:panRecognizer];
  374. UIPanGestureRecognizer *panRotationBtnRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(rotateImageWatermarkView:)];
  375. [_imageRotationBtn addGestureRecognizer:panRotationBtnRecognizer];
  376. }
  377. - (void)rotateImageWatermarkView:(UIPanGestureRecognizer *)recognizer {
  378. CGPoint point = [recognizer translationInView:_imagePreView];
  379. CGFloat radian = atan2(point.x + _imagePreView.center.x - _preImage.center.x,point.y + _imagePreView.center.y - _preImage.center.y);
  380. if (self.imagedataModel.watermarkScale == (17.0 / 24.0)) {
  381. _preImage.transform = CGAffineTransformMakeRotation(-radian);
  382. } else {
  383. _preImage.transform = CGAffineTransformRotate(self.transform2, -radian);
  384. }
  385. _transform1 = CGAffineTransformMakeRotation(-radian);
  386. self.imagedataModel.watermarkRotation = (-radian) * 180 / M_PI;
  387. }
  388. - (void)panImageWatermarkView:(UIPanGestureRecognizer *)recognizer {
  389. CGPoint point = [recognizer translationInView:_imagePreView];
  390. CGRect documentFrame = _imagePreView.frame;
  391. documentFrame.origin.x -= _imagePreView.frame.origin.x;
  392. documentFrame.origin.y -= _imagePreView.frame.origin.y;
  393. [_preImage setCenter:CGPointMake(_preImage.center.x + point.x, _preImage.center.y + point.y)];
  394. [_imageRotationBtn setCenter:CGPointMake(_imageRotationBtn.center.x + point.x, _imageRotationBtn.center.y + point.y)];
  395. if (!CGRectContainsRect(documentFrame,_preImage.frame)) {
  396. [_preImage setCenter:CGPointMake(_preImage.center.x - point.x, _preImage.center.y - point.y)];
  397. [_imageRotationBtn setCenter:CGPointMake(_imageRotationBtn.center.x - point.x, _imageRotationBtn.center.y - point.y)];
  398. }
  399. self.imagedataModel.tx = _preImage.center.x - (_imageDocumentView.center.x - _imageDocumentView.frame.origin.x);
  400. self.imagedataModel.ty = _imageDocumentView.center.y - _imageDocumentView.frame.origin.y - _preImage.center.y;
  401. _shadowImageWaterLabel.center = _preImage.center;
  402. [recognizer setTranslation:CGPointZero inView:_imagePreView];
  403. _normalImageWatermarkRect = _shadowImageWaterLabel.frame;
  404. }
  405. #pragma mark - Accessors
  406. - (NSArray *)dataArray {
  407. if (!_dataArray) {
  408. _dataArray = [NSMutableArray array];
  409. NSArray *waterArray = [_document watermarks];
  410. for (NSInteger i = 0; i < waterArray.count; i++) {
  411. CPDFWatermark *dataWater = [[CPDFWatermark alloc] init];
  412. dataWater = waterArray[i];
  413. [_dataArray addObject:dataWater];
  414. }
  415. }
  416. return _dataArray;
  417. }
  418. #pragma mark - UITableViewDataSource
  419. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  420. return self.dataArray.count;
  421. }
  422. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  423. static NSString *path = @"AddWater";
  424. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:path];
  425. if (!cell) {
  426. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:path];
  427. }
  428. if ([self.dataArray[indexPath.row] text]) {
  429. cell.textLabel.text = [self.dataArray[indexPath.row] text];
  430. } else {
  431. cell.imageView.image = [self.dataArray[indexPath.row] image];
  432. }
  433. return cell;
  434. }
  435. #pragma mark - UITableViewDelegate
  436. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  437. return UITableViewCellEditingStyleDelete;
  438. }
  439. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  440. if (editingStyle == UITableViewCellEditingStyleDelete) {
  441. [self.delegate PDFViewPerformDeleteWatermark:self.dataArray[indexPath.row]];
  442. [self.dataArray removeObjectAtIndex:indexPath.row];
  443. [tableView beginUpdates];
  444. [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
  445. [tableView endUpdates];
  446. }
  447. }
  448. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
  449. CPDFWatermark *dataWater = _dataArray[sourceIndexPath.row];
  450. [_dataArray removeObject:dataWater];
  451. [_dataArray insertObject:dataWater atIndex:destinationIndexPath.row];
  452. }
  453. #pragma mark - Actions
  454. - (void)onClickedDoneBtn {
  455. self.navigationItem.rightBarButtonItem = nil;
  456. if (_segmentedControl.selectedSegmentIndex == 0) {
  457. _dataModel = self.textDataModel;
  458. [self.delegate PDFViewPerformChangeTextWatermark:self.dataModel];
  459. } else {
  460. _dataModel = self.imagedataModel;
  461. [self.delegate PDFViewPerformChangeImageWatermark:self.dataModel];
  462. }
  463. [self.navigationController popViewControllerAnimated:YES];
  464. }
  465. - (void)segmentedAction:(UISegmentedControl *)sender {
  466. switch(sender.selectedSegmentIndex) {
  467. case 0:
  468. _textViewController.hidden = NO;
  469. _imageViewController.hidden = YES;
  470. break;
  471. case 1:
  472. _textViewController.hidden = YES;
  473. _imageViewController.hidden = NO;
  474. break;
  475. default:
  476. break;
  477. }
  478. }
  479. - (void)editClick {
  480. _editView.hidden = NO;
  481. }
  482. - (void)cancelClick {
  483. _editView.hidden = YES;
  484. }
  485. #pragma mark - CPDFClipTextPreviewDelegate
  486. - (void)clipText:(CGContextRef)context {
  487. CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);
  488. CGContextFillRect(context, _textPreview.frame);
  489. CGContextClearRect(context, _textDocumentView.frame);
  490. }
  491. #pragma mark - UIImagePickerControllerDelegate
  492. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
  493. UIImage *image = info[UIImagePickerControllerOriginalImage];
  494. CGSize size;
  495. size.width = image.size.width / 30;
  496. size.height = image.size.height / 30;
  497. _preImage.image = [self imageWithImageSimple:image scaledToSize:size];
  498. self.imagedataModel.image = [self imageWithImageSimple:image scaledToSize:size];
  499. [_preImage sizeToFit];
  500. _shadowImageWaterLabel.frame = _preImage.frame;
  501. [picker dismissViewControllerAnimated:YES completion:nil];
  502. _normalImageWatermarkRect = _shadowImageWaterLabel.frame;
  503. }
  504. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  505. [picker dismissViewControllerAnimated:YES completion:nil];
  506. }
  507. - (UIImage *)imageWithImageSimple:(UIImage *)image scaledToSize:(CGSize)newSize
  508. {
  509. UIGraphicsBeginImageContext(newSize);
  510. [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
  511. UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  512. UIGraphicsEndImageContext();
  513. return newImage;
  514. }
  515. #pragma mark - UITextFieldDelegate
  516. - (void)textFieldDidBeginEditing:(UITextField *)textField {
  517. if (_imageHorizontalField == textField || _imageVerticalField == textField) {
  518. self.imageDrawView.hidden = YES;
  519. self.imageClipView.hidden = YES;
  520. if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown) {
  521. } else {
  522. if (_imageView.frame.origin.y > (self.view.frame.size.height - 450)) {
  523. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  524. self.imageView.center = CGPointMake(self.imageView.center.x, self.imageView.center.y - 300);
  525. } completion:nil];
  526. }
  527. }
  528. } else if (_textHorizontalField == textField || _textVerticalField == textField) {
  529. self.textDrawView.hidden = YES;
  530. self.textClipView.hidden = YES;
  531. if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown) {
  532. } else {
  533. if (_textView.frame.origin.y > (self.view.frame.size.height - 450)) {
  534. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  535. self.textView.center = CGPointMake(self.textView.center.x, self.textView.center.y - 300);
  536. } completion:nil];
  537. }
  538. }
  539. }
  540. }
  541. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  542. [textField resignFirstResponder];
  543. if (_imageHorizontalField == textField || _imageVerticalField == textField) {
  544. self.imageDrawView.hidden = NO;
  545. self.imageClipView.hidden = NO;
  546. if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown) {
  547. } else {
  548. if(_imageView.frame.origin.y < self.view.frame.size.height - 200) {
  549. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  550. self.imageView.center = CGPointMake(self.imageView.center.x, self.imageView.center.y + 300);
  551. } completion:nil];
  552. }
  553. }
  554. } else if (_textHorizontalField == textField || _textVerticalField == textField) {
  555. self.imageDrawView.hidden = NO;
  556. self.imageClipView.hidden = NO;
  557. if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown) {
  558. } else {
  559. if(_textView.frame.origin.y < self.view.frame.size.height - 200) {
  560. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  561. self.textView.center = CGPointMake(self.textView.center.x, self.textView.center.y + 300);
  562. } completion:nil];
  563. }
  564. }
  565. }
  566. return YES;
  567. }
  568. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  569. if (_textHorizontalField == textField || _textVerticalField == textField) {
  570. return [self validateValue:string];
  571. } else if (_imageHorizontalField == textField || _imageVerticalField == textField) {
  572. return [self validateValue:string];
  573. }
  574. return YES;
  575. }
  576. - (BOOL)validateValue:(NSString *)number {
  577. BOOL res = YES;
  578. NSCharacterSet *numberSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
  579. NSInteger i = 0;
  580. while (i < number.length) {
  581. NSString *str = [number substringWithRange:NSMakeRange(i, 1)];
  582. NSRange range = [str rangeOfCharacterFromSet:numberSet];
  583. if (range.length == 0) {
  584. res = NO;
  585. break;
  586. }
  587. i++;
  588. }
  589. return res;
  590. }
  591. @end