CPDFViewController.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #define KSCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
  20. #define KSCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height
  21. @interface CPDFViewController () <UIActionSheetDelegate>
  22. @property (nonatomic,strong) CPDFTextViewController *textViewController;
  23. @property (nonatomic,strong) CPDFImageViewController *imageViewController;
  24. @property (nonatomic,strong) CPDFView *pdfView;
  25. @property (nonatomic,strong) CPDFWatermark *textWatermark;
  26. @property (nonatomic,strong) CPDFWatermark *imageWatermark;
  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 = NO;
  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. CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
  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)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. CGSize size = [_pdfView.document pageSizeAtIndex:0];
  66. _textViewController.textPreview.documentView.image = [pdfPage thumbnailOfSize:size];
  67. _imageViewController.imagePreview.documentView.image = [pdfPage thumbnailOfSize:size];
  68. _imageViewController.view.hidden = YES;
  69. _pdfView.hidden = YES;
  70. _textWatermark = [[CPDFWatermark alloc] initWithDocument:_pdfView.document type:CPDFWatermarkTypeText];
  71. _imageWatermark = [[CPDFWatermark alloc] initWithDocument:_pdfView.document 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)createFrame {
  88. NSSet *set = [UIApplication sharedApplication].connectedScenes;
  89. UIWindowScene *windowScene = [set anyObject];
  90. UIStatusBarManager *statusBarManager = windowScene.statusBarManager;
  91. _segmentedControl.frame = CGRectMake(0, statusBarManager.statusBarFrame.size.height + self.navigationController.navigationBar.frame.size
  92. .height, KSCREEN_WIDTH, KSCREEN_HEIGHT / 20);
  93. _segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
  94. UIEdgeInsets padding = UIEdgeInsetsMake(statusBarManager.statusBarFrame.size.height + self.navigationController.navigationBar.frame.size
  95. .height + KSCREEN_HEIGHT / 20, 0, 0, 0);
  96. [_textViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
  97. make.edges.equalTo(self.view).with.insets(padding);
  98. }];
  99. [_imageViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
  100. make.edges.equalTo(self.view).with.insets(padding);
  101. }];
  102. }
  103. - (void)onClickedOkbtn {
  104. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  105. UIAlertAction *addWatermarkAction = [UIAlertAction actionWithTitle:@"Add Watermark" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  106. [self createView];
  107. [self createFrame];
  108. UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(onClickedDoneBtn)];
  109. self.navigationItem.rightBarButtonItem = doneBtn;
  110. }];
  111. UIAlertAction *addHeaderfooterAction = [UIAlertAction actionWithTitle:@"Add Headerfooter" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  112. NSLog(@"addHeaderfooterAction");
  113. }];
  114. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  115. NSLog(@"Cancel Action");
  116. }];
  117. [alertController addAction:addWatermarkAction];
  118. [alertController addAction:addHeaderfooterAction];
  119. [alertController addAction:cancelAction];
  120. [self presentViewController:alertController animated:YES completion:nil];
  121. }
  122. - (void)onClickedDoneBtn {
  123. self.navigationItem.rightBarButtonItem = nil;
  124. _pdfView.hidden = NO;
  125. _textViewController.view.hidden = YES;
  126. _imageViewController.view.hidden = YES;
  127. _segmentedControl.hidden = YES;
  128. if (_segmentedControl.selectedSegmentIndex == 0) {
  129. _textWatermark.text = @"Watermark";
  130. _textWatermark.textColor = _textViewController.dataModel.textColor;
  131. _textWatermark.opacity = _textViewController.dataModel.watermarkOpacity;
  132. _textWatermark.scale = _textViewController.dataModel.watermarkScale;
  133. _textWatermark.isTilePage = _textViewController.dataModel.isTile;
  134. [_pdfView.document addWatermark:_textWatermark];
  135. } else {
  136. [_pdfView.document addWatermark:_imageWatermark];
  137. }
  138. }
  139. @end