// // CPDFViewController.m // PDFViewer // // Created by kdan on 2022/11/15. // #import #import #import #import "CPDFViewController.h" #import "CPDFTextViewController.h" #import "CPDFImageViewController.h" #import "CPDFTextView.h" #import "CPDFTextPreview.h" #import "CPDFImageView.h" #import "CPDFImagePreview.h" #import "CPDFDataModel.h" #import "Masonry.h" #define KSCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width #define KSCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height @interface CPDFViewController () @property (nonatomic,strong) CPDFTextViewController *textViewController; @property (nonatomic,strong) CPDFImageViewController *imageViewController; @property (nonatomic,strong) CPDFView *pdfView; @property (nonatomic,strong) CPDFWatermark *textWatermark; @property (nonatomic,strong) CPDFWatermark *imageWatermark; @end @implementation CPDFViewController - (instancetype)initWithPath:(NSString *)inPath { if (self = [super init]) { _path = inPath; } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //add UIBarButtonItem in Navigation self.navigationController.toolbarHidden = NO; UIImage *btnMore = [UIImage imageNamed:@"btn_more"]; UIBarButtonItem *ringhtBarItem = [[UIBarButtonItem alloc] initWithImage:btnMore style:UIBarButtonItemStylePlain target:self action:@selector(onClickedOkbtn)]; self.navigationItem.rightBarButtonItem = ringhtBarItem; //setting URL NSURL *url = [NSURL fileURLWithPath:_path]; CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url]; _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds]; _pdfView.document = document; _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; [self.view addSubview:_pdfView]; } - (void)createView { NSArray *segmentArray = @[@"Text",@"Image"]; _segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentArray]; [_segmentedControl addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged]; _segmentedControl.selectedSegmentIndex = 0; [_segmentedControl setBackgroundColor:[UIColor whiteColor]]; _textViewController = [[CPDFTextViewController alloc] init]; _imageViewController = [[CPDFImageViewController alloc] init]; [self addChildViewController:_textViewController]; [self addChildViewController:_imageViewController]; [self.view addSubview:_segmentedControl]; [self.view addSubview:_textViewController.view]; [self.view addSubview:_imageViewController.view]; CPDFPage *pdfPage = [_pdfView.document pageAtIndex:0]; CGSize size = [_pdfView.document pageSizeAtIndex:0]; _textViewController.textPreview.documentView.image = [pdfPage thumbnailOfSize:size]; _imageViewController.imagePreview.documentView.image = [pdfPage thumbnailOfSize:size]; _imageViewController.view.hidden = YES; _pdfView.hidden = YES; _textWatermark = [[CPDFWatermark alloc] initWithDocument:_pdfView.document type:CPDFWatermarkTypeText]; _imageWatermark = [[CPDFWatermark alloc] initWithDocument:_pdfView.document type:CPDFWatermarkTypeImage]; } - (void)segmentedAction:(UISegmentedControl *)sender { switch(sender.selectedSegmentIndex) { case 0: _textViewController.view.hidden = NO; _imageViewController.view.hidden = YES; break; case 1: _textViewController.view.hidden = YES; _imageViewController.view.hidden = NO; break; default: break; } } - (void)createFrame { NSSet *set = [UIApplication sharedApplication].connectedScenes; UIWindowScene *windowScene = [set anyObject]; UIStatusBarManager *statusBarManager = windowScene.statusBarManager; _segmentedControl.frame = CGRectMake(0, statusBarManager.statusBarFrame.size.height + self.navigationController.navigationBar.frame.size .height, KSCREEN_WIDTH, KSCREEN_HEIGHT / 20); _segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin; UIEdgeInsets padding = UIEdgeInsetsMake(statusBarManager.statusBarFrame.size.height + self.navigationController.navigationBar.frame.size .height + KSCREEN_HEIGHT / 20, 0, 0, 0); [_textViewController.view mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.view).with.insets(padding); }]; [_imageViewController.view mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.view).with.insets(padding); }]; } - (void)onClickedOkbtn { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *addWatermarkAction = [UIAlertAction actionWithTitle:@"Add Watermark" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self createView]; [self createFrame]; UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(onClickedDoneBtn)]; self.navigationItem.rightBarButtonItem = doneBtn; }]; UIAlertAction *addHeaderfooterAction = [UIAlertAction actionWithTitle:@"Add Headerfooter" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"addHeaderfooterAction"); }]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"Cancel Action"); }]; [alertController addAction:addWatermarkAction]; [alertController addAction:addHeaderfooterAction]; [alertController addAction:cancelAction]; [self presentViewController:alertController animated:YES completion:nil]; } - (void)onClickedDoneBtn { self.navigationItem.rightBarButtonItem = nil; _pdfView.hidden = NO; _textViewController.view.hidden = YES; _imageViewController.view.hidden = YES; _segmentedControl.hidden = YES; if (_segmentedControl.selectedSegmentIndex == 0) { _textWatermark.text = @"Watermark"; _textWatermark.textColor = _textViewController.dataModel.textColor; _textWatermark.opacity = _textViewController.dataModel.watermarkOpacity; _textWatermark.scale = _textViewController.dataModel.watermarkScale; _textWatermark.isTilePage = _textViewController.dataModel.isTile; [_pdfView.document addWatermark:_textWatermark]; } else { [_pdfView.document addWatermark:_imageWatermark]; } } @end