// // CPDFViewController.m // PDFViewer // // Created by kdan on 2022/11/15. // #import #import #import #import "CPDFViewController.h" #import "CPDFTextPreview.h" #import "CPDFImagePreview.h" #import "CPDFTextView.h" #import "CPDFImageView.h" #import "CPDFDataModel.h" #import "Masonry.h" #import "CPDFAddViewController.h" @interface CPDFViewController () @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) 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]; _pdfDocument = [[CPDFDocument alloc] initWithURL:url]; _pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds]; _pdfView.document = _pdfDocument; _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; [self.view addSubview:_pdfView]; CPDFPage *pdfPage = [_pdfView.document pageAtIndex:0]; _imageSize = [_pdfView.document pageSizeAtIndex:0]; _image = [pdfPage thumbnailOfSize:_imageSize]; } #pragma mark - Actions - (void)onClickedOkbtn { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *addWatermarkAction = [UIAlertAction actionWithTitle:@"Add Watermark" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { self.addWaterMarkViewController = [[CPDFAddViewController alloc] initWithImage:self.image withDocument:self.pdfDocument]; self.addWaterMarkViewController.delegate = self; [self.navigationController pushViewController:self.addWaterMarkViewController animated:YES]; }]; 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]; } #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; _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 = [self.addWaterMarkViewController.textViewController.textView.verticalField.text floatValue]; _textWatermark.horizontalSpacing = [self.addWaterMarkViewController.textViewController.textView.horizontalField.text floatValue]; } 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; _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 = [self.addWaterMarkViewController.imageViewController.imageView.verticalField.text floatValue]; _imageWatermark.horizontalSpacing = [self.addWaterMarkViewController.imageViewController.imageView.horizontalField.text floatValue]; } if (dataModel.pageString) { _imageWatermark.pageString = dataModel.pageString; } [_pdfView.document addWatermark:_imageWatermark]; [_pdfView layoutDocumentView]; // NSURL *url = [NSURL fileURLWithPath:_path]; // [_pdfView.document writeContentToURL:url]; } @end