CPDFViewController.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 "CPDFTextPreview.h"
  12. #import "CPDFImagePreview.h"
  13. #import "CPDFTextView.h"
  14. #import "CPDFImageView.h"
  15. #import "CPDFDataModel.h"
  16. #import "CPDFBackgroundModel.h"
  17. #import "Masonry.h"
  18. #import "CPDFAddViewController.h"
  19. #import "CPDFBackgroundSettingViewController.h"
  20. @interface CPDFViewController () <UIActionSheetDelegate,WaterMarkModelDataDelegate,BackgroundDataModelDelegate>
  21. @property (nonatomic,strong) CPDFView *pdfView;
  22. @property (nonatomic,strong) CPDFDocument *pdfDocument;
  23. @property (nonatomic,strong) CPDFWatermark *textWatermark;
  24. @property (nonatomic,strong) CPDFWatermark *imageWatermark;
  25. @property (nonatomic,strong) CPDFAddViewController *addWaterMarkViewController;
  26. @property (nonatomic,strong) CPDFBackgroundSettingViewController * addBackgroundViewController;
  27. @property (nonatomic,strong) UIImage *image;
  28. @property (nonatomic,assign) CGSize imageSize;
  29. @end
  30. @implementation CPDFViewController
  31. - (instancetype)initWithPath:(NSString *)inPath {
  32. if (self = [super init]) {
  33. _path = inPath;
  34. }
  35. return self;
  36. }
  37. - (void)viewDidLoad {
  38. [super viewDidLoad];
  39. // Do any additional setup after loading the view.
  40. //add UIBarButtonItem in Navigation
  41. self.navigationController.toolbarHidden = YES;
  42. UIImage *btnMore = [UIImage imageNamed:@"btn_more"];
  43. UIBarButtonItem *ringhtBarItem = [[UIBarButtonItem alloc] initWithImage:btnMore style:UIBarButtonItemStylePlain target:self action:@selector(onClickedOkbtn)];
  44. self.navigationItem.rightBarButtonItem = ringhtBarItem;
  45. //setting URL
  46. NSURL *url = [NSURL fileURLWithPath:_path];
  47. _pdfDocument = [[CPDFDocument alloc] initWithURL:url];
  48. _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
  49. _pdfView.document = _pdfDocument;
  50. _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  51. [self.view addSubview:_pdfView];
  52. CPDFPage *pdfPage = [_pdfView.document pageAtIndex:0];
  53. _imageSize = [_pdfView.document pageSizeAtIndex:0];
  54. _image = [pdfPage thumbnailOfSize:_imageSize];
  55. }
  56. #pragma mark - Actions
  57. - (void)onClickedOkbtn {
  58. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  59. UIAlertAction *addWatermarkAction = [UIAlertAction actionWithTitle:@"Add Watermark" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  60. self.addWaterMarkViewController = [[CPDFAddViewController alloc] initWithDocument:self.pdfDocument];
  61. self.addWaterMarkViewController.delegate = self;
  62. [self.navigationController pushViewController:self.addWaterMarkViewController animated:YES];
  63. }];
  64. UIAlertAction *addHeaderfooterAction = [UIAlertAction actionWithTitle:@"Add Headerfooter" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  65. NSLog(@"addHeaderfooterAction");
  66. }];
  67. UIAlertAction *addBackgroundAction = [UIAlertAction actionWithTitle:@"Add Background" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  68. self.addBackgroundViewController = [[CPDFBackgroundSettingViewController alloc] initWithDocument:self.pdfDocument];
  69. self.addBackgroundViewController.delegate = self;
  70. [self.navigationController pushViewController:self.addBackgroundViewController animated:YES];
  71. }];
  72. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  73. }];
  74. [alertController addAction:addWatermarkAction];
  75. [alertController addAction:addHeaderfooterAction];
  76. [alertController addAction:addBackgroundAction];
  77. [alertController addAction:cancelAction];
  78. [self presentViewController:alertController animated:YES completion:nil];
  79. }
  80. #pragma mark - WaterMarkModelDataDelegate
  81. - (void)changeTextModel:(CPDFDataModel *)dataModel {
  82. _textWatermark = [[CPDFWatermark alloc] initWithDocument:_pdfDocument type:CPDFWatermarkTypeText];
  83. _textWatermark.text = dataModel.text;
  84. _textWatermark.textColor = dataModel.textColor;
  85. _textWatermark.opacity = dataModel.watermarkOpacity;
  86. _textWatermark.scale = dataModel.watermarkScale * 2.5;
  87. _textWatermark.isTilePage = dataModel.isTile;
  88. _textWatermark.tx = dataModel.tx * _imageSize.width / self.addWaterMarkViewController.textViewController.textPreview.documentView.frame.size.width;
  89. _textWatermark.ty = dataModel.ty * _imageSize.height / self.addWaterMarkViewController.textViewController.textPreview.documentView.frame.size.height;
  90. _textWatermark.rotation = dataModel.watermarkRotation;
  91. if (_textWatermark.isTilePage) {
  92. _textWatermark.verticalSpacing = dataModel.verticalSpacing;
  93. _textWatermark.horizontalSpacing = dataModel.horizontalSpacing;
  94. }
  95. if (dataModel.pageString) {
  96. _textWatermark.pageString = dataModel.pageString;
  97. }
  98. [_pdfView.document addWatermark:_textWatermark];
  99. [_pdfView layoutDocumentView];
  100. NSURL *url = [NSURL fileURLWithPath:_path];
  101. [_pdfView.document writeToURL:url];
  102. }
  103. - (void)changeImageModel:(CPDFDataModel *)dataModel {
  104. _imageWatermark = [[CPDFWatermark alloc] initWithDocument:_pdfDocument type:CPDFWatermarkTypeImage];
  105. _imageWatermark.image = dataModel.image;
  106. _imageWatermark.opacity = dataModel.watermarkOpacity;
  107. _imageWatermark.scale = dataModel.watermarkScale * 2;
  108. _imageWatermark.isTilePage = dataModel.isTile;
  109. _imageWatermark.tx = dataModel.tx * _imageSize.width / self.addWaterMarkViewController.imageViewController.imagePreview.documentView.frame.size.width;
  110. _imageWatermark.ty = dataModel.ty * _imageSize.height / self.addWaterMarkViewController.imageViewController.imagePreview.documentView.frame.size.height;
  111. _imageWatermark.rotation = dataModel.watermarkRotation;
  112. if (_imageWatermark.isTilePage) {
  113. _imageWatermark.verticalSpacing = dataModel.verticalSpacing;
  114. _imageWatermark.horizontalSpacing = dataModel.horizontalSpacing;
  115. }
  116. if (dataModel.pageString) {
  117. _imageWatermark.pageString = dataModel.pageString;
  118. }
  119. [_pdfView.document addWatermark:_imageWatermark];
  120. [_pdfView layoutDocumentView];
  121. NSURL *url = [NSURL fileURLWithPath:_path];
  122. [_pdfView.document writeContentToURL:url];
  123. }
  124. - (void)deleteTextModel:(CPDFWatermark *)waterMark {
  125. [self.pdfDocument removeWatermark:waterMark];
  126. NSURL *url = [NSURL fileURLWithPath:_path];
  127. [_pdfView.document writeToURL:url];
  128. _pdfDocument = [[CPDFDocument alloc] initWithURL:url];
  129. _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
  130. _pdfView.document = _pdfDocument;
  131. _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  132. [self.view addSubview:_pdfView];
  133. }
  134. #pragma mark - BackgroundDataModelDelegate
  135. - (void)changeBackgroundModel:(CPDFBackgroundModel *)dataModel {
  136. CPDFBackground *pageBackground = [_pdfDocument background];
  137. pageBackground.color = dataModel.backgroundColor;
  138. pageBackground.scale = dataModel.backgroudScale;
  139. pageBackground.rotation = dataModel.backgroundRotation;
  140. pageBackground.opacity = dataModel.backgroundOpacity;
  141. pageBackground.pageString = dataModel.pageString;
  142. pageBackground.xOffset = dataModel.horizontalSpacing;
  143. pageBackground.yOffset = dataModel.verticalSpacing;
  144. pageBackground.isAllowsView = YES;
  145. pageBackground.isAllowsPrint = YES;
  146. [pageBackground setImage:dataModel.image];
  147. [pageBackground update];
  148. NSURL *url = [NSURL fileURLWithPath:_path];
  149. [_pdfView.document writeToURL:url];
  150. _pdfDocument = [[CPDFDocument alloc] initWithURL:url];
  151. _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
  152. _pdfView.document = _pdfDocument;
  153. _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  154. [self.view addSubview:_pdfView];
  155. }
  156. - (void)deleteBackgroundModel {
  157. CPDFBackground *pageBackground = [_pdfDocument background];
  158. [pageBackground clear];
  159. NSURL *url = [NSURL fileURLWithPath:_path];
  160. [_pdfView.document writeToURL:url];
  161. _pdfDocument = [[CPDFDocument alloc] initWithURL:url];
  162. _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
  163. _pdfView.document = _pdfDocument;
  164. _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  165. [self.view addSubview:_pdfView];
  166. }
  167. @end