1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726 |
- //
- // PDFViewController.m
- // PDFReader
- //
- // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved.
- //
- // The PDF Reader Sample applications are licensed with a modified BSD license.
- // Please see License for details. This notice may not be removed from this file.
- //
- #import "PDFViewController.h"
- #import "PDFNoteViewController.h"
- #import "PDFColorViewController.h"
- #import "PDFShapeViewController.h"
- #import "PDFStampViewController.h"
- #import "PDFLinkViewController.h"
- #import "PDFSignatureViewController.h"
- #import "PDFViewerModeViewController.h"
- #import "PDFSearchViewController.h"
- #import "PDFBOTAViewController.h"
- #import "PDFPageEditViewController.h"
- #import <ComPDFKit/CPDFWatermark.h>
- #import <ComPDFKit/CPDFHeaderFooter.h>
- #import "PDFWatermarkDataModel.h"
- #import "PDFHeaderFooterAddController.h"
- #import "PDFBatesAddViewController.h"
- #import "PDFHeaderFooterSettingView.h"
- #import "PDFBackgroundSettingViewController.h"
- #import "PDFBackgroundModel.h"
- #import "PDFAddWaterMarkViewController.h"
- #import "PDFWatermarkTextPreview.h"
- #import "PDFWatermarkImagePreview.h"
- #import "PDFToolbar.h"
- #import "PDFFreehandView.h"
- #import "PDFSlider.h"
- #import "PDFBottomToolbar.h"
- #import "PDFTitleView.h"
- #import "PDFKeyboardToolbar.h"
- @interface PDFViewController () <UIPopoverPresentationControllerDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, CPDFViewDelegate, PDFListViewDelegate, PDFKeyboardDelegate, PDFModelDataDelegate, BackgroundDataModelDelegate, WaterMarkModelDataDelegate>
- @property (nonatomic,retain) NSString *filePath;
- @property (nonatomic,retain) PDFListView *pdfView;
- @property (nonatomic,retain) UIActivityIndicatorView *activityIndicatorView;
- @property (nonatomic,retain) PDFToolbar *annotationToolbar;
- @property (nonatomic,retain) PDFBottomToolbar *bottomToolbar;
- @property (nonatomic,retain) PDFFreehandView *freehandView;
- @property (nonatomic,retain) PDFSlider *pageSlider;
- @property (nonatomic,retain) PDFSearchViewController *searchViewController;
- @property (nonatomic,retain) PDFBOTAViewController *botaViewController;
- @property (nonatomic,retain) UILabel *pageNumIndicator;
- @property (nonatomic,retain) UILabel *informationIndicator;
- @property (nonatomic,retain) UIButton *pageBackButton;
- @property (nonatomic,assign) NSInteger lastPageIndex;
- @property (nonatomic,assign) BOOL isCreateFromToolbar;
- @property (nonatomic,retain) CPDFFreeTextAnnotation *annotationFreeText;
- // Demo property
- @property (nonatomic,retain) UIImage *image;
- @property (nonatomic,retain) PDFHeaderFooterSettingView *headerFooterSetting;
- @property (nonatomic,retain) PDFHeaderFooterSettingView *batesSetting;
- @property (nonatomic,retain) PDFAddWaterMarkViewController *addWaterMarkViewController;
- @end
- @implementation PDFViewController
- #pragma mark - Initializers
- - (instancetype)initWithFilePath:(NSString *)filePath {
- if (self = [super init]) {
- self.filePath = filePath;
- }
- return self;
- }
- - (void)dealloc {
- [_annotationFreeText release];
- [_informationIndicator release];
- [_pageBackButton release];
- [_pageSlider release];
- [_pageNumIndicator release];
- [_botaViewController release];
- [_searchViewController release];
- [_freehandView release];
- [_bottomToolbar release];
- [_annotationToolbar release];
- [_activityIndicatorView release];
- [_pdfView release];
- [_filePath release];
-
- // Demo property release
- [_image release];
- [_headerFooterSetting release];
- [_batesSetting release];
- [_addWaterMarkViewController release];
-
- [super dealloc];
- }
- #pragma mark - Accessors
- - (PDFListView *)pdfView {
- if (!_pdfView) {
- _pdfView = [[PDFListView alloc] initWithFrame:self.view.bounds];
- _pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
- _pdfView.delegate = self;
- _pdfView.performDelegate = self;
- _pdfView.performDelegate = self;
- }
- return _pdfView;
- }
- - (UIActivityIndicatorView *)activityIndicatorView {
- if (!_activityIndicatorView) {
- _activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
- _activityIndicatorView.center = self.view.center;
- _activityIndicatorView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
- }
- [_activityIndicatorView startAnimating];
- return _activityIndicatorView;
- }
- #pragma mark - UIViewController Methods
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- // Regarding the transparent navigation bar, if you want the PDFViewer to appear below the navigation bar (safe area), set this parameter to NO
- self.navigationController.navigationBar.translucent = YES;
- self.automaticallyAdjustsScrollViewInsets = NO;
-
- // self.navigationController.navigationBar.translucent = NO;
- // self.navigationController.navigationBar.barTintColor = UIColor.whiteColor;
-
- // Do any additional setup after loading the view.
- UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [backBtn setImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];
- [backBtn addTarget:self action:@selector(buttonItemClicked_Back:) forControlEvents:UIControlEventTouchUpInside];
- [backBtn setExclusiveTouch:YES];
- [backBtn sizeToFit];
- UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
- self.navigationItem.leftBarButtonItem = backItem;
- [backItem release];
- self.view.backgroundColor = [UIColor whiteColor];
- [self.view addSubview:self.pdfView];
- [self loadDocumentWithFilePath:self.filePath completion:nil];
-
- _headerFooterSetting = [[PDFHeaderFooterSettingView alloc] init];
- [_headerFooterSetting setText];
-
- _batesSetting = [[PDFHeaderFooterSettingView alloc] init];
- [_batesSetting setText];
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
-
- [self becomeFirstResponder];
- }
- - (void)loadDocumentWithFilePath:(NSString *)filePath completion:(void (^)(BOOL result))completion {
- self.title = [[filePath lastPathComponent] stringByDeletingPathExtension];
-
- self.pdfView.document = nil;
-
- [self.view addSubview:self.activityIndicatorView];
- [self.navigationController.view setUserInteractionEnabled:NO];
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- NSURL *url = [NSURL fileURLWithPath:self.filePath];
- CPDFDocument *document = [[[CPDFDocument alloc] initWithURL:url] autorelease];
- dispatch_async(dispatch_get_main_queue(), ^{
- [self.navigationController.view setUserInteractionEnabled:YES];
- [self.activityIndicatorView removeFromSuperview];
- self.activityIndicatorView = nil;
- if (document.error && document.error.code != CPDFDocumentPasswordError) {
- UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- [self.navigationController popViewControllerAnimated:YES];
- }];
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
- message:NSLocalizedString(@"Sorry PDF Reader Can't open this pdf file!", nil)
- preferredStyle:UIAlertControllerStyleAlert];
- [alert addAction:okAction];
- [self presentViewController:alert animated:YES completion:nil];
-
- if (completion) {
- completion(NO);
- }
- } else {
- self.pdfView.document = document;
-
- if (_image == nil) {
- CPDFPage *pdfPage = [_pdfView.document pageAtIndex:0];
- [pdfPage retain];
- CGSize imageSize = [_pdfView.document pageSizeAtIndex:0];
- _image = [pdfPage thumbnailOfSize:imageSize];
- [_image retain];
- [pdfPage release];
- }
-
- if (completion) {
- completion(YES);
- }
- }
- });
- });
- }
- - (void)buttonItemClicked_Back:(id)sender {
- if (self.pdfView.document.isModified) {
- [self.pdfView.document writeToURL:self.pdfView.document.documentURL];
- }
- [self.navigationController popViewControllerAnimated:YES];
- }
- #pragma mark - CPDFViewDelegate
- - (void)PDFViewDocumentDidLoaded:(CPDFView *)pdfView {
- if (self.pdfView.document.isEncrypted) {
- UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
- style:UIAlertActionStyleCancel
- handler:nil];
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Attention", nil)
- message:NSLocalizedString(@"This encrypted file can not be annotated because its format is under password protection.", nil)
- preferredStyle:UIAlertControllerStyleAlert];
- [alert addAction:okAction];
- [self presentViewController:alert animated:YES completion:nil];
- } else {
- [self.view addSubview:self.annotationToolbar];
- }
- [self.view addSubview:self.pageSlider];
-
- UIBarButtonItem *moreItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_more.png"]
- style:UIBarButtonItemStylePlain
- target:self
- action:@selector(buttonItemClicked_More:)];
-
- UIBarButtonItem *settingsItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_viewer_mode00.png"]
- style:UIBarButtonItemStylePlain
- target:self
- action:@selector(buttonItemClicked_ViewerMode:)];
-
- UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_search2_01.png"]
- style:UIBarButtonItemStylePlain
- target:self
- action:@selector(buttonItemClicked_Search:)];
-
- UIBarButtonItem *botaItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_bota.png"]
- style:UIBarButtonItemStylePlain
- target:self
- action:@selector(buttonItemClicked_BOTA:)];
-
- UIBarButtonItem *pageEditItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_pageedit.png"]
- style:UIBarButtonItemStylePlain
- target:self
- action:@selector(buttonItemClicked_PageEdit:)];
- if (UIUserInterfaceIdiomPhone == UI_USER_INTERFACE_IDIOM()) {
- self.navigationItem.rightBarButtonItems = @[moreItem, settingsItem, botaItem, searchItem, pageEditItem];
-
- PDFTitleView *titleView = [[PDFTitleView alloc] initWithTitle:self.title];
- [titleView showInView:self.view hideAfter:3.0 complete:nil];
- [titleView release];
-
- self.title = nil;
- } else {
- self.navigationItem.rightBarButtonItems = @[moreItem, botaItem, searchItem, pageEditItem];
- [self.view addSubview:self.bottomToolbar];
- }
-
- [settingsItem release];
- [searchItem release];
- [botaItem release];
-
- [self PDFViewCurrentPageDidChanged:self.pdfView];
- }
- - (void)PDFViewCurrentPageDidChanged:(CPDFView *)pdfView {
- if (!_pageNumIndicator) {
- if (UIUserInterfaceIdiomPhone == UI_USER_INTERFACE_IDIOM()) {
- _pageNumIndicator = [[UILabel alloc] initWithFrame:CGRectMake(6, self.view.frame.size.height-84, 60, 30)];
- _pageNumIndicator.autoresizingMask = UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin;
- } else {
- _pageNumIndicator = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width-60)/2.0, self.view.frame.size.height-84, 60, 30)];
- _pageNumIndicator.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin;
- }
- _pageNumIndicator.textAlignment = NSTextAlignmentCenter;
- _pageNumIndicator.font = [UIFont boldSystemFontOfSize:14.0f];
- _pageNumIndicator.textColor = [UIColor whiteColor];
- _pageNumIndicator.backgroundColor = [UIColor blackColor];
- _pageNumIndicator.alpha = 0.4;
- _pageNumIndicator.layer.masksToBounds = YES;
- _pageNumIndicator.layer.cornerRadius = 8.0f;
- _pageNumIndicator.hidden = YES;
- }
-
- [self.view addSubview:self.pageNumIndicator];
- self.pageNumIndicator.text = [NSString stringWithFormat:@"%ld / %ld",(long)[self.pdfView currentPageIndex]+1, (long)[self.pdfView.document pageCount]];
- CGSize size = [self.pageNumIndicator sizeThatFits:CGSizeMake(200, self.pageNumIndicator.frame.size.height)];
- self.pageNumIndicator.frame = CGRectMake(self.pageNumIndicator.frame.origin.x,
- self.pageNumIndicator.frame.origin.y,
- size.width+15, self.pageNumIndicator.frame.size.height);
-
- if (!_informationIndicator) {
- _informationIndicator = [[UILabel alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-84, 60, 30)];
- _informationIndicator.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleTopMargin;
- _informationIndicator.textAlignment = NSTextAlignmentCenter;
- _informationIndicator.font = [UIFont boldSystemFontOfSize:14.0f];
- _informationIndicator.textColor = [UIColor whiteColor];
- _informationIndicator.backgroundColor = [UIColor blackColor];
- _informationIndicator.alpha = 0.4;
- _informationIndicator.layer.masksToBounds = YES;
- _informationIndicator.layer.cornerRadius = 8.0f;
- _informationIndicator.hidden = YES;
- }
-
- [self.view addSubview:self.informationIndicator];
- self.informationIndicator.text = [NSString stringWithFormat:@"Powered by ComPDFKit"];
- size = [self.informationIndicator sizeThatFits:CGSizeMake(200, self.informationIndicator.frame.size.height)];
- self.informationIndicator.frame = CGRectMake(self.view.frame.size.width-(size.width+15)-6,
- self.informationIndicator.frame.origin.y,
- size.width+15, self.informationIndicator.frame.size.height);
-
- [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hidePageNumIndicator) object:nil];
- if (UIUserInterfaceIdiomPhone == UI_USER_INTERFACE_IDIOM()) {
- [self showPageNumIndicator];
- [self performSelector:@selector(hidePageNumIndicator) withObject:nil afterDelay:0.5];
- } else {
- if (self.bottomToolbar.alpha == 0) {
- [self showPageNumIndicator];
- [self performSelector:@selector(hidePageNumIndicator) withObject:nil afterDelay:0.5];
- }
- [self.bottomToolbar reloadData];
- }
-
- [self.pageSlider reloadData];
- }
- - (void)PDFViewDidClickOnLink:(CPDFView *)pdfView withURL:(NSString *)url {
- if(url){
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
- } else {
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
- style:UIAlertActionStyleCancel
- handler:nil];
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Error", nil)
- message:NSLocalizedString(@"The hyperlink is invalid.", nil)
- preferredStyle:UIAlertControllerStyleAlert];
- [alert addAction:cancelAction];
- [self presentViewController:alert animated:YES completion:nil];
- }
- }
- - (void)PDFViewPerformURL:(CPDFView *)pdfView withContent:(NSString *)content {
- if (content) {
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:content]];
- } else {
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
- style:UIAlertActionStyleCancel
- handler:nil];
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Error", nil)
- message:NSLocalizedString(@"The hyperlink is invalid.", nil)
- preferredStyle:UIAlertControllerStyleAlert];
- [alert addAction:cancelAction];
- [self presentViewController:alert animated:YES completion:nil];
- }
- }
- - (void)PDFViewPerformPrint:(CPDFView *)pdfView {
- NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"flatten.pdf"];
- [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
- BOOL result = [self.pdfView.document writeFlattenToURL:[NSURL fileURLWithPath:filePath]];
- if (result) {
- if ([UIPrintInteractionController isPrintingAvailable]) {
- UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
- UIPrintInfo *printInfo = [UIPrintInfo printInfo];
- printInfo.outputType = UIPrintInfoOutputGeneral;
- printInfo.jobName = self.filePath;
- printInfo.duplex = UIPrintInfoDuplexLongEdge;
- controller.printInfo = printInfo;
- controller.showsPageRange = YES;
- [controller setPrintingItem:[NSURL fileURLWithPath:filePath]];
- [controller presentAnimated:YES completionHandler:nil];
- }
- }
- }
- - (void)PDFViewPerformReset:(CPDFView *)pdfView {
- UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Yes", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
- [self.pdfView.document resetForm];
- [self.pdfView setNeedsDisplayForVisiblePages];
- }];
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"No", nil) style:UIAlertActionStyleCancel handler:nil];
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
- message:[NSString stringWithFormat:NSLocalizedString(@"Do you really want to reset the form?", nil)]
- preferredStyle:UIAlertControllerStyleAlert];
- [alert addAction:okAction];
- [alert addAction:cancelAction];
- [self presentViewController:alert animated:YES completion:nil];
- }
- - (void)PDFViewShouldBeginEditing:(CPDFView *)pdfView textView:(UITextView *)textView forAnnotation:(CPDFFreeTextAnnotation *)annotation {
- self.annotationFreeText = annotation;
- PDFKeyboardToolbar *keyboardToolbar = [[[PDFKeyboardToolbar alloc] initWithFontName:annotation.font.fontName fontSize:annotation.font.pointSize] autorelease];
- keyboardToolbar.delegate = self;
- [keyboardToolbar refreshFontName:annotation.font.fontName fontSize:annotation.font.pointSize opacity:annotation.opacity];
- [keyboardToolbar bindToTextView:textView];
- }
- - (void)PDFViewShouldEndEditing:(CPDFView *)pdfView textView:(UITextView *)textView forAnnotation:(CPDFFreeTextAnnotation *)annotation {
-
- }
- /*
- 1 - PDFViewer will call back this method when the user zooms or drags the PDFViewer to stop.
- 2 - [self.pdfView goToDestination:<#(CPDFDestination *)#> animated:<#(BOOL)#>];
- */
- - (void)PDFViewDidEndDragging:(CPDFView *)pdfView {
- // CPDFDestination *currentDestination = self.pdfView.currentDestination;
- }
- #pragma mark - PDFListViewDelegate
- - (void)PDFViewPerformOpenNote:(PDFListView *)pdfView forAnnotation:(CPDFAnnotation *)annotation {
- CGRect rect = [pdfView convertRect:annotation.bounds fromPage:annotation.page];
- PDFNoteViewController *vc = [[PDFNoteViewController alloc] init];
- [vc setContent:annotation.contents];
- vc.callback = ^(NSString *string, BOOL isDelete) {
- if (isDelete) {
- [annotation.page removeAnnotation:annotation];
- [self.pdfView setNeedsDisplayForPage:annotation.page];
- } else {
- annotation.contents = string;
- }
- };
- [vc showViewController:self inRect:rect];
- [vc release];
- }
- - (void)PDFViewPerformChangeColor:(PDFListView *)pdfView forAnnotation:(CPDFAnnotation *)annotation {
- CGRect rect = [pdfView convertRect:annotation.bounds fromPage:annotation.page];
- if ([annotation isKindOfClass:[CPDFCircleAnnotation class]] ||
- [annotation isKindOfClass:[CPDFSquareAnnotation class]]) {
- __block PDFShapeViewController *vc = [[PDFShapeViewController alloc] init];
- vc.color = annotation.color;
- vc.opacity = annotation.opacity*100;
- vc.fillColor = [(CPDFCircleAnnotation *)annotation interiorColor];
- if ([(CPDFCircleAnnotation *)annotation interiorOpacity] > 0) {
- vc.fillOpacity = [(CPDFCircleAnnotation *)annotation interiorOpacity]*100;
- } else {
- vc.fillOpacity = 0;
- }
- vc.borderWidth = annotation.borderWidth;
- vc.callback = ^{
- annotation.color = vc.color;
- annotation.opacity = vc.opacity/100;
- [(CPDFCircleAnnotation *)annotation setInteriorColor:vc.fillColor];
- [(CPDFCircleAnnotation *)annotation setInteriorOpacity:vc.fillOpacity/100];
- annotation.borderWidth = vc.borderWidth;
- [self.pdfView setNeedsDisplayForPage:annotation.page];
- };
- [vc showViewController:self inRect:rect];
- [vc release];
- } else {
- __block PDFColorViewController *vc = [[PDFColorViewController alloc] init];
- vc.color = annotation.color;
- vc.opacity = annotation.opacity*100;
- vc.callback = ^{
- annotation.color = vc.color;
- annotation.opacity = vc.opacity/100;
- [self.pdfView setNeedsDisplayForPage:annotation.page];
- };
- [vc showViewController:self inRect:rect];
- [vc release];
- }
- }
- - (void)PDFViewPerformShare:(PDFListView *)pdfView forAnnotation:(CPDFMarkupAnnotation *)annotation {
- CGRect rect = [pdfView convertRect:annotation.bounds fromPage:annotation.page];
- NSString *text = [annotation markupText] ? : @"";
- UIActivityViewController *vc = [[UIActivityViewController alloc] initWithActivityItems:@[text] applicationActivities:nil];
- vc.popoverPresentationController.sourceView = self.view;
- vc.popoverPresentationController.sourceRect = rect;
- [self presentViewController:vc animated:YES completion:nil];
- [vc release];
- }
- - (void)PDFViewPerformSave:(PDFListView *)pdfView forAnnotation:(CPDFStampAnnotation *)annotation {
- if ([annotation stampImage]) {
- UIImageWriteToSavedPhotosAlbum([annotation stampImage], self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
- }
- }
- - (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
- if (!error) {
- dispatch_async(dispatch_get_main_queue(), ^{
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
- message:NSLocalizedString(@"This image has been saved to the photo library.", nil)
- preferredStyle:UIAlertControllerStyleAlert];
- [self presentViewController:alert animated:YES completion:nil];
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [alert dismissViewControllerAnimated:YES completion:nil];
- });
- });
- }
- }
- - (void)PDFViewPerformPopup:(PDFListView *)pdfView forAnnotation:(CPDFMarkupAnnotation *)annotation {
- CGRect rect = [pdfView convertRect:annotation.bounds fromPage:annotation.page];
- PDFNoteViewController *vc = [[PDFNoteViewController alloc] init];
- [vc setContent:annotation.contents];
- vc.callback = ^(NSString *string, BOOL isDelete) {
- if (isDelete) {
- annotation.contents = @"";
- [annotation removePopup];
- [self.pdfView setNeedsDisplayForPage:annotation.page];
- } else {
- annotation.contents = string;
- }
- };
- [vc showViewController:self inRect:rect];
- [vc release];
- }
- - (void)PDFViewPerformEditLink:(PDFListView *)pdfView forAnnotation:(CPDFLinkAnnotation *)annotation {
- PDFLinkViewController *vc = [[PDFLinkViewController alloc] init];
- NSString *url = annotation.URL;
- CPDFDestination *destination = annotation.destination;
- if (url) {
- if ([url hasPrefix:@"mailto:"]) {
- vc.type = 2;
- vc.content = [url substringFromIndex:7];
- } else {
- vc.type = 0;
- vc.content = url;
- }
- } else if (destination) {
- vc.type = 1;
- vc.content = [NSString stringWithFormat:@"%@", @(destination.pageIndex+1)];
- }
- vc.callback = ^(NSString *content, NSInteger type) {
- if (content) {
- if (type == 0) {
- annotation.URL = content;
- } else if (type == 1) {
- annotation.destination = [[[CPDFDestination alloc] initWithDocument:pdfView.document
- pageIndex:[content integerValue]-1
- atPoint:CGPointZero
- zoom:1] autorelease];
- } else if (type == 2) {
- annotation.URL = [NSString stringWithFormat:@"mailto:%@", content];
- }
- } else {
- pdfView.activeAnnotation = nil;
- [pdfView setNeedsDisplayForPage:annotation.page];
- }
- };
- vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
- [self presentViewController:vc animated:YES completion:nil];
- [vc release];
- }
- - (void)PDFViewPerformSignatureWidget:(PDFListView *)pdfView forAnnotation:(CPDFSignatureWidgetAnnotation *)annotation {
- PDFSignatureViewController *vc = [[PDFSignatureViewController alloc] init];
- vc.callback = ^(UIImage *image) {
- [annotation signWithImage:image];
- [pdfView setNeedsDisplayForPage:annotation.page];
- };
- UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
- nav.modalPresentationStyle = UIModalPresentationFormSheet;
- [self presentViewController:nav animated:YES completion:nil];
- [nav release];
- [vc release];
- }
- - (void)PDFViewPerformShare:(PDFListView *)pdfView forSelection:(CPDFSelection *)selection {
- CGRect rect = [pdfView convertRect:selection.bounds fromPage:selection.page];
- NSString *text = [selection string] ? : @"";
- UIActivityViewController *vc = [[UIActivityViewController alloc] initWithActivityItems:@[text] applicationActivities:nil];
- vc.popoverPresentationController.sourceView = self.view;
- vc.popoverPresentationController.sourceRect = rect;
- [self presentViewController:vc animated:YES completion:nil];
- [vc release];
- }
- - (void)PDFViewPerformDefine:(PDFListView *)pdfView forSelection:(CPDFSelection *)selection {
- NSString *word = selection.string;
- word = [word stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
- word = [word stringByTrimmingCharactersInSet:[NSCharacterSet controlCharacterSet]];
- word = [word stringByTrimmingCharactersInSet:[NSCharacterSet punctuationCharacterSet]];
-
- CGRect rect = [self.pdfView convertRect:selection.bounds fromPage:selection.page];
- UIReferenceLibraryViewController *reference = [[[UIReferenceLibraryViewController alloc] initWithTerm:word] autorelease];
- reference.modalPresentationStyle = UIModalPresentationPopover;
- UIPopoverPresentationController *popVC = reference.popoverPresentationController;
- popVC.delegate = self;
- popVC.sourceRect = rect;
- popVC.sourceView = self.pdfView;
- [self presentViewController:reference animated:YES completion:nil];
- }
- - (void)PDFViewPerformGoogleSearch:(PDFListView *)pdfView forSelection:(CPDFSelection *)selection {
- NSString *string = [[NSString stringWithFormat:@"http://www.google.com/search?q=%@&hl=en", selection.string] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
- NSURL *url = [NSURL URLWithString:string];
- [[UIApplication sharedApplication] openURL:url];
- }
- - (void)PDFViewPerformWikiSearch:(PDFListView *)pdfView forSelection:(CPDFSelection *)selection {
- NSString *string = [[NSString stringWithFormat:@"http://en.wikipedia.org/wiki/%@", selection.string] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
- NSURL *url = [NSURL URLWithString:string];
- [[UIApplication sharedApplication] openURL:url];
- }
- - (void)PDFViewPerformAddSignture:(PDFListView *)pdfView atPoint:(CGPoint)point forPage:(CPDFPage *)page {
- PDFSignatureViewController *vc = [[PDFSignatureViewController alloc] init];
- vc.callback = ^(UIImage *image) {
- CPDFSignatureAnnotation *annotation = [[[CPDFSignatureAnnotation alloc] initWithDocument:pdfView.document] autorelease];
- [annotation setImage:image];
- CGRect bounds = annotation.bounds;
- bounds.origin.x = point.x-bounds.size.width/2.0;
- bounds.origin.y = point.y-bounds.size.height/2.0;
- annotation.bounds = bounds;
- [self.pdfView addAnnotation:annotation forPage:page];
- };
- UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
- nav.modalPresentationStyle = UIModalPresentationFormSheet;
- [self presentViewController:nav animated:YES completion:nil];
- [nav release];
- [vc release];
- }
- - (void)PDFViewPerformAddStamp:(PDFListView *)pdfView atPoint:(CGPoint)point forPage:(CPDFPage *)page {
- CGPoint p = [self.pdfView convertPoint:point fromPage:page];
- CGRect rect = CGRectMake(p.x, p.y, 2, 2);
- PDFStampViewController *vc = [[PDFStampViewController alloc] init];
- vc.callback = ^(NSInteger selectedIndex, NSDictionary *stamp) {
- if (selectedIndex != -1) {
- if (stamp) {
- if (stamp[PDFAnnotationStampKeyImagePath]) {
- UIImage *image = [UIImage imageWithContentsOfFile:stamp[PDFAnnotationStampKeyImagePath]];
- CPDFStampAnnotation *annotation = [[[CPDFStampAnnotation alloc] initWithDocument:pdfView.document image:image] autorelease];
- CGRect bounds = annotation.bounds;
- bounds.origin.x = point.x-bounds.size.width/2.0;
- bounds.origin.y = point.y-bounds.size.height/2.0;
- annotation.bounds = bounds;
- [self.pdfView addAnnotation:annotation forPage:page];
- } else {
- NSString *stampText = stamp[PDFAnnotationStampKeyText];
- BOOL stampShowDate = [stamp[PDFAnnotationStampKeyShowDate] boolValue];
- BOOL stampShowTime = [stamp[PDFAnnotationStampKeyShowTime] boolValue];
- CPDFStampStyle stampStyle = [stamp[PDFAnnotationStampKeyStyle] integerValue];
- CPDFStampShape stampShape = [stamp[PDFAnnotationStampKeyShape] integerValue];
-
- NSString *detailText = nil;
- NSTimeZone *timename = [NSTimeZone systemTimeZone];
- NSDateFormatter *outputFormatter = [[[NSDateFormatter alloc] init] autorelease];
- [outputFormatter setTimeZone:timename];
- if (stampShowDate && !stampShowTime){
- detailText = [NSDateFormatter localizedStringFromDate:[NSDate date]
- dateStyle:NSDateFormatterShortStyle
- timeStyle:NSDateFormatterNoStyle];
- } else if (stampShowTime && !stampShowDate) {
- [outputFormatter setDateFormat:@"HH:mm:ss"];
- detailText = [outputFormatter stringFromDate:[NSDate date]];
- } else if (stampShowDate && stampShowTime) {
- [outputFormatter setDateFormat:@" HH:mm"];
- detailText = [NSDateFormatter localizedStringFromDate:[NSDate date]
- dateStyle:NSDateFormatterShortStyle
- timeStyle:NSDateFormatterNoStyle];
- detailText = [detailText stringByAppendingString:[outputFormatter stringFromDate:[NSDate date]]];
- }
-
- CPDFStampAnnotation *annotation = [[[CPDFStampAnnotation alloc] initWithDocument:pdfView.document text:stampText detailText:detailText style:stampStyle shape:stampShape] autorelease];
- CGRect bounds = annotation.bounds;
- bounds.origin.x = point.x-bounds.size.width/2.0;
- bounds.origin.y = point.y-bounds.size.height/2.0;
- annotation.bounds = bounds;
- [self.pdfView addAnnotation:annotation forPage:page];
- }
- } else {
- CPDFStampAnnotation *annotation = [[[CPDFStampAnnotation alloc] initWithDocument:pdfView.document type:selectedIndex+1] autorelease];
- CGRect bounds = annotation.bounds;
- bounds.origin.x = point.x-bounds.size.width/2.0;
- bounds.origin.y = point.y-bounds.size.height/2.0;
- annotation.bounds = bounds;
- [self.pdfView addAnnotation:annotation forPage:page];
- }
- }
- };
- [vc showViewController:self inRect:rect];
- [vc release];
- }
- - (void)PDFViewPerformAddImage:(PDFListView *)pdfView atPoint:(CGPoint)point forPage:(CPDFPage *)page {
- self.isCreateFromToolbar = NO;
- CGPoint p = [self.pdfView convertPoint:point fromPage:page];
- CGRect rect = CGRectMake(p.x, p.y, 2, 2);
- UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Use Camera", nil)
- style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- UIImagePickerController *imagePickerController = [[[UIImagePickerController alloc] init] autorelease];
- imagePickerController.delegate = self;
- imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
- [self presentViewController:imagePickerController animated:YES completion:nil];
- }];
-
- UIAlertAction *photoAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Photo Library", nil)
- style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- UIImagePickerController *imagePickerController = [[[UIImagePickerController alloc] init] autorelease];
- imagePickerController.delegate = self;
- imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
- imagePickerController.modalPresentationStyle = UIModalPresentationPopover;
- UIPopoverPresentationController *popVC = imagePickerController.popoverPresentationController;
- popVC.delegate = self;
- popVC.sourceRect = rect;
- popVC.sourceView = self.view;
- [self presentViewController:imagePickerController animated:YES completion:nil];
- }];
-
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil)
- style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
- self.pdfView.annotationMode = PDFViewAnnotationModeNone;
- [self.annotationToolbar reloadData];
- }];
-
- UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil
- message:nil
- preferredStyle:UIAlertControllerStyleActionSheet];
- [actionSheet addAction:cameraAction];
- [actionSheet addAction:photoAction];
- [actionSheet addAction:cancelAction];
- actionSheet.modalPresentationStyle = UIModalPresentationPopover;
- UIPopoverPresentationController *popVC = actionSheet.popoverPresentationController;
- popVC.delegate = self;
- popVC.sourceRect = rect;
- popVC.sourceView = self.view;
- [self presentViewController:actionSheet animated:YES completion:nil];
- }
- - (void)PDFViewPerformTouchEnded:(PDFListView *)pdfView {
- if (PDFViewAnnotationModeNone != self.pdfView.annotationMode) {
- self.pdfView.annotationMode = PDFViewAnnotationModeNone;
- [self.annotationToolbar reloadData];
- return;
- }
- if (UIUserInterfaceIdiomPhone == UI_USER_INTERFACE_IDIOM()) {
- if (self.navigationController.navigationBarHidden) {
- [self.navigationController setNavigationBarHidden:NO animated:YES];
- [UIView animateWithDuration:0.3 animations:^{
- CGRect frame = self.annotationToolbar.frame;
- frame.origin.y = self.view.bounds.size.height-frame.size.height;
- self.annotationToolbar.frame = frame;
- self.pageSlider.alpha = 1.0;
- }];
- } else {
- [self.navigationController setNavigationBarHidden:YES animated:YES];
- [UIView animateWithDuration:0.3 animations:^{
- CGRect frame = self.annotationToolbar.frame;
- frame.origin.y = self.view.bounds.size.height;
- self.annotationToolbar.frame = frame;
- self.pageSlider.alpha = 0.0;
- }];
- }
- } else {
- if (self.navigationController.navigationBarHidden) {
- [self.navigationController setNavigationBarHidden:NO animated:YES];
- [UIView animateWithDuration:0.3 animations:^{
- self.annotationToolbar.alpha = 1.0;
- self.pageSlider.alpha = 1.0;
- self.bottomToolbar.alpha = 1.0;
- }];
- } else {
- [self.navigationController setNavigationBarHidden:YES animated:YES];
- [UIView animateWithDuration:0.3 animations:^{
- self.annotationToolbar.alpha = 0.0;
- self.pageSlider.alpha = 0.0;
- self.bottomToolbar.alpha = 0.0;
- }];
- }
- }
- }
- - (void)PDFViewPerformWillGoTo:(PDFListView *)pdfView pageIndex:(NSInteger)pageIndex {
- self.lastPageIndex = self.pdfView.currentPageIndex;
- [self.view addSubview:self.pageBackButton];
- }
- #pragma mark - PDFKeyboardDelegate
- - (void)keyboard:(PDFKeyboardToolbar *)toolbar updateFontName:(NSString *)fontName {
- UIFont *font = [UIFont fontWithName:fontName size:self.annotationFreeText.font.pointSize];
- [self.pdfView setEditAnnotationFreeTextFont:font];
- }
- - (void)keyboard:(PDFKeyboardToolbar *)toolbar updateFontSize:(CGFloat)fontSize {
- UIFont *font = [UIFont fontWithName:self.annotationFreeText.font.fontName size:fontSize];
- [self.pdfView setEditAnnotationFreeTextFont:font];
- }
- - (void)keyboard:(PDFKeyboardToolbar *)toolbar updateTextColor:(UIColor *)textColor {
- UIColor *color = [textColor colorWithAlphaComponent:self.annotationFreeText.opacity];
- [self.pdfView setEditAnnotationFreeTextColor:color];
- }
- - (void)keyboardShouldDissmiss:(PDFKeyboardToolbar *)toolbar {
- [self.pdfView commitEditAnnotationFreeText];
-
- self.pdfView.annotationMode = PDFViewAnnotationModeNone;
- [self.annotationToolbar reloadData];
- }
- - (void)keyboard:(PDFKeyboardToolbar *)toolbar updateOpacity:(CGFloat)opacity {
- UIColor *color = [self.annotationFreeText.fontColor colorWithAlphaComponent:opacity];
- [self.pdfView setEditAnnotationFreeTextColor:color];
- }
- #pragma mark - init Bottom Toolbar, Only iPad
- - (PDFBottomToolbar *)bottomToolbar {
- if (!_bottomToolbar) {
- _bottomToolbar = [[PDFBottomToolbar alloc] initWithFrame:CGRectMake((self.view.bounds.size.width-300)/2.0, self.view.bounds.size.height-40, 300, 40)];
- _bottomToolbar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin;
- _bottomToolbar.pdfViewController = self;
- }
- return _bottomToolbar;
- }
- #pragma mark - init Annotation Toolbar
- - (PDFToolbar *)annotationToolbar {
- if (!_annotationToolbar) {
- __block __typeof(self) blockSelf = self;
- if (UIUserInterfaceIdiomPhone == UI_USER_INTERFACE_IDIOM()) {
- BOOL iPhoneX = [UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO;
- CGFloat height = iPhoneX ? 78 : 44;
-
- _annotationToolbar = [[PDFToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height-height,
- self.view.bounds.size.width, height)];
- _annotationToolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin;
- } else {
- CGFloat height = 600;
- _annotationToolbar = [[PDFToolbar alloc] initWithFrame:CGRectMake(0, (self.view.bounds.size.height-height)/2.0, 44, height)];
- _annotationToolbar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
- }
- _annotationToolbar.callback = ^(NSInteger index, BOOL isSelected, UIButton *button) {
- if (isSelected) {
- NSString *promptString = nil;
- if (index == PDFToolbarNote) {
- blockSelf.pdfView.annotationMode = PDFViewAnnotationModeNote;
- } else if (index == PDFToolbarHighlight) {
- blockSelf.pdfView.annotationMode = PDFViewAnnotationModeHighlight;
- static BOOL showHighlightPromptString = YES;
- if (showHighlightPromptString) {
- showHighlightPromptString = NO;
- promptString = NSLocalizedString(@"Drag over the text to highlight", nil);
- }
- } else if (index == PDFToolbarUnderline) {
- blockSelf.pdfView.annotationMode = PDFViewAnnotationModeUnderline;
- static BOOL showUnderlinePromptString = YES;
- if (showUnderlinePromptString) {
- showUnderlinePromptString = NO;
- promptString = NSLocalizedString(@"Drag over the text to underline", nil);
- }
- } else if (index == PDFToolbarStrikeout) {
- blockSelf.pdfView.annotationMode = PDFViewAnnotationModeStrikeout;
- static BOOL showStrikeoutPromptString = YES;
- if (showStrikeoutPromptString) {
- showStrikeoutPromptString = NO;
- promptString = NSLocalizedString(@"Drag over the text to strikethrough", nil);
- }
- } else if (index == PDFToolbarSquiggly) {
- blockSelf.pdfView.annotationMode = PDFViewAnnotationModeSquiggly;
- static BOOL showSquigglyPromptString = YES;
- if (showSquigglyPromptString) {
- showSquigglyPromptString = NO;
- promptString = NSLocalizedString(@"Drag over the text to Wavy line", nil);
- }
- } else if (index == PDFToolbarFreehand) {
- blockSelf.pdfView.annotationMode = PDFViewAnnotationModeInk;
- [blockSelf.pdfView beginDrawing];
- [blockSelf showFreehandView];
- } else if (index == PDFToolbarShape) {
- if (blockSelf.annotationToolbar.shapeStyle == 0) {
- blockSelf.pdfView.annotationMode = PDFViewAnnotationModeCircle;
- } else if (blockSelf.annotationToolbar.shapeStyle == 1) {
- blockSelf.pdfView.annotationMode = PDFViewAnnotationModeSquare;
- } else if (blockSelf.annotationToolbar.shapeStyle == 2) {
- blockSelf.pdfView.annotationMode = PDFViewAnnotationModeArrow;
- } else {
- blockSelf.pdfView.annotationMode = PDFViewAnnotationModeLine;
- }
- } else if (index == PDFToolbarFreeText) {
- blockSelf.pdfView.annotationMode = PDFViewAnnotationModeFreeText;
- } else if (index == PDFToolbarSignature) {
- [blockSelf showSignatureViewController];
- } else if (index == PDFToolbarStamp) {
- CGRect rect = [button.superview convertRect:button.frame toView:blockSelf.view];
- blockSelf.pdfView.annotationMode = PDFViewAnnotationModeStamp;
- [blockSelf showStampViewControllerInRect:rect];
- } else if (index == PDFToolbarImage) {
- CGRect rect = [button.superview convertRect:button.frame toView:blockSelf.view];
- blockSelf.pdfView.annotationMode = PDFViewAnnotationModeImage;
- [blockSelf showImagePickerControllerInRect:rect];
- } else if (index == PDFToolbarLink) {
- blockSelf.pdfView.annotationMode = PDFViewAnnotationModeLink;
- static BOOL showLinkPromptString = YES;
- if (showLinkPromptString) {
- showLinkPromptString = NO;
- promptString = NSLocalizedString(@"Drag to specify an area where you would like to add a hyperlink to this PDF", nil);
- }
- }
- if (promptString) {
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:promptString preferredStyle:UIAlertControllerStyleAlert];
- [blockSelf presentViewController:alert animated:YES completion:nil];
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [alert dismissViewControllerAnimated:YES completion:nil];
- });
- }
- } else {
- blockSelf.pdfView.annotationMode = PDFViewAnnotationModeNone;
- }
- };
- }
- return _annotationToolbar;
- }
- #pragma mark - init Back Button
- - (UIButton *)pageBackButton {
- if (!_pageBackButton) {
- _pageBackButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
- _pageBackButton.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;
- [_pageBackButton addTarget:self action:@selector(buttonItemClicked_PageBack:) forControlEvents:UIControlEventTouchUpInside];
- [_pageBackButton setImage:[UIImage imageNamed:@"btn_pageback.png"] forState:UIControlStateNormal];
- [_pageBackButton sizeToFit];
-
- CGRect frame = _pageBackButton.frame;
- frame.origin.x = self.view.frame.size.width-_pageBackButton.frame.size.width-40;
- frame.origin.y = self.view.frame.size.height-_pageBackButton.frame.size.height-60;
- _pageBackButton.frame = frame;
- }
- return _pageBackButton;
- }
- - (void)buttonItemClicked_PageBack:(id)sender {
- [self.pdfView goToPageIndex:self.lastPageIndex animated:NO];
- [self.pageBackButton removeFromSuperview];
- }
- #pragma mark - init Slider
- - (PDFSlider *)pageSlider {
- if (!_pageSlider) {
- if (UIUserInterfaceIdiomPhone == UI_USER_INTERFACE_IDIOM()) {
- _pageSlider = [[PDFSlider alloc] initWithFrame:CGRectMake(self.view.bounds.size.width-22, 64, 22, self.view.bounds.size.height-self.annotationToolbar.bounds.size.height-64)];
- } else {
- _pageSlider = [[PDFSlider alloc] initWithFrame:CGRectMake(self.view.bounds.size.width-22, 64, 22, self.view.bounds.size.height-64)];
- }
- _pageSlider.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleLeftMargin;
- _pageSlider.pdfViewController = self;
- }
- return _pageSlider;
- }
- #pragma mark - Annotation - Image : ImagePickerController
- - (void)showImagePickerControllerInRect:(CGRect)rect {
- self.isCreateFromToolbar = YES;
- UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Use Camera", nil)
- style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- UIImagePickerController *imagePickerController = [[[UIImagePickerController alloc] init] autorelease];
- imagePickerController.delegate = self;
- imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
- [self presentViewController:imagePickerController animated:YES completion:nil];
- }];
-
- UIAlertAction *photoAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Photo Library", nil)
- style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- UIImagePickerController *imagePickerController = [[[UIImagePickerController alloc] init] autorelease];
- imagePickerController.delegate = self;
- imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
- imagePickerController.modalPresentationStyle = UIModalPresentationPopover;
- UIPopoverPresentationController *popVC = imagePickerController.popoverPresentationController;
- popVC.delegate = self;
- popVC.sourceRect = rect;
- popVC.sourceView = self.view;
- [self presentViewController:imagePickerController animated:YES completion:nil];
- }];
-
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil)
- style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
- self.pdfView.annotationMode = PDFViewAnnotationModeNone;
- [self.annotationToolbar reloadData];
- }];
-
- UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil
- message:nil
- preferredStyle:UIAlertControllerStyleActionSheet];
- [actionSheet addAction:cameraAction];
- [actionSheet addAction:photoAction];
- [actionSheet addAction:cancelAction];
- actionSheet.modalPresentationStyle = UIModalPresentationPopover;
- UIPopoverPresentationController *popVC = actionSheet.popoverPresentationController;
- popVC.delegate = self;
- popVC.sourceRect = rect;
- popVC.sourceView = self.view;
- [self presentViewController:actionSheet animated:YES completion:nil];
- }
- #pragma mark - Annotation - Stamp : Standard & Custom StampViewController
- - (void)showStampViewControllerInRect:(CGRect)rect {
- PDFStampViewController *vc = [[PDFStampViewController alloc] init];
- vc.callback = ^(NSInteger selectedIndex, NSDictionary *stamp) {
- if (selectedIndex == -1) {
- self.pdfView.annotationMode = PDFViewAnnotationModeNone;
- [self.annotationToolbar reloadData];
- } else {
- if (stamp) {
- if (stamp[PDFAnnotationStampKeyImagePath]) {
- UIImage *image = [UIImage imageWithContentsOfFile:stamp[PDFAnnotationStampKeyImagePath]];
- CPDFStampAnnotation *annotation = [[[CPDFStampAnnotation alloc] initWithDocument:self.pdfView.document image:image] autorelease];
- self.pdfView.addAnnotation = annotation;
- } else {
- NSString *stampText = stamp[PDFAnnotationStampKeyText];
- BOOL stampShowDate = [stamp[PDFAnnotationStampKeyShowDate] boolValue];
- BOOL stampShowTime = [stamp[PDFAnnotationStampKeyShowTime] boolValue];
- CPDFStampStyle stampStyle = [stamp[PDFAnnotationStampKeyStyle] integerValue];
- CPDFStampShape stampShape = [stamp[PDFAnnotationStampKeyShape] integerValue];
- NSString *detailText = nil;
- NSTimeZone *timename = [NSTimeZone systemTimeZone];
- NSDateFormatter *outputFormatter = [[[NSDateFormatter alloc] init] autorelease];
- [outputFormatter setTimeZone:timename];
- if (stampShowDate && !stampShowTime){
- detailText = [NSDateFormatter localizedStringFromDate:[NSDate date]
- dateStyle:NSDateFormatterShortStyle
- timeStyle:NSDateFormatterNoStyle];
- } else if (stampShowTime && !stampShowDate) {
- [outputFormatter setDateFormat:@"HH:mm:ss"];
- detailText = [outputFormatter stringFromDate:[NSDate date]];
- } else if (stampShowDate && stampShowTime) {
- [outputFormatter setDateFormat:@" HH:mm"];
- detailText = [NSDateFormatter localizedStringFromDate:[NSDate date]
- dateStyle:NSDateFormatterShortStyle
- timeStyle:NSDateFormatterNoStyle];
- detailText = [detailText stringByAppendingString:[outputFormatter stringFromDate:[NSDate date]]];
- }
- CPDFStampAnnotation *annotation = [[[CPDFStampAnnotation alloc] initWithDocument:self.pdfView.document text:stampText detailText:detailText style:stampStyle shape:stampShape] autorelease];
- self.pdfView.addAnnotation = annotation;
- }
- } else {
- CPDFStampAnnotation *annotation = [[[CPDFStampAnnotation alloc] initWithDocument:self.pdfView.document type:selectedIndex+1] autorelease];
- self.pdfView.addAnnotation = annotation;
- }
- }
- };
- [vc showViewController:self inRect:rect];
- [vc release];
- }
- #pragma mark - Annotation - Freehand : Show & Remove FreehandView
- - (void)showFreehandView {
- if (!_freehandView) {
- __block __typeof(self) blockSelf = self;
- _freehandView = [[PDFFreehandView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width-310, 30, 300, 60)];
- _freehandView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
- _freehandView.callback = ^(NSInteger buttonTag, BOOL isSelected) {
- if (buttonTag == 0) {
- // Undo Button
- [blockSelf.pdfView drawUndo];
- } else if (buttonTag == 1) {
- // Redo Button
- [blockSelf.pdfView drawRedo];
- } else if (buttonTag == 2) {
- // Erase Button
- [blockSelf.pdfView setDrawErasing:isSelected];
- } else if (buttonTag == 3) {
- // Color Button
- [blockSelf.pdfView commitDrawing];
- } else if (buttonTag == 4) {
- // Clear Button
- [blockSelf removeFreehandView];
- [blockSelf.annotationToolbar reloadData];
- blockSelf.pdfView.annotationMode = PDFViewAnnotationModeNone;
- } else if (buttonTag == 5) {
- // Save Button
- [blockSelf.pdfView commitDrawing];
- [blockSelf removeFreehandView];
- [blockSelf.annotationToolbar reloadData];
- blockSelf.pdfView.annotationMode = PDFViewAnnotationModeNone;
- }
- };
- }
- [self.view addSubview:_freehandView];
-
- [self.navigationController setNavigationBarHidden:YES animated:YES];
- if (UIUserInterfaceIdiomPhone == UI_USER_INTERFACE_IDIOM()) {
- [UIView animateWithDuration:0.3 animations:^{
- CGRect frame = self.annotationToolbar.frame;
- frame.origin.y = self.view.bounds.size.height;
- self.annotationToolbar.frame = frame;
- // self.pageSlider.alpha = 0.0;
- }];
- } else {
- [UIView animateWithDuration:0.3 animations:^{
- self.annotationToolbar.alpha = 0.0;
- // self.pageSlider.alpha = 0.0;
- self.bottomToolbar.alpha = 0.0;
- }];
- }
- }
- - (void)removeFreehandView {
- [self.freehandView removeFromSuperview];
- self.freehandView = nil;
-
- [self.navigationController setNavigationBarHidden:NO animated:YES];
- if (UIUserInterfaceIdiomPhone == UI_USER_INTERFACE_IDIOM()) {
- [UIView animateWithDuration:0.3 animations:^{
- CGRect frame = self.annotationToolbar.frame;
- frame.origin.y = self.view.bounds.size.height-frame.size.height;
- self.annotationToolbar.frame = frame;
- self.pageSlider.alpha = 1.0;
- }];
- } else {
- [UIView animateWithDuration:0.3 animations:^{
- self.annotationToolbar.alpha = 1.0;
- self.pageSlider.alpha = 1.0;
- self.bottomToolbar.alpha = 1.0;
- }];
- }
- }
- #pragma mark - Annotation - Signature : SignatureViewController
- - (void)showSignatureViewController {
- PDFSignatureViewController *vc = [[PDFSignatureViewController alloc] init];
- vc.callback = ^(UIImage *image) {
- if (image) {
- CPDFSignatureAnnotation *annotation = [[[CPDFSignatureAnnotation alloc] initWithDocument:self.pdfView.document] autorelease];
- [annotation setImage:image];
- [self.pdfView addAnnotation:annotation];
- }
- [self.annotationToolbar reloadData];
- };
- UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
- nav.modalPresentationStyle = UIModalPresentationFormSheet;
- [self presentViewController:nav animated:YES completion:nil];
- [nav release];
- [vc release];
- }
- #pragma mark - Viewer Mode : Themes, Brightness, Scroll Pages, Crop Mode, Night Mode
- - (void)buttonItemClicked_ViewerMode:(UIBarButtonItem *)barButtonItem {
- __block PDFViewerModeViewController *vc = [[PDFViewerModeViewController alloc] init];
- vc.themesType = self.pdfView.displayMode;
- vc.color = self.pdfView.displayModeCustomColor;
- vc.brightness = [UIScreen mainScreen].brightness;
- vc.scrollType = self.pdfView.displayDirection;
- vc.isCropMode = self.pdfView.displayCrop;
- vc.callback = ^(NSInteger index) {
- if (index == 0) {
- self.pdfView.displayModeCustomColor = vc.color;
- self.pdfView.displayMode = vc.themesType;
- [self.pdfView layoutDocumentView];
- } else if (index == 1) {
- [UIScreen mainScreen].brightness = vc.brightness;
- } else if (index == 2) {
- self.pdfView.displayDirection = vc.scrollType;
- [self.pdfView layoutDocumentView];
- } else if (index == 3) {
- self.view.window.userInteractionEnabled = NO;
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- self.pdfView.displayCrop = vc.isCropMode;
- dispatch_async(dispatch_get_main_queue(), ^{
- self.view.window.userInteractionEnabled = YES;
- [self.pdfView layoutDocumentView];
- });
- });
- }
- };
- [vc showViewController:self inBarButtonItem:barButtonItem];
- [vc release];
- }
- #pragma mark - PDFViewer - Search Text & Jump Page
- - (void)buttonItemClicked_Search:(UIBarButtonItem *)barButtonItem {
- if (!_searchViewController) {
- _searchViewController = [[PDFSearchViewController alloc] init];
- }
- _searchViewController.pdfViewController = self;
- [_searchViewController showViewController:self inBarButtonItem:barButtonItem];
- }
- #pragma mark - PDFViewer - BOTA : Bookmarks, Outline,Thumb,Annotation list
- - (void)buttonItemClicked_BOTA:(UIBarButtonItem *)barButtonItem {
- if (!_botaViewController) {
- _botaViewController = [[PDFBOTAViewController alloc] init];
- }
- _botaViewController.pdfViewController = self;
- [_botaViewController showViewController:self inBarButtonItem:barButtonItem];
- }
- #pragma mark - PDFViewer - More : Import / Export XFDF Annotation
- - (void)buttonItemClicked_More:(UIBarButtonItem *)barButtonItem {
- UIAlertAction *exportAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Annotation Export", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
- NSString *xfdfPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"test.xfdf"];
- BOOL result = [self.pdfView.document exportAnnotationToXFDFPath:xfdfPath];
- if (result) {
- NSURL *url = [NSURL fileURLWithPath:xfdfPath];
- UIDocumentInteractionController *documentController = [[UIDocumentInteractionController interactionControllerWithURL:url] retain];
- [documentController presentOpenInMenuFromBarButtonItem:barButtonItem animated:YES];
- }
- }];
- UIAlertAction *importAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Annotation Import", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
- NSURL *samplesURL = [NSBundle.mainBundle.resourceURL URLByAppendingPathComponent:@"Samples"];
- NSString *xfdfPath = [samplesURL.path stringByAppendingPathComponent:@"test.xfdf"];
- BOOL result = [self.pdfView.document importAnnotationFromXFDFPath:xfdfPath];
- if (result) {
- [self.pdfView setNeedsDisplayForVisiblePages];
- }
- }];
-
- #pragma mark - PDFViewer - More :Add / Delete images and text Watermarks
-
- UIAlertAction *addWatermarkAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Add Watermark", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
- self.addWaterMarkViewController = [[[PDFAddWaterMarkViewController alloc] initWithDocument:self.pdfView.document] autorelease];
- self.addWaterMarkViewController.delegate = self;
-
- [self.navigationController pushViewController:self.addWaterMarkViewController animated:YES];
- }];
- #pragma mark - PDFViewer - More :Add / Delte headerfooter and bates
-
- // Setting headerfooter action
- UIAlertAction *settingHeaderFooter = [UIAlertAction actionWithTitle:NSLocalizedString(@"Setting Headerfooter", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- [self PDFViewPerformSettingHeaderFooter];
- self.navigationItem.rightBarButtonItem.enabled = NO;
- }];
-
- // Seeting bates action
- UIAlertAction *settingBatesAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Setting Bates",nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- [self PDFViewPerformSettingBates];
- }];
- #pragma mark - PDFView - More :Add / Delete background
-
- UIAlertAction *addBackgroundAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Add Background", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- PDFBackgroundSettingViewController * addBackgroundViewController = [[[PDFBackgroundSettingViewController alloc] initWithDocument:self.pdfView.document] autorelease];
- [self.pdfView.document retain];
- addBackgroundViewController.delegate = self;
-
- [self.navigationController pushViewController:addBackgroundViewController animated:YES];
-
- }];
-
- #pragma mark - PDFViewer - More : Print
- UIAlertAction *printAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Print", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
- [self PDFViewPerformPrint:self.pdfView];
- }];
-
- #pragma mark - PDFViewer - More : Flatten
- UIAlertAction *flattenAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Flatten", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
- NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"flatten.pdf"];
- [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
- BOOL result = [self.pdfView.document writeFlattenToURL:[NSURL fileURLWithPath:filePath]];
- if (result) {
- NSURL *url = [NSURL fileURLWithPath:filePath];
- UIDocumentInteractionController *documentController = [[UIDocumentInteractionController interactionControllerWithURL:url] retain];
- [documentController presentOpenInMenuFromBarButtonItem:barButtonItem animated:YES];
- }
- }];
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:nil];
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
- alert.popoverPresentationController.barButtonItem = barButtonItem;
- [alert addAction:flattenAction];
- [alert addAction:printAction];
- [alert addAction:addBackgroundAction];
- [alert addAction:settingHeaderFooter];
- [alert addAction:settingBatesAction];
- [alert addAction:addWatermarkAction];
- [alert addAction:exportAction];
- [alert addAction:importAction];
- [alert addAction:cancelAction];
- [self presentViewController:alert animated:YES completion:nil];
- }
- #pragma mark - PDFViewer - Actions :Add / Delete images HeaderFooter and Bates
- - (void)PDFViewPerformSettingHeaderFooter {
- [self.view addSubview:_headerFooterSetting];
-
- _headerFooterSetting.translatesAutoresizingMaskIntoConstraints = NO;
-
- [self.view addConstraints:@[[NSLayoutConstraint constraintWithItem:_headerFooterSetting attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0],[NSLayoutConstraint constraintWithItem:_headerFooterSetting attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0],[NSLayoutConstraint constraintWithItem:_headerFooterSetting attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0],[NSLayoutConstraint constraintWithItem:_headerFooterSetting attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:100]]];
-
- // [_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(PDFViewPerformAddHeaderFooter:) forControlEvents:UIControlEventTouchUpInside];
- [_headerFooterSetting.deleteButton addTarget:self action:@selector(PDFViewPerformDeleteHeaderFooter:) forControlEvents:UIControlEventTouchUpInside];
- }
- - (void)PDFViewPerformSettingBates {
- [self.view addSubview:_batesSetting];
-
- _batesSetting.translatesAutoresizingMaskIntoConstraints = NO;
-
- [self.view addConstraints:@[[NSLayoutConstraint constraintWithItem:_batesSetting attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0],[NSLayoutConstraint constraintWithItem:_batesSetting attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0],[NSLayoutConstraint constraintWithItem:_batesSetting attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0],[NSLayoutConstraint constraintWithItem:_batesSetting attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:100]]];
- // [_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(PDFViewPerformAddBates:) forControlEvents:UIControlEventTouchUpInside];
- [_batesSetting.deleteButton addTarget:self action:@selector(PDFViewPerformDeleteBates:) forControlEvents:UIControlEventTouchUpInside];
- }
- - (void)PDFViewPerformAddHeaderFooter:(UIButton *)btn {
- self.navigationItem.rightBarButtonItem.enabled = YES;
- PDFHeaderFooterAddController *headerFooterControl = [[[PDFHeaderFooterAddController alloc] initWithImage:self.image] autorelease];
- headerFooterControl.delegate = self;
- [self.navigationController pushViewController:headerFooterControl animated:NO];
- [_headerFooterSetting removeFromSuperview];
- }
- - (void)PDFViewPerformDeleteHeaderFooter:(UIButton *)btn {
- self.navigationItem.rightBarButtonItem.enabled = YES;
- [self PDFViewPerformDeleteHeaderFooterAction];
-
- [_headerFooterSetting removeFromSuperview];
- }
- - (void)PDFViewPerformAddBates:(UIButton *)btn {
- PDFBatesAddViewController *batesAddControl = [[[PDFBatesAddViewController alloc] initWithImage:self.image] autorelease];
-
- batesAddControl.delegate = self;
-
- [self.navigationController pushViewController:batesAddControl animated:NO];
-
- [_batesSetting removeFromSuperview];
- }
- - (void)PDFViewPerformDeleteBates:(UIButton *)btn {
- [self PDFViewPerformDeleteBatesAction];
-
- [_batesSetting removeFromSuperview];
- }
- // Detele headerfootr action
- - (void)PDFViewPerformDeleteHeaderFooterAction {
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Delete Page Number", nil) message:NSLocalizedString(@"Are you sure delete all pages number", nil) preferredStyle:UIAlertControllerStyleAlert];
-
- UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- [self PDFViewPerformDeleteHeaderFooter];
- }];
-
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
- }];
-
- [alertController addAction:okAction];
- [alertController addAction:cancelAction];
-
- [self presentViewController:alertController animated:YES completion:nil];
- }
- // Delete bates action
- - (void)PDFViewPerformDeleteBatesAction {
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Delete Bates", nil) message:NSLocalizedString(@"Are you sure delete all Bates",nil) preferredStyle:UIAlertControllerStyleAlert];
-
- UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
-
- [self PDFViewPerformDeleteBates];
- }];
-
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull 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)PDFViewPerformChangeHeaderFooter:(PDFHeaderFooterModel *)modelData {
- CPDFHeaderFooter *headerFooter = [self.pdfView.document headerFooter];
- [headerFooter retain];
- NSString *pageIndex = [NSString stringWithFormat:@"0-%lu",self.pdfView.document.pageCount - 1];
- [pageIndex retain];
- NSString *text = [[[NSString alloc] init] autorelease];
- 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.pdfView.document.pageCount];
- break;
- case 3:
- text = [NSString stringWithFormat:@"<<%@>> of %lu",modelData.pageStart,self.pdfView.document.pageCount];
- break;
- case 4:
- text = [NSString stringWithFormat:@"page <<%@>> of %lu",modelData.pageStart,self.pdfView.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];
-
- [self.pdfView.document writeToURL:self.pdfView.document.documentURL];
- [self loadDocumentWithFilePath:self.pdfView.document.documentURL.path completion:nil];
-
- [headerFooter release];
- [pageIndex release];
- }
- // Get bates' model to setting page bates's add functional property
- - (void)PDFViewPerformChangBates:(PDFHeaderFooterModel *)modelData {
- PDFHeaderFooterModel *modelBatesData = modelData;
-
- CPDFBates *bates = [self.pdfView.document bates];
- [bates retain];
-
- NSString *pageIndex = [NSString stringWithFormat:@"%@-%lu",modelBatesData.pageStart,self.pdfView.document.pageCount - 1];
- [pageIndex retain];
- bates.pageString = pageIndex;
- NSString *text = [NSString stringWithFormat:@"<<%@>>",modelBatesData.fontText];
- [text retain];
- [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];
-
- [self.pdfView.document writeToURL:self.pdfView.document.documentURL];
- [self loadDocumentWithFilePath:self.pdfView.document.documentURL.path completion:nil];
-
- [bates release];
- [pageIndex release];
- [text release];
- }
- #pragma mark - WaterMarkModelDataDelegate
- - (void)PDFViewPerformChangeTextWatermark:(PDFWatermarkDataModel *)dataModel {
- CPDFWatermark *textWatermark = [[[CPDFWatermark alloc] initWithDocument:_pdfView.document type:CPDFWatermarkTypeText] autorelease];
-
- 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 * (_image.size.width / 3) / self.addWaterMarkViewController.textViewController.textPreview.documentImageView.frame.size.width;
- // textWatermark.ty = dataModel.ty * (_image.size.height / 3) / self.addWaterMarkViewController.textViewController.textPreview.documentImageView.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];
- [self.pdfView.document writeToURL:self.pdfView.document.documentURL];
- [self loadDocumentWithFilePath:self.pdfView.document.documentURL.path completion:nil];
- }
- - (void)PDFViewPerformChangeImageWatermark:(PDFWatermarkDataModel *)dataModel {
- CPDFWatermark *imageWatermark = [[[CPDFWatermark alloc] initWithDocument:_pdfView.document type:CPDFWatermarkTypeImage] autorelease];
-
- imageWatermark.image = dataModel.image;
-
- imageWatermark.opacity = dataModel.watermarkOpacity;
-
- imageWatermark.scale = dataModel.watermarkScale * 2;
-
- imageWatermark.isTilePage = dataModel.isTile;
-
- // imageWatermark.tx = dataModel.tx * (_image.size.width / 3) / self.addWaterMarkViewController.imageViewController.imagePreview.documentImageView.frame.size.width;
- // imageWatermark.ty = dataModel.ty * (_image.size.height / 3) / self.addWaterMarkViewController.imageViewController.imagePreview.documentImageView.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];
- [self.pdfView.document writeToURL:self.pdfView.document.documentURL];
- [self loadDocumentWithFilePath:self.pdfView.document.documentURL.path completion:nil];
- }
- - (void)PDFViewPerformDeleteWatermark:(CPDFWatermark *)waterMark {
- [self.pdfView.document removeWatermark:waterMark];
-
- [self.pdfView.document writeToURL:self.pdfView.document.documentURL];
- [self loadDocumentWithFilePath:self.pdfView.document.documentURL.path completion:nil];
- }
- #pragma mark - BackgroundDataModelDelegate
- - (void)PDFViewPerformChangeBackground:(PDFBackgroundModel *)dataModel {
- CPDFBackground *pageBackground = [_pdfView.document background];
- [pageBackground retain];
-
- 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];
-
- [self.pdfView.document writeToURL:self.pdfView.document.documentURL];
- [self loadDocumentWithFilePath:self.pdfView.document.documentURL.path completion:nil];
- [pageBackground release];
- }
- - (void)PDFViewPerformDeleteBackground {
- CPDFBackground *pageBackground = [_pdfView.document background];
- [pageBackground retain];
-
- [pageBackground clear];
-
- [self.pdfView.document writeToURL:self.pdfView.document.documentURL];
- [self loadDocumentWithFilePath:self.pdfView.document.documentURL.path completion:nil];
- [pageBackground release];
- }
- #pragma mark - Delete Methods
- // Get headerfooter' model to setting page headerfooter's delete functional property
- - (void)PDFViewPerformDeleteHeaderFooter {
- CPDFHeaderFooter *headerFooter = [self.pdfView.document headerFooter];
- [headerFooter retain];
- [headerFooter clear];
-
- [self.pdfView.document writeToURL:self.pdfView.document.documentURL];
- [self loadDocumentWithFilePath:self.pdfView.document.documentURL.path completion:nil];
-
- [headerFooter release];
- }
- // Get bates' model to setting page bates's delete functional property
- - (void)PDFViewPerformDeleteBates {
- CPDFBates *bates = [self.pdfView.document bates];
- [bates retain];
- [bates clear];
-
- [self.pdfView.document writeToURL:self.pdfView.document.documentURL];
- [self loadDocumentWithFilePath:self.pdfView.document.documentURL.path completion:nil];
-
- [bates release];
- }
- #pragma mark - PDFViewer - Page Editor: Split / Extract, Rotate, and delete pages
- - (void)buttonItemClicked_PageEdit:(UIBarButtonItem *)barButtonItem {
- PDFPageEditViewController *vc = [[PDFPageEditViewController alloc] init];
- vc.pdfViewController = self;
- UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
- [self presentViewController:nav animated:YES completion:nil];
- [nav release];
- [vc release];
- }
- #pragma mark - Show/Hide Page Number Indicator
- - (void)showPageNumIndicator {
- if (self.pageNumIndicator.hidden) {
- self.pageNumIndicator.alpha = 0.4;
- self.pageNumIndicator.hidden = NO;
- self.informationIndicator.alpha = 0.4;
- self.informationIndicator.hidden = NO;
- }
- }
- - (void)hidePageNumIndicator {
- if (self.pageNumIndicator.hidden) {
- return;
- }
-
- [UIView animateWithDuration:0.2 animations:^{
- self.pageNumIndicator.alpha = 0.0;
- self.informationIndicator.alpha = 0.0;
- }completion:^(BOOL finished){
- self.pageNumIndicator.hidden = YES;
- self.informationIndicator.hidden = YES;
- }];
- }
- #pragma mark - PDFViewer - Zoom In / Zoom Out
- - (void)zoomIn:(id)sender {
- if (self.pdfView.scaleFactor >= 6) {
- return;
- }
- self.pdfView.scaleFactor += 1;
- }
- - (void)zoomOut:(id)sender {
- if (self.pdfView.scaleFactor <= 1) {
- return;
- }
- self.pdfView.scaleFactor -= 1;
- }
- #pragma mark - UIImagePickerControllerDelegate
- - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
- [picker dismissViewControllerAnimated:YES completion:nil];
-
- UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
- UIImageOrientation imageOrientation = image.imageOrientation;
- if (imageOrientation != UIImageOrientationUp) {
- UIGraphicsBeginImageContext(image.size);
- [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
- image = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- }
-
- if (self.isCreateFromToolbar) {
- CPDFStampAnnotation *annotation = [[[CPDFStampAnnotation alloc] initWithDocument:self.pdfView.document image:image] autorelease];
- self.pdfView.addAnnotation = annotation;
- } else {
- CPDFStampAnnotation *annotation = [[[CPDFStampAnnotation alloc] initWithDocument:self.pdfView.document image:image] autorelease];
- CGRect bounds = annotation.bounds;
- bounds.origin.x = self.pdfView.menuPoint.x-bounds.size.width/2.0;
- bounds.origin.y = self.pdfView.menuPoint.y-bounds.size.height/2.0;
- annotation.bounds = bounds;
- [self.pdfView addAnnotation:annotation forPage:self.pdfView.menuPage];
- }
- }
- - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
- [picker dismissViewControllerAnimated:YES completion:nil];
- if (self.isCreateFromToolbar) {
- self.pdfView.annotationMode = PDFViewAnnotationModeNone;
- [self.annotationToolbar reloadData];
- }
- }
- #pragma mark - UIPopoverPresentationControllerDelegate
- - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection {
- return UIModalPresentationNone;
- }
- - (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController {
- if ([popoverPresentationController.presentedViewController isKindOfClass:[UIAlertController class]]) {
- return;
- }
- self.pdfView.annotationMode = PDFViewAnnotationModeNone;
- [self.annotationToolbar reloadData];
- }
- @end
|