123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- //
- // CPDFViewController.m
- // PDFViewer
- //
- // Created by kdan on 2022/11/15.
- //
- #import <ComPDFKit/ComPDFKit.h>
- #import <ComPDFKit/CPDFWatermark.h>
- #import <ComPDFKit/CPDFHeaderFooter.h>
- #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"
- @interface CPDFViewController () <UIActionSheetDelegate>
- @property (nonatomic,strong) CPDFTextViewController *textViewController;
- @property (nonatomic,strong) CPDFImageViewController *imageViewController;
- @property (nonatomic,strong) CPDFView *pdfView;
- @property (nonatomic,strong) CPDFDocument *pdfDocument;
- @property (nonatomic,strong) CPDFWatermark *textWatermark;
- @property (nonatomic,strong) CPDFWatermark *imageWatermark;
- @property (nonatomic,assign) CGSize imageSize;
- @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 = YES;
- 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];
- _pdfDocument = [[CPDFDocument alloc] initWithURL:url];
- _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
- _pdfView.document = _pdfDocument;
- _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];
- _imageSize = [_pdfView.document pageSizeAtIndex:0];
- _textViewController.textPreview.documentView.image = [pdfPage thumbnailOfSize:_imageSize];
- _imageViewController.imagePreview.documentView.image = [pdfPage thumbnailOfSize:_imageSize];
- _imageViewController.view.hidden = YES;
- _pdfView.hidden = YES;
-
- _textWatermark = [[CPDFWatermark alloc] initWithDocument:_pdfDocument type:CPDFWatermarkTypeText];
- _imageWatermark = [[CPDFWatermark alloc] initWithDocument:_pdfDocument 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)addConstraint {
-
- [_segmentedControl mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.navigationController.navigationBar.mas_bottom).offset(0);
- make.width.equalTo(self.view.mas_width);
- make.height.equalTo(@(self.view.bounds.size.height / 20));
- }];
-
- [_textViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(_segmentedControl.mas_bottom).offset(0);
- make.width.equalTo(_segmentedControl.mas_width);
- make.bottom.equalTo(self.view).offset(-34);
- }];
- [_imageViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(_segmentedControl.mas_bottom).offset(0);
- make.width.equalTo(_segmentedControl.mas_width);
- make.bottom.equalTo(self.view).offset(-34);
- }];
- [_textViewController.textPreview.documentView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerX.equalTo(_textViewController.textPreview.mas_centerX);
- make.centerY.equalTo(_textViewController.textPreview.mas_centerY);
- make.width.equalTo(@(_imageSize.width / 2));
- make.height.equalTo(@(_imageSize.height / 2));
- }];
- [_imageViewController.imagePreview.documentView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerX.equalTo(_imageViewController.imagePreview.mas_centerX);
- make.centerY.equalTo(_imageViewController.imagePreview.mas_centerY);
- make.width.equalTo(@(_imageSize.width / 2));
- make.height.equalTo(@(_imageSize.height / 2));
- }];
- }
- - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
- [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
- if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
-
- if (_imageSize.width > _imageSize.height) {
- [_textViewController.textPreview.documentView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerX.equalTo(_textViewController.textPreview.mas_centerX);
- make.centerY.equalTo(_textViewController.textPreview.mas_centerY);
- make.width.equalTo(@(_imageSize.width / 3));
- make.height.equalTo(@(_imageSize.height / 3));
- }];
- [_imageViewController.imagePreview.documentView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerX.equalTo(_imageViewController.imagePreview.mas_centerX);
- make.centerY.equalTo(_imageViewController.imagePreview.mas_centerY);
- make.width.equalTo(@(_imageSize.width / 3));
- make.height.equalTo(@(_imageSize.height / 3));
- }];
- } else {
- [_textViewController.textPreview.documentView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerX.equalTo(_textViewController.textPreview.mas_centerX);
- make.centerY.equalTo(_textViewController.textPreview.mas_centerY);
- make.width.equalTo(@(_imageSize.width / 3));
- make.height.equalTo(@(_imageSize.height / 3.3));
- }];
- [_imageViewController.imagePreview.documentView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerX.equalTo(_imageViewController.imagePreview.mas_centerX);
- make.centerY.equalTo(_imageViewController.imagePreview.mas_centerY);
- make.width.equalTo(@(_imageSize.width / 3));
- make.height.equalTo(@(_imageSize.height / 3.3));
- }];
- }
-
- } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
-
- [_textViewController.textPreview.documentView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.centerX.equalTo(_textViewController.textPreview.mas_centerX);
- make.centerY.equalTo(_textViewController.textPreview.mas_centerY);
- make.width.equalTo(@(_imageSize.width / 2));
- make.height.equalTo(@(_imageSize.height / 2));
- }];
- [_imageViewController.imagePreview.documentView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.centerX.equalTo(_imageViewController.imagePreview.mas_centerX);
- make.centerY.equalTo(_imageViewController.imagePreview.mas_centerY);
- make.width.equalTo(@(_imageSize.width / 2));
- make.height.equalTo(@(_imageSize.height / 2));
- }];
- }
- }
- - (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 addConstraint];
- 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) {
-
- }];
- [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 = _textViewController.dataModel.text;
- _textWatermark.textColor = _textViewController.dataModel.textColor;
- _textWatermark.opacity = _textViewController.dataModel.watermarkOpacity;
- _textWatermark.scale = _textViewController.dataModel.watermarkScale;
- _textWatermark.isTilePage = _textViewController.dataModel.isTile;
- _textWatermark.tx = _textViewController.dataModel.tx * _imageSize.width / _textViewController.textPreview.documentView.frame.size.width;
- _textWatermark.ty = _textViewController.dataModel.ty * _imageSize.height / _textViewController.textPreview.documentView.frame.size.height;
- _textWatermark.rotation = _textViewController.dataModel.watermarkRotation;
-
- if (_textWatermark.isTilePage) {
- _textWatermark.verticalSpacing = [_textViewController.textView.verticalField.text floatValue];
- _textWatermark.horizontalSpacing = [_textViewController.textView.horizontalField.text floatValue];
- }
-
- if (_textViewController.dataModel.pageString) {
- _textWatermark.pageString = _textViewController.dataModel.pageString;
- }
-
- [_pdfView.document addWatermark:_textWatermark];
- [_pdfView layoutDocumentView];
- // NSURL *url = [NSURL fileURLWithPath:_path];
- // [_pdfView.document writeToURL:url];
-
- } else {
-
- _imageWatermark.image = [UIImage imageNamed:_imageViewController.dataModel.imagePath];
- _imageWatermark.opacity = _imageViewController.dataModel.watermarkOpacity;
- _imageWatermark.scale = _imageViewController.dataModel.watermarkScale;
- _imageWatermark.isTilePage = _imageViewController.dataModel.isTile;
- _imageWatermark.tx = _imageViewController.dataModel.tx * _imageSize.width / _imageViewController.imagePreview.documentView.frame.size.width;
- _imageWatermark.ty = _imageViewController.dataModel.ty * _imageSize.height / _imageViewController.imagePreview.documentView.frame.size.height;
- _imageWatermark.rotation = _imageViewController.dataModel.watermarkRotation;
-
- if (_imageWatermark.isTilePage) {
- _imageWatermark.verticalSpacing = [_imageViewController.imageView.verticalField.text floatValue];
- _imageWatermark.horizontalSpacing = [_imageViewController.imageView.horizontalField.text floatValue];
- }
-
- if (_imageViewController.dataModel.pageString) {
- _imageWatermark.pageString = _imageViewController.dataModel.pageString;
- }
-
- [_pdfView.document addWatermark:_imageWatermark];
- [_pdfView layoutDocumentView];
- // NSURL *url = [NSURL fileURLWithPath:_path];
- // [_pdfView.document writeContentToURL:url];
- }
- }
- @end
|