123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- //
- // 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 "CPDFTextPreview.h"
- #import "CPDFImagePreview.h"
- #import "CPDFTextView.h"
- #import "CPDFImageView.h"
- #import "CPDFDataModel.h"
- #import "CPDFBackgroundModel.h"
- #import "Masonry.h"
- #import "CPDFAddViewController.h"
- #import "CPDFBackgroundSettingViewController.h"
- @interface CPDFViewController () <UIActionSheetDelegate,WaterMarkModelDataDelegate,BackgroundDataModelDelegate>
- @property (nonatomic,strong) CPDFView *pdfView;
- @property (nonatomic,strong) CPDFDocument *pdfDocument;
- @property (nonatomic,strong) CPDFWatermark *textWatermark;
- @property (nonatomic,strong) CPDFWatermark *imageWatermark;
- @property (nonatomic,strong) CPDFAddViewController *addWaterMarkViewController;
- @property (nonatomic,strong) CPDFBackgroundSettingViewController * addBackgroundViewController;
- @property (nonatomic,strong) UIImage *image;
- @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];
- _document = [[CPDFDocument alloc] initWithURL:url];
-
- // Get document frist image
- CPDFPage *page = [_document pageAtIndex:0];
- CGSize size = [_document pageSizeAtIndex:0];
- _image = [page thumbnailOfSize:size];
-
- // Get document view
- _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
- _pdfView.document = _document;
- _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
-
- [self.view addSubview:_pdfView];
-
- _headerFooterSetting = [[CPDFSettingView alloc] init];
- [_headerFooterSetting setText];
-
- _batesSetting = [[CPDFSettingView alloc] init];
- [_batesSetting setText];
- }
- #pragma mark - CPDFViewController Actions
- - (void)onClickedOkbtn {
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
-
- // Add watermark action
- UIAlertAction *addWatermarkAction = [UIAlertAction actionWithTitle:@"Add Watermark" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- self.addWaterMarkViewController = [[CPDFAddViewController alloc] initWithDocument:self.pdfDocument];
- self.addWaterMarkViewController.delegate = self;
-
- [self.navigationController pushViewController:self.addWaterMarkViewController animated:YES];
- }];
-
- // Setting headerfooter action
- UIAlertAction *settingHeaderFooter = [UIAlertAction actionWithTitle:@"Setting Headerfooter" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- [self settingHeaderFooter];
- }];
-
- // Seeting bates action
- UIAlertAction *settingBatesAction = [UIAlertAction actionWithTitle:@"Setting Bates" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- [self settingBates];
- }];
-
- // Cancel action
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
- NSLog(@"Cancel Action");
- }];
-
- // Add controller
- [alertController addAction:addWatermarkAction];
- [alertController addAction:settingHeaderFooter];
- [alertController addAction:settingBatesAction];
- [alertController addAction:addBackgroundAction];
- [alertController addAction:cancelAction];
-
- [self presentViewController:alertController animated:YES completion:nil];
- }
- #pragma mark - Setting Pop
- - (void)settingHeaderFooter {
- [self.view addSubview:_headerFooterSetting];
-
- [_headerFooterSetting mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.bottom.equalTo(self.view.mas_bottom);
- make.left.equalTo(self.view.mas_left);
- make.right.equalTo(self.view.mas_right);
- make.height.mas_equalTo(100);
- }];
-
- [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
- self.headerFooterSetting.center = CGPointMake(self.headerFooterSetting.center.x, self.headerFooterSetting.center.y - 100);
- } completion:nil];
-
- [_headerFooterSetting.addButton addTarget:self action:@selector(addHeaderFooter:) forControlEvents:UIControlEventTouchUpInside];
- [_headerFooterSetting.deleteButton addTarget:self action:@selector(deleteHeaderFooter:) forControlEvents:UIControlEventTouchUpInside];
- }
- - (void)settingBates {
- [self.view addSubview:_batesSetting];
-
- [_batesSetting mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.bottom.equalTo(self.view.mas_bottom);
- make.left.equalTo(self.view.mas_left);
- make.right.equalTo(self.view.mas_right);
- make.height.mas_equalTo(100);
- }];
-
- [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
- self.batesSetting.center = CGPointMake(self.batesSetting.center.x, self.batesSetting.center.y - 100);
- } completion:nil];
-
- [_batesSetting.addButton addTarget:self action:@selector(addBates:) forControlEvents:UIControlEventTouchUpInside];
- [_batesSetting.deleteButton addTarget:self action:@selector(deleteBates:) forControlEvents:UIControlEventTouchUpInside];
- }
- #pragma mark - Actions
- - (void)addHeaderFooter:(UIButton *)btn {
- CPDFHeaderFooterAddController *headerFooterControl = [[CPDFHeaderFooterAddController alloc] initWithImage:self.image];
- headerFooterControl.delegate = self;
- [self.navigationController pushViewController:headerFooterControl animated:NO];
-
- [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
- self.headerFooterSetting.center = CGPointMake(self.headerFooterSetting.center.x, self.headerFooterSetting.center.y + 100);
- } completion:nil];
- }
- - (void)deleteHeaderFooter:(UIButton *)btn {
- [self deleteHeaderFooterAction];
-
- [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
- self.headerFooterSetting.center = CGPointMake(self.headerFooterSetting.center.x, self.headerFooterSetting.center.y + 100);
- } completion:nil];
- }
- - (void)addBates:(UIButton *)btn {
- CPDFBatesAddViewController *batesAddControl = [[CPDFBatesAddViewController alloc] initWithImage:self.image];
-
- batesAddControl.delegate = self;
-
- [self.navigationController pushViewController:batesAddControl animated:NO];
-
- [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
- self.batesSetting.center = CGPointMake(self.batesSetting.center.x, self.batesSetting.center.y + 100);
- } completion:nil];
- }
- - (void)deleteBates:(UIButton *)btn {
- [self deleteBatesAction];
-
- [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
- self.batesSetting.center = CGPointMake(self.batesSetting.center.x, self.batesSetting.center.y + 100);
- } completion:nil];
- }
- #pragma mark - Delete Alert
- // Detele headerfootr action
- - (void)deleteHeaderFooterAction {
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Delete Page Number" message:@"Are you sure delete all pages number" preferredStyle:UIAlertControllerStyleAlert];
-
- UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- [self deleteHeaderFooter];
- }];
-
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
- NSLog(@"Cancel Action");
- }];
-
- [alertController addAction:okAction];
- [alertController addAction:cancelAction];
-
- [self presentViewController:alertController animated:YES completion:nil];
- }
- // Delete bates action
- - (void)deleteBatesAction {
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Delete Bates" message:@"Are you sure delete all Bates" preferredStyle:UIAlertControllerStyleAlert];
-
- UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
-
- [self deleteBates];
- }];
-
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
- NSLog(@"Cancel Action");
- }];
-
- [alertController addAction:okAction];
- [alertController addAction:cancelAction];
-
- [self presentViewController:alertController animated:YES completion:nil];
- }
- #pragma mark - CPDFModelDataDelegate
- // Get headerfooter' model to setting page headerfooter's add functional property
- - (void)changeHeaderFooterModelData:(CPDFHeaderFooterModel *)modelData {
- CPDFHeaderFooter *headerFooter = [self.document headerFooter];
- NSString *pageIndex = [NSString stringWithFormat:@"0-%lu",self.document.pageCount - 1];
- NSString *text = [[NSString alloc] init];
- switch (modelData.fontSelcet) {
- case 0:
- text = [NSString stringWithFormat:@"<<%@>>",modelData.pageStart];
- break;
- case 1:
- text = [NSString stringWithFormat:@"page <<%@>>",modelData.pageStart];
- break;
- case 2:
- text = [NSString stringWithFormat:@"<<%@>>/%lu",modelData.pageStart,self.document.pageCount];
- break;
- case 3:
- text = [NSString stringWithFormat:@"<<%@>> of %lu",modelData.pageStart,self.document.pageCount];
- break;
- case 4:
- text = [NSString stringWithFormat:@"page <<%@>> of %lu",modelData.pageStart,self.document.pageCount];
- break;
- default:
- break;
- }
- headerFooter.pageString = pageIndex;
- [headerFooter setText:text atIndex:modelData.fontPosition];
- [headerFooter setFontSize:modelData.fontSize atIndex:modelData.fontPosition];
- [headerFooter setTextColor:modelData.fontColor atIndex:modelData.fontPosition];
- [headerFooter update];
- NSURL *url = [NSURL fileURLWithPath:self.path];
- [self.document writeToURL:url];
-
- _document = [[CPDFDocument alloc] initWithURL:url];
- _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
- _pdfView.document = _document;
- _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
- [self.view addSubview:_pdfView];
- }
- // Get bates' model to setting page bates's add functional property
- - (void)changBatesModelData:(CPDFHeaderFooterModel *)modelData {
- CPDFHeaderFooterModel *modelBatesData = modelData;
-
- CPDFBates *bates = [self.document bates];
-
- NSString *pageIndex = [NSString stringWithFormat:@"%@-%lu",modelBatesData.pageStart,self.document.pageCount - 1];
- bates.pageString = pageIndex;
- NSString *text = [NSString stringWithFormat:@"<<%@>>",modelBatesData.fontText];
- [bates setText:text atIndex:modelBatesData.fontPosition];
- [bates setFontName:modelBatesData.fontName atIndex:modelBatesData.fontPosition];
- [bates setFontSize:15.0 atIndex:modelBatesData.fontPosition];
- [bates setTextColor:modelBatesData.fontColor atIndex:modelBatesData.fontPosition];
- [bates update];
-
- NSURL *url = [NSURL fileURLWithPath:self.path];
- [self.document writeToURL:url];
-
- _document = [[CPDFDocument alloc] initWithURL:url];
- _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
- _pdfView.document = _document;
- _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
-
- [self.view addSubview:_pdfView];
- }
- #pragma mark - Delete Methods
- // Get headerfooter' model to setting page headerfooter's delete functional property
- - (void)deleteHeaderFooter {
- CPDFHeaderFooter *headerFooter = [self.document headerFooter];
- [headerFooter clear];
-
- NSURL *url = [NSURL fileURLWithPath:self.path];
- [self.document writeToURL:url];
- _document = [[CPDFDocument alloc] initWithURL:url];
- _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
- _pdfView.document = _document;
- _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
-
- [self.view addSubview:_pdfView];
- }
- // Get bates' model to setting page bates's delete functional property
- - (void)deleteBates {
- CPDFBates *bates = [self.document bates];
- [bates clear];
-
- NSURL *url = [NSURL fileURLWithPath:self.path];
- [self.document writeToURL:url];
- _document = [[CPDFDocument alloc] initWithURL:url];
- _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
- _pdfView.document = _document;
- _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
-
- [self.view addSubview:_pdfView];
-
- #pragma mark - WaterMarkModelDataDelegate
- - (void)changeTextModel:(CPDFDataModel *)dataModel {
- _textWatermark = [[CPDFWatermark alloc] initWithDocument:_pdfDocument type:CPDFWatermarkTypeText];
-
- _textWatermark.text = dataModel.text;
- _textWatermark.textColor = dataModel.textColor;
-
- _textWatermark.opacity = dataModel.watermarkOpacity;
-
- _textWatermark.scale = dataModel.watermarkScale * 2.5;
-
- _textWatermark.isTilePage = dataModel.isTile;
-
- _textWatermark.tx = dataModel.tx * _imageSize.width / self.addWaterMarkViewController.textViewController.textPreview.documentView.frame.size.width;
- _textWatermark.ty = dataModel.ty * _imageSize.height / self.addWaterMarkViewController.textViewController.textPreview.documentView.frame.size.height;
-
- _textWatermark.rotation = dataModel.watermarkRotation;
-
- if (_textWatermark.isTilePage) {
- _textWatermark.verticalSpacing = dataModel.verticalSpacing;
- _textWatermark.horizontalSpacing = dataModel.horizontalSpacing;
- }
-
- if (dataModel.pageString) {
- _textWatermark.pageString = dataModel.pageString;
- }
-
- [_pdfView.document addWatermark:_textWatermark];
- [_pdfView layoutDocumentView];
- NSURL *url = [NSURL fileURLWithPath:_path];
- [_pdfView.document writeToURL:url];
- }
- - (void)changeImageModel:(CPDFDataModel *)dataModel {
- _imageWatermark = [[CPDFWatermark alloc] initWithDocument:_pdfDocument type:CPDFWatermarkTypeImage];
-
- _imageWatermark.image = dataModel.image;
-
- _imageWatermark.opacity = dataModel.watermarkOpacity;
-
- _imageWatermark.scale = dataModel.watermarkScale * 2;
-
- _imageWatermark.isTilePage = dataModel.isTile;
-
- _imageWatermark.tx = dataModel.tx * _imageSize.width / self.addWaterMarkViewController.imageViewController.imagePreview.documentView.frame.size.width;
- _imageWatermark.ty = dataModel.ty * _imageSize.height / self.addWaterMarkViewController.imageViewController.imagePreview.documentView.frame.size.height;
-
- _imageWatermark.rotation = dataModel.watermarkRotation;
-
- if (_imageWatermark.isTilePage) {
- _imageWatermark.verticalSpacing = dataModel.verticalSpacing;
- _imageWatermark.horizontalSpacing = dataModel.horizontalSpacing;
- }
-
- if (dataModel.pageString) {
- _imageWatermark.pageString = dataModel.pageString;
- }
-
- [_pdfView.document addWatermark:_imageWatermark];
- [_pdfView layoutDocumentView];
- NSURL *url = [NSURL fileURLWithPath:_path];
- [_pdfView.document writeContentToURL:url];
- }
- - (void)deleteTextModel:(CPDFWatermark *)waterMark {
- [self.pdfDocument removeWatermark:waterMark];
- NSURL *url = [NSURL fileURLWithPath:_path];
- [_pdfView.document writeToURL:url];
-
- _pdfDocument = [[CPDFDocument alloc] initWithURL:url];
- _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
- _pdfView.document = _pdfDocument;
- _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
- [self.view addSubview:_pdfView];
- }
- #pragma mark - BackgroundDataModelDelegate
- - (void)changeBackgroundModel:(CPDFBackgroundModel *)dataModel {
- CPDFBackground *pageBackground = [_pdfDocument background];
-
- pageBackground.color = dataModel.backgroundColor;
- pageBackground.scale = dataModel.backgroudScale;
- pageBackground.rotation = dataModel.backgroundRotation;
- pageBackground.opacity = dataModel.backgroundOpacity;
- pageBackground.pageString = dataModel.pageString;
- pageBackground.xOffset = dataModel.horizontalSpacing;
- pageBackground.yOffset = dataModel.verticalSpacing;
- pageBackground.isAllowsView = YES;
- pageBackground.isAllowsPrint = YES;
- [pageBackground setImage:dataModel.image];
-
- [pageBackground update];
-
- NSURL *url = [NSURL fileURLWithPath:_path];
- [_pdfView.document writeToURL:url];
-
- _pdfDocument = [[CPDFDocument alloc] initWithURL:url];
- _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
- _pdfView.document = _pdfDocument;
- _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
- [self.view addSubview:_pdfView];
- }
- - (void)deleteBackgroundModel {
- CPDFBackground *pageBackground = [_pdfDocument background];
-
- [pageBackground clear];
-
- NSURL *url = [NSURL fileURLWithPath:_path];
- [_pdfView.document writeToURL:url];
-
- _pdfDocument = [[CPDFDocument alloc] initWithURL:url];
- _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
- _pdfView.document = _pdfDocument;
- _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
- [self.view addSubview:_pdfView];
- }
- @end
|