123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- //
- // 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 "Masonry.h"
- #import "CPDFAddViewController.h"
- @interface CPDFViewController () <UIActionSheetDelegate,WaterMarkModelDataDelegate>
- @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];
- 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
|