CPDFViewController.m 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488
  1. //
  2. // CPDFViewController.m
  3. // ContentEditor
  4. //
  5. // Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. #import "CPDFViewController.h"
  13. #import <ComPDFKit/ComPDFKit.h>
  14. #import <AVFAudio/AVFAudio.h>
  15. #import <AVFoundation/AVFoundation.h>
  16. #import <MobileCoreServices/MobileCoreServices.h>
  17. @interface CPDFViewController () <CPDFSoundPlayBarDelegate,CPDFAnnotationBarDelegate,CPDFToolsViewControllerDelegate,CPDFNoteOpenViewControllerDelegate,CPDFBOTAViewControllerDelegate,CPDFEditToolBarDelegate,CPDFFormBarDelegate,CPDFListViewDelegate,CPDFSignatureViewControllerDelegate,CPDFPageEditViewControllerDelegate,CPDFKeyboardToolbarDelegate,CPDFDigitalSignatureToolBarDelegate,CCertificateViewControllerDelegate,CDigitalTypeSelectViewDelegate,CCreateCertificateInfoViewControllerDelegate,CPDFSigntureVerifyViewControllerDelegate,CSignatureTypeSelectViewDelegate,UIDocumentPickerDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
  18. @property(nonatomic, strong) CPDFAnnotationToolBar *annotationBar;
  19. @property(nonatomic, strong) CPDFFormToolBar *formBar;
  20. @property(nonatomic, strong) CPDFSoundPlayBar *soundPlayBar;
  21. @property(nonatomic, strong) CAnnotationManage *annotationManage;
  22. @property(nonatomic, strong) CPDFEditToolBar * toolBar;
  23. @property(nonatomic, strong) CPDFEditViewController *baseVC;
  24. @property(nonatomic, assign) CPDFEditMode editMode;
  25. @property(nonatomic, strong) CPDFSignatureWidgetAnnotation * signatureAnnotation;
  26. @property(nonatomic, assign) CGRect addImageRect;
  27. @property(nonatomic, retain) CPDFPage *addImagePage;
  28. @property (nonatomic, strong) CPDFDigitalSignatureToolBar *digitalSignatureBar;
  29. @property (nonatomic, strong) CPDFSigntureViewController *signtureViewController;
  30. @property (nonatomic, strong) CPDFSigntureVerifyViewController *signtureVerifyViewController;
  31. @property (nonatomic, strong) UIDocumentPickerViewController *pkcs12DocumentPickerViewController;
  32. @property (nonatomic, assign) BOOL isSelctSignature;
  33. @end
  34. @implementation CPDFViewController
  35. - (void)viewDidLoad {
  36. [super viewDidLoad];
  37. self.pdfListView.toolModel = CToolModelViewer;
  38. CPDFEditingConfig *editingConfig = [[CPDFEditingConfig alloc]init];
  39. editingConfig.editingBorderWidth = 1.0;
  40. editingConfig.editingOffsetGap = 5;
  41. self.pdfListView.editingConfig = editingConfig;
  42. self.isSelctSignature = NO;
  43. [self initAnnotationBar];
  44. [self initWithEditTool];
  45. [self initWithFormTool];
  46. [self initDigitalSignatureBar];
  47. [self enterViewerMode];
  48. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(signatureHaveChangeDidChangeNotification:) name:CSignatureHaveChangeDidChangeNotification object:nil];
  49. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(signatureTrustCerDidChangeNotification:) name:CSignatureTrustCerDidChangeNotification object:nil];
  50. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(PDFPageDidRemoveAnnotationNotification:) name:CPDFPageDidRemoveAnnotationNotification object:nil];
  51. }
  52. - (void)initAnnotationBar {
  53. self.annotationManage = [[CAnnotationManage alloc] initWithPDFView:self.pdfListView];
  54. self.annotationBar = [[CPDFAnnotationToolBar alloc] initAnnotationManage:self.annotationManage];
  55. CGFloat height = 44.0;
  56. if (@available(iOS 11.0, *))
  57. height += self.view.safeAreaInsets.bottom;
  58. self.annotationBar.frame = CGRectMake(0, self.view.frame.size.height - height, self.view.frame.size.width, height);
  59. self.annotationBar.delegate = self;
  60. [self.annotationBar setParentVC:self];
  61. [self.view addSubview:self.annotationBar];
  62. }
  63. - (void)initWithEditTool {
  64. if(!self.toolBar){
  65. self.toolBar = [[CPDFEditToolBar alloc] initWithPDFView:self.pdfListView];
  66. }
  67. self.toolBar.delegate = self;
  68. [self.view addSubview:self.toolBar];
  69. }
  70. - (void)initWithFormTool {
  71. if(!self.formBar){
  72. self.formBar = [[CPDFFormToolBar alloc] initAnnotationManage:self.annotationManage];
  73. }
  74. self.formBar.delegate = self;
  75. self.formBar.parentVC = self;
  76. [self.view addSubview:self.formBar];
  77. }
  78. - (void)initDigitalSignatureBar {
  79. self.digitalSignatureBar = [[CPDFDigitalSignatureToolBar alloc] initWithPDFListView:self.pdfListView];
  80. CGFloat height = 60.0;
  81. if (@available(iOS 11.0, *))
  82. height += self.view.safeAreaInsets.bottom;
  83. self.digitalSignatureBar.delegate = self;
  84. }
  85. - (void)initWitNavigationTitle {
  86. [super initWitNavigationTitle];
  87. }
  88. - (void)viewWillLayoutSubviews {
  89. [super viewWillLayoutSubviews];
  90. if([self.popMenu superview]) {
  91. if (@available(iOS 11.0, *)) {
  92. [self.popMenu showMenuInRect:CGRectMake(self.view.frame.size.width - self.view.safeAreaInsets.right - 250, CGRectGetMaxY(self.navigationController.navigationBar.frame), 250, 250)];
  93. } else {
  94. // Fallback on earlier versions
  95. [self.popMenu showMenuInRect:CGRectMake(self.view.frame.size.width - 250, CGRectGetMaxY(self.navigationController.navigationBar.frame), 250, 250)];
  96. }
  97. }
  98. CGFloat height = 44.0;
  99. if (@available(iOS 11.0, *))
  100. height += self.view.safeAreaInsets.bottom;
  101. CGFloat bottomHeight = 0;
  102. if(CToolModelAnnotation == self.pdfListView.toolModel) {
  103. self.annotationBar.frame = CGRectMake(0, self.view.frame.size.height - height, self.view.frame.size.width, height);
  104. bottomHeight = self.self.annotationBar.frame.size.height;
  105. } else if(CToolModelEdit == self.pdfListView.toolModel){
  106. self.toolBar.frame = CGRectMake(0, self.view.frame.size.height - height, self.view.frame.size.width, height);
  107. bottomHeight = self.self.toolBar.frame.size.height;
  108. } else if(CToolModelForm == self.pdfListView.toolModel){
  109. self.formBar.frame = CGRectMake(0, self.view.frame.size.height - height, self.view.frame.size.width, height);
  110. bottomHeight = self.self.formBar.frame.size.height;
  111. }
  112. if(self.digitalSignatureBar.superview) {
  113. height += 14;
  114. self.digitalSignatureBar.frame = CGRectMake(0, self.view.frame.size.height - height, self.view.frame.size.width, height);
  115. }
  116. height = CGRectGetMaxY(self.navigationController.navigationBar.frame) ;
  117. if([self.signtureViewController.view superview]) {
  118. self.signtureViewController.view.frame = CGRectMake(0, height, self.view.frame.size.width, 44.0);
  119. }
  120. CGFloat tPosY = 0;
  121. CGFloat tBottomY = 0;
  122. if(CToolModelAnnotation == self.pdfListView.toolModel) {
  123. if (!self.navigationController.navigationBarHidden) {
  124. [UIView animateWithDuration:0.3 animations:^{
  125. CGRect frame = self.annotationBar.frame;
  126. frame.origin.y = self.view.bounds.size.height-frame.size.height;
  127. self.annotationBar.frame = frame;
  128. }];
  129. CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
  130. tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height;
  131. tBottomY = self.annotationBar.frame.size.height;
  132. } else {
  133. [UIView animateWithDuration:0.3 animations:^{
  134. CGRect frame = self.annotationBar.frame;
  135. frame.origin.y = self.view.bounds.size.height;
  136. self.annotationBar.frame = frame;
  137. }];
  138. }
  139. } else {
  140. tPosY = 0;
  141. if (!self.navigationController.navigationBarHidden) {
  142. CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
  143. tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height;
  144. }
  145. }
  146. if (CPDFDisplayDirectionVertical == [CPDFKitConfig sharedInstance].displayDirection) {
  147. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  148. inset.bottom = 10 + bottomHeight;
  149. self.pdfListView.documentView.contentInset = inset;
  150. } else{
  151. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  152. inset.bottom = 0;
  153. self.pdfListView.documentView.contentInset = inset;
  154. }
  155. }
  156. #pragma mark - Public Methods
  157. - (void)selectDocumentRefresh {
  158. if(CToolModelAnnotation == self.pdfListView.toolModel) {
  159. self.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
  160. [self.annotationBar updatePropertiesButtonState];
  161. [self.annotationBar reloadData];
  162. [self.annotationBar updateUndoRedoState];
  163. }else if(CToolModelForm == self.pdfListView.toolModel) {
  164. [self.formBar initUndoRedo];
  165. }
  166. }
  167. - (void)reloadDocumentWithFilePath:(NSString *)filePath password:(nullable NSString *)password completion:(void (^)(BOOL result))completion {
  168. [self.navigationController.view setUserInteractionEnabled:NO];
  169. if (![self.loadingView superview]) {
  170. [self.view addSubview:self.loadingView];
  171. }
  172. [self.loadingView startAnimating];
  173. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  174. NSURL *url = [NSURL fileURLWithPath:filePath];
  175. CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
  176. if([document isLocked]) {
  177. [document unlockWithPassword:password];
  178. }
  179. dispatch_async(dispatch_get_main_queue(), ^{
  180. [self.navigationController.view setUserInteractionEnabled:YES];
  181. [self.loadingView stopAnimating];
  182. [self.loadingView removeFromSuperview];
  183. if (document.error && document.error.code != CPDFDocumentPasswordError) {
  184. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  185. [self.navigationController popViewControllerAnimated:YES];
  186. }];
  187. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
  188. message:NSLocalizedString(@"Sorry PDF Reader Can't open this pdf file!", nil)
  189. preferredStyle:UIAlertControllerStyleAlert];
  190. [alert addAction:okAction];
  191. if (completion) {
  192. completion(NO);
  193. }
  194. } else {
  195. self.pdfListView.document = document;
  196. if ([self.digitalSignatureBar superview]) {
  197. NSArray *signatures = [self.pdfListView.document signatures];
  198. for (CPDFSignature *signature in signatures) {
  199. CPDFSignatureWidgetAnnotation *signatureWidgetAnnotation = [signature signatureWidgetAnnotationWithDocument:self.pdfListView.document];
  200. if (!signatureWidgetAnnotation.page) {
  201. [self.pdfListView.document removeSignature:signature];
  202. NSMutableArray *signaturess = [NSMutableArray arrayWithArray:signatures];
  203. [signaturess removeObject:signature];
  204. signatures = signaturess;
  205. }
  206. }
  207. [self.digitalSignatureBar updateStatusWithsignatures:signatures];
  208. if (completion) {
  209. completion(YES);
  210. }
  211. if([self.signtureViewController.view superview]) {
  212. NSArray *signatures = [self.pdfListView.document signatures];
  213. NSMutableArray *mSignatures = [NSMutableArray array];
  214. for (CPDFSignature *sign in signatures) {
  215. if (sign.signers.count > 0) {
  216. [mSignatures addObject:sign];
  217. }
  218. }
  219. self.signatures = mSignatures;
  220. [self verifySignature];
  221. }
  222. }
  223. }
  224. });
  225. });
  226. }
  227. #pragma mark - Private
  228. - (void)enterEditMode {
  229. [self selectDocumentRefresh];
  230. self.toolBar.hidden = NO;
  231. self.annotationBar.hidden = YES;
  232. self.formBar.hidden = YES;
  233. self.pdfListView.toolModel = CToolModelEdit;
  234. if (CPDFEditModeText == self.toolBar.editToolBarSelectType) {
  235. [self.pdfListView beginEditingLoadType:CEditingLoadTypeText];
  236. [self.pdfListView setShouAddEditAreaType:CAddEditingAreaTypeText];
  237. } else if (CPDFEditModeImage == self.toolBar.editToolBarSelectType) {
  238. [self.pdfListView beginEditingLoadType:CEditingLoadTypeImage];
  239. [self.pdfListView setShouAddEditAreaType:CAddEditingAreaTypeImage];
  240. } else {
  241. [self.pdfListView beginEditingLoadType:CEditingLoadTypeText | CEditingLoadTypeImage];
  242. [self.pdfListView setShouAddEditAreaType:CAddEditingAreaTypeNone];
  243. }
  244. self.navigationTitle = NSLocalizedString(@"Content Edit", nil);
  245. [self.titleButton setTitle:self.navigationTitle forState:UIControlStateNormal];
  246. [self.toolBar updateButtonState];
  247. CGRect frame = self.toolBar.frame;
  248. frame.origin.y = self.view.bounds.size.height-frame.size.height;
  249. self.toolBar.frame = frame;
  250. if([self.digitalSignatureBar superview]) {
  251. [self.digitalSignatureBar removeFromSuperview];
  252. }
  253. [self viewWillLayoutSubviews];
  254. }
  255. - (void)enterAnnotationMode {
  256. self.toolBar.hidden = YES;
  257. self.annotationBar.hidden = NO;
  258. self.formBar.hidden = YES;
  259. if (self.pdfListView.isEdited) {
  260. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  261. [self.pdfListView commitEditing];
  262. dispatch_async(dispatch_get_main_queue(), ^{
  263. [self.pdfListView endOfEditing];
  264. });
  265. });
  266. } else {
  267. [self.pdfListView endOfEditing];
  268. }
  269. self.pdfListView.toolModel = CToolModelAnnotation;
  270. self.navigationTitle = NSLocalizedString(@"Annotation", nil);
  271. [self.titleButton setTitle:self.navigationTitle forState:UIControlStateNormal];
  272. CGFloat tPosY = 0;
  273. CGFloat tBottomY = 0;
  274. CGRect frame = self.annotationBar.frame;
  275. frame.origin.y = self.view.bounds.size.height-frame.size.height;
  276. self.annotationBar.frame = frame;
  277. CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
  278. tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height;
  279. tBottomY = self.annotationBar.frame.size.height;
  280. if (CPDFDisplayDirectionVertical == [CPDFKitConfig sharedInstance].displayDirection) {
  281. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  282. inset.bottom = 10 + self.annotationBar.frame.size.height;
  283. self.pdfListView.documentView.contentInset = inset;
  284. } else{
  285. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  286. inset.bottom = 0;
  287. self.pdfListView.documentView.contentInset = inset;
  288. }
  289. if([self.digitalSignatureBar superview]) {
  290. [self.digitalSignatureBar removeFromSuperview];
  291. }
  292. [self viewWillLayoutSubviews];
  293. }
  294. - (void)enterViewerMode {
  295. self.toolBar.hidden = YES;
  296. self.formBar.hidden = YES;
  297. self.annotationBar.hidden = YES;
  298. if (self.pdfListView.isEdited) {
  299. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  300. [self.pdfListView commitEditing];
  301. dispatch_async(dispatch_get_main_queue(), ^{
  302. [self.pdfListView endOfEditing];
  303. });
  304. });
  305. } else {
  306. [self.pdfListView endOfEditing];
  307. }
  308. self.pdfListView.toolModel = CToolModelViewer;
  309. self.navigationTitle = NSLocalizedString(@"Viewer", nil);
  310. [self.titleButton setTitle:self.navigationTitle forState:UIControlStateNormal];
  311. CGRect frame = self.annotationBar.frame;
  312. frame.origin.y = self.view.bounds.size.height;
  313. self.annotationBar.frame = frame;
  314. if (CPDFDisplayDirectionVertical == [CPDFKitConfig sharedInstance].displayDirection) {
  315. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  316. inset.bottom = 0;
  317. self.pdfListView.documentView.contentInset = inset;
  318. } else{
  319. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  320. inset.bottom = 0;
  321. self.pdfListView.documentView.contentInset = inset;
  322. }
  323. if([self.digitalSignatureBar superview]) {
  324. [self.digitalSignatureBar removeFromSuperview];
  325. }
  326. }
  327. - (void)enterFormMode {
  328. self.toolBar.hidden = YES;
  329. self.annotationBar.hidden = YES;
  330. self.formBar.hidden = NO;
  331. if (self.pdfListView.isEdited) {
  332. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  333. [self.pdfListView commitEditing];
  334. dispatch_async(dispatch_get_main_queue(), ^{
  335. [self.pdfListView endOfEditing];
  336. });
  337. });
  338. } else {
  339. [self.pdfListView endOfEditing];
  340. }
  341. self.pdfListView.toolModel = CToolModelForm;
  342. self.navigationTitle = NSLocalizedString(@"Form", nil);
  343. [self.titleButton setTitle:self.navigationTitle forState:UIControlStateNormal];
  344. CGFloat tPosY = 0;
  345. CGFloat tBottomY = 0;
  346. CGRect frame = self.formBar.frame;
  347. frame.origin.y = self.view.bounds.size.height-frame.size.height;
  348. self.formBar.frame = frame;
  349. CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
  350. tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height;
  351. tBottomY = self.formBar.frame.size.height;
  352. if (CPDFDisplayDirectionVertical == [CPDFKitConfig sharedInstance].displayDirection) {
  353. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  354. inset.bottom = 10 + self.formBar.frame.size.height;
  355. self.pdfListView.documentView.contentInset = inset;
  356. } else{
  357. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  358. inset.bottom = 0;
  359. self.pdfListView.documentView.contentInset = inset;
  360. }
  361. if([self.digitalSignatureBar superview]) {
  362. [self.digitalSignatureBar removeFromSuperview];
  363. }
  364. [self viewWillLayoutSubviews];
  365. }
  366. - (void)enterSignatureMode {
  367. if (self.pdfListView.isEdited) {
  368. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  369. [self.pdfListView commitEditing];
  370. dispatch_async(dispatch_get_main_queue(), ^{
  371. [self.pdfListView endOfEditing];
  372. });
  373. });
  374. } else {
  375. [self.pdfListView endOfEditing];
  376. }
  377. if(self.isSelctSignature) {
  378. self.pdfListView.toolModel = CToolModelForm;
  379. } else {
  380. self.pdfListView.toolModel = CToolModelViewer;
  381. }
  382. [self.view addSubview:self.digitalSignatureBar];
  383. self.navigationTitle = NSLocalizedString(@"Digital Signature", nil);
  384. [self.titleButton setTitle:self.navigationTitle forState:UIControlStateNormal];
  385. CGFloat tPosY = 0;
  386. CGFloat tBottomY = 0;
  387. CGRect frame = self.digitalSignatureBar.frame;
  388. frame.origin.y = self.view.bounds.size.height-frame.size.height;
  389. self.digitalSignatureBar.frame = frame;
  390. tBottomY = self.digitalSignatureBar.frame.size.height;
  391. if([self.signtureViewController.view superview]) {
  392. tPosY = self.signtureViewController.view.frame.size.height;
  393. }
  394. if (CPDFDisplayDirectionVertical == [CPDFKitConfig sharedInstance].displayDirection) {
  395. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  396. inset.bottom = 10 + tBottomY;
  397. inset.top = tPosY;
  398. self.pdfListView.documentView.contentInset = inset;
  399. } else{
  400. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  401. inset.bottom = 0;
  402. self.pdfListView.documentView.contentInset = inset;
  403. }
  404. }
  405. - (void)setTitleRefresh {
  406. if (CToolModelEdit == self.pdfListView.toolModel) {
  407. [self enterEditMode];
  408. } else if (CToolModelViewer == self.pdfListView.toolModel) {
  409. [self enterViewerMode];
  410. } else if (CToolModelAnnotation == self.pdfListView.toolModel) {
  411. [self enterAnnotationMode];
  412. } else if(CToolModelForm == self.pdfListView.toolModel) {
  413. [self enterFormMode];
  414. }
  415. }
  416. - (CPDFSigntureViewController *)signtureViewController {
  417. if (!_signtureViewController) {
  418. _signtureViewController = [[CPDFSigntureViewController alloc] init];
  419. }
  420. return _signtureViewController;
  421. }
  422. - (NSString *)tagString {
  423. NSDateFormatter *formatter = [[NSDateFormatter alloc ] init];
  424. [formatter setDateFormat:@"YYYY-MM-ddHH:mm:ssSS"];
  425. NSString *dateString = [formatter stringFromDate:[NSDate date]];
  426. return dateString;
  427. }
  428. - (void)writeSignatureToWidget:(CPDFSignatureWidgetAnnotation *)widget PKCS12Cert:(NSString *)path password:(NSString *)password config:(CPDFSignatureConfig *)config lockDocument:(BOOL)isLock {
  429. NSString *homePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
  430. NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", homePath, @"Signature"];
  431. if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
  432. [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
  433. NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@_Widget_%@.pdf",writeDirectoryPath,self.pdfListView.document.documentURL.lastPathComponent.stringByDeletingPathExtension,[self tagString]];
  434. if([[NSFileManager defaultManager] fileExistsAtPath:writeFilePath]) {
  435. [[NSFileManager defaultManager] removeItemAtPath:writeFilePath error:nil];
  436. }
  437. CPDFSignatureConfigItem *reaItem = nil;
  438. CPDFSignatureConfigItem *locaItem = nil;
  439. NSString *locationStr = @"";
  440. NSString *reasonStr = [NSString stringWithFormat:@" %@",NSLocalizedString(@"none", nil)];
  441. for (CPDFSignatureConfigItem *item in config.contents) {
  442. if ([item.key isEqual:@"Reason"]) {
  443. reaItem = item;
  444. locationStr = reaItem.value;
  445. } else if ([item.key isEqual:@"Location"]) {
  446. locaItem = item;
  447. locationStr = locaItem.value;
  448. }
  449. }
  450. BOOL isSuccess = [self.pdfListView.document writeSignatureToURL:[NSURL fileURLWithPath:writeFilePath] withWidget:widget PKCS12Cert:path password:password location:locationStr reason:reasonStr permissions:CPDFSignaturePermissionsForbidChange];
  451. if (isSuccess) {
  452. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  453. if (([[NSFileManager defaultManager] fileExistsAtPath:writeFilePath])) {
  454. [self reloadDocumentWithFilePath:writeFilePath password:nil completion:^(BOOL result) {
  455. }];
  456. }
  457. });
  458. }
  459. }
  460. #pragma mark - Action
  461. - (void)buttonItemClicked_Bota:(id)sender {
  462. CPDFBOTAViewController *botaViewController = [[CPDFBOTAViewController alloc] initCustomizeWithPDFView:self.pdfListView navArrays:@[@(CPDFBOTATypeStateOutline),@(CPDFBOTATypeStateBookmark),@(CPDFBOTATypeStateAnnotation)]];
  463. botaViewController.delegate = self;
  464. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  465. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:botaViewController presentingViewController:self];
  466. botaViewController.transitioningDelegate = presentationController;
  467. [self presentViewController:botaViewController animated:YES completion:nil];
  468. }
  469. - (void) titleButtonClickd:(UIButton *) button {
  470. CPDFToolsViewController * toolsVc = [[CPDFToolsViewController alloc] initCustomizeWithToolArrays:@[@(CToolModelViewer),@(CToolModelEdit),@(CToolModelAnnotation),@(CToolModelForm),@(CToolModelSignatureMode)]];
  471. toolsVc.delegate = self;
  472. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  473. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:toolsVc presentingViewController:self];
  474. toolsVc.transitioningDelegate = presentationController;
  475. [self presentViewController:toolsVc animated:YES completion:nil];
  476. }
  477. #pragma - CPDFEditToolBarDelegate
  478. - (void)editClickInToolBar:(CPDFEditToolBar*)toolBar editMode:(CPDFEditMode)mode {
  479. self.editMode = mode;
  480. }
  481. - (void)undoDidClickInToolBar:(CPDFEditToolBar *)toolBar{
  482. [self.pdfListView editTextUndo];
  483. }
  484. - (void)redoDidClickInToolBar:(CPDFEditToolBar *)toolBar{
  485. [self.pdfListView editTextRedo];
  486. }
  487. - (void)propertyEditDidClickInToolBar:(CPDFEditToolBar *)toolBar{
  488. [self showMenuList];
  489. }
  490. - (void)showMenuList {
  491. _baseVC = [[CPDFEditViewController alloc] initWithPDFView:self.pdfListView];
  492. _baseVC.editMode = self.editMode;
  493. if((self.editMode == CPDFEditModeText || self.editMode == CPDFEditModeImage) && self.pdfListView.editStatus != CEditingSelectStateEmpty){
  494. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  495. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:self.baseVC presentingViewController:self];
  496. self.baseVC.transitioningDelegate = presentationController;
  497. [self presentViewController:self.baseVC animated:YES completion:nil];
  498. } else if (CAddEditingAreaTypeText == self.pdfListView.shouAddEditAreaType) {
  499. _baseVC.editMode = CPDFEditModeText;
  500. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  501. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:self.baseVC presentingViewController:self];
  502. self.baseVC.transitioningDelegate = presentationController;
  503. [self presentViewController:self.baseVC animated:YES completion:nil];
  504. }
  505. }
  506. #pragma mark - CPDFViewDelegate
  507. - (void)PDFViewEditingSelectStateDidChanged:(CPDFView *)pdfView {
  508. if([pdfView.editingArea isKindOfClass:[CPDFEditImageArea class]]) {
  509. self.editMode = CPDFEditModeImage;
  510. }else if([pdfView.editingArea isKindOfClass:[CPDFEditTextArea class]]) {
  511. self.editMode = CPDFEditModeText;
  512. }
  513. [self.toolBar updateButtonState];
  514. }
  515. - (void)PDFViewShouldBeginEditing:(CPDFView *)pdfView textView:(UITextView *)textView forAnnotation:(CPDFFreeTextAnnotation *)annotation {
  516. CPDFKeyboardToolbar *keyBoadrdToolbar = [[CPDFKeyboardToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)];
  517. keyBoadrdToolbar.delegate = self;
  518. [keyBoadrdToolbar bindToTextView:textView];
  519. }
  520. - (void)PDFViewEditingAddTextArea:(CPDFView * _Nonnull)pdfView addPage:(CPDFPage * _Nonnull )page addRect:(CGRect)rect {
  521. UIColor *fontColor = [CPDFTextProperty sharedManager].fontColor;
  522. if (fontColor) {
  523. CGFloat red, green, blue, alpha;
  524. [fontColor getRed:&red green:&green blue:&blue alpha:&alpha];
  525. fontColor = [UIColor colorWithRed:red green:green blue:blue alpha:[CPDFTextProperty sharedManager].textOpacity];
  526. }
  527. if(!fontColor)
  528. fontColor = [UIColor blackColor];
  529. UIFont *font = [UIFont fontWithName:[CPDFTextProperty sharedManager].fontName size:[CPDFTextProperty sharedManager].fontSize];
  530. if(!font)
  531. font = [UIFont fontWithName:@"Helvetica-Oblique" size:10];
  532. CEditAttributes *atributes = [[CEditAttributes alloc]init];
  533. atributes.font = font;
  534. atributes.fontColor = fontColor;
  535. atributes.isBold = [CPDFTextProperty sharedManager].isBold;
  536. atributes.isItalic = [CPDFTextProperty sharedManager].isItalic;
  537. atributes.alignment = [CPDFTextProperty sharedManager].textAlignment;
  538. [self.pdfListView createStringBounds:rect withAttributes:atributes page:page];
  539. }
  540. - (void)PDFViewEditingAddImageArea:(CPDFView * _Nonnull)pdfView addPage:(CPDFPage * _Nonnull)page addRect:(CGRect)rect {
  541. self.addImageRect = rect;
  542. self.addImagePage = page;
  543. UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
  544. imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  545. imagePicker.delegate = self;
  546. [self presentViewController:imagePicker animated:YES completion:nil];
  547. }
  548. - (void)PDFViewDocumentDidLoaded:(CPDFView *)pdfView {
  549. [super PDFViewDocumentDidLoaded:self.pdfListView];
  550. if ([self.digitalSignatureBar superview]) {
  551. [self.digitalSignatureBar updateStatusWithsignatures:self.signatures];
  552. }
  553. }
  554. #pragma mark - CPDFListViewDelegate
  555. - (void)PDFListViewPerformTouchEnded:(CPDFListView *)pdfView {
  556. CGFloat tPosY = 0;
  557. CGFloat tBottomY = 0;
  558. if(CToolModelAnnotation == self.pdfListView.toolModel) {
  559. if (self.navigationController.navigationBarHidden) {
  560. [self.navigationController setNavigationBarHidden:NO animated:YES];
  561. [UIView animateWithDuration:0.3 animations:^{
  562. CGRect frame = self.annotationBar.frame;
  563. frame.origin.y = self.view.bounds.size.height-frame.size.height;
  564. self.annotationBar.frame = frame;
  565. self.pdfListView.pageSliderView.alpha = 1.0;
  566. self.annotationBar.topToolBar.alpha = 1.0;
  567. self.annotationBar.drawPencilFuncView.alpha = 1.0;
  568. self.signtureViewController.view.alpha = 1.0;
  569. }];
  570. CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
  571. tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height;
  572. tBottomY = self.annotationBar.frame.size.height;
  573. } else {
  574. [self.navigationController setNavigationBarHidden:YES animated:YES];
  575. [UIView animateWithDuration:0.3 animations:^{
  576. CGRect frame = self.annotationBar.frame;
  577. frame.origin.y = self.view.bounds.size.height;
  578. self.annotationBar.frame = frame;
  579. self.pdfListView.pageSliderView.alpha = 0.0;
  580. self.annotationBar.topToolBar.alpha = 0.0;
  581. self.annotationBar.drawPencilFuncView.alpha = 0.0;
  582. self.signtureViewController.view.alpha = 0.0;
  583. }];
  584. }
  585. } else if ([self.digitalSignatureBar superview]) {
  586. CGFloat tTopY = 0;
  587. if (self.navigationController.navigationBarHidden) {
  588. [self.navigationController setNavigationBarHidden:NO animated:YES];
  589. [UIView animateWithDuration:0.3 animations:^{
  590. CGRect frame = self.digitalSignatureBar.frame;
  591. frame.origin.y = self.view.bounds.size.height-frame.size.height;
  592. self.digitalSignatureBar.frame = frame;
  593. self.pdfListView.pageSliderView.alpha = 1.0;
  594. self.signtureViewController.view.alpha = 1.0;
  595. }];
  596. tPosY = CGRectGetMaxY(self.navigationController.navigationBar.frame);
  597. if([self.digitalSignatureBar superview]) {
  598. tBottomY = self.digitalSignatureBar.frame.size.height;
  599. }
  600. if([self.signtureViewController.view superview]) {
  601. tTopY = self.signtureViewController.view.frame.size.height;
  602. }
  603. } else {
  604. [self.navigationController setNavigationBarHidden:YES animated:YES];
  605. [UIView animateWithDuration:0.3 animations:^{
  606. CGRect frame = self.digitalSignatureBar.frame;
  607. frame.origin.y = self.view.bounds.size.height;
  608. self.digitalSignatureBar.frame = frame;
  609. self.pdfListView.pageSliderView.alpha = 0.0;
  610. self.signtureViewController.view.alpha = 0.0;
  611. }];
  612. }
  613. } else {
  614. CGFloat tPosY = 0;
  615. if (self.navigationController.navigationBarHidden) {
  616. [self.navigationController setNavigationBarHidden:NO animated:YES];
  617. [UIView animateWithDuration:0.3 animations:^{
  618. self.pdfListView.pageSliderView.alpha = 1.0;
  619. self.signtureViewController.view.alpha = 1.0;
  620. }];
  621. CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
  622. tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height;
  623. } else {
  624. [self.navigationController setNavigationBarHidden:YES animated:YES];
  625. [UIView animateWithDuration:0.3 animations:^{
  626. self.pdfListView.pageSliderView.alpha = 0.0;
  627. self.signtureViewController.view.alpha = 0.0;
  628. }];
  629. }
  630. }
  631. if (CPDFDisplayDirectionVertical == [CPDFKitConfig sharedInstance].displayDirection) {
  632. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  633. inset.bottom = 10 + self.annotationBar.frame.size.height;
  634. self.pdfListView.documentView.contentInset = inset;
  635. } else{
  636. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  637. inset.bottom = 0;
  638. self.pdfListView.documentView.contentInset = inset;
  639. }
  640. }
  641. - (void)PDFListViewEditNote:(CPDFListView *)pdfListView forAnnotation:(CPDFAnnotation *)annotation {
  642. if([annotation isKindOfClass:[CPDFLinkAnnotation class]]) {
  643. [self.annotationBar buttonItemClicked_openAnnotation:self.titleButton];
  644. } else if ([annotation isKindOfClass:[CPDFWidgetAnnotation class]]) {
  645. [self.formBar buttonItemClicked_openOption:annotation];
  646. } else {
  647. CGRect rect = [self.pdfListView convertRect:annotation.bounds fromPage:annotation.page];
  648. CPDFNoteOpenViewController *noteVC = [[CPDFNoteOpenViewController alloc]initWithAnnotation:annotation];
  649. noteVC.delegate = self;
  650. [noteVC showViewController:self inRect:rect];
  651. }
  652. }
  653. - (void)PDFListViewChangedAnnotationType:(CPDFListView *)pdfListView forAnnotationMode:(CPDFViewAnnotationMode)annotationMode {
  654. if(CToolModelAnnotation == self.pdfListView.toolModel) {
  655. [self.annotationBar reloadData];
  656. }else if(CToolModelForm == self.pdfListView.toolModel) {
  657. [self.formBar reloadData];
  658. }
  659. }
  660. - (void)PDFListViewPerformUrl:(CPDFListView *)pdfView withContent:(NSString *)content {
  661. NSURL * url = [NSURL URLWithString:content];
  662. [[UIApplication sharedApplication] openURL:url];
  663. }
  664. - (void)PDFListViewPerformAddStamp:(CPDFListView *)pdfView atPoint:(CGPoint)point forPage:(CPDFPage *)page {
  665. [self.annotationBar addStampAnnotationWithPage:page point:point];
  666. }
  667. - (void)PDFListViewPerformAddImage:(CPDFListView *)pdfView atPoint:(CGPoint)point forPage:(CPDFPage *)page {
  668. [self.annotationBar addImageAnnotationWithPage:page point:point];
  669. }
  670. - (BOOL)PDFListViewerTouchEndedIsAudioRecordMedia:(CPDFListView *)pdfListView {
  671. if (CPDFMediaStateAudioRecord == [CPDFMediaManager shareManager].mediaState) {
  672. [self PDFListViewPerformTouchEnded:self.pdfListView];
  673. return YES;
  674. }
  675. return NO;
  676. }
  677. - (void)PDFListViewPerformCancelMedia:(CPDFListView *)pdfView atPoint:(CGPoint)point forPage:(CPDFPage *)page {
  678. [CPDFMediaManager shareManager].mediaState = CPDFMediaStateStop;
  679. }
  680. - (void)PDFListViewPerformRecordMedia:(CPDFListView *)pdfView atPoint:(CGPoint)point forPage:(CPDFPage *)page {
  681. if([self.soundPlayBar superview]) {
  682. if(self.soundPlayBar.soundState == CPDFSoundStatePlay) {
  683. [self.soundPlayBar stopAudioPlay];
  684. [self.soundPlayBar removeFromSuperview];
  685. } else if (self.soundPlayBar.soundState == CPDFSoundStateRecord) {
  686. [self.soundPlayBar stopRecord];
  687. [self.soundPlayBar removeFromSuperview];
  688. }
  689. }
  690. AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
  691. if (authStatus == AVAuthorizationStatusNotDetermined || authStatus == AVAuthorizationStatusDenied) {
  692. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
  693. if (granted) {
  694. } else {
  695. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  696. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  697. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  698. }
  699. }
  700. }];
  701. }
  702. if (authStatus == AVAuthorizationStatusAuthorized) {
  703. NSInteger pageindex = [self.pdfListView.document indexForPage:page];
  704. [CPDFMediaManager shareManager].mediaState = CPDFMediaStateAudioRecord;
  705. [CPDFMediaManager shareManager].pageNum = pageindex;
  706. [CPDFMediaManager shareManager].ptInPdf = point;
  707. _soundPlayBar = [[CPDFSoundPlayBar alloc] initWithStyle:self.annotationManage.annotStyle];
  708. _soundPlayBar.delegate = self;
  709. [_soundPlayBar showInView:self.pdfListView soundState:CPDFSoundStateRecord];
  710. [_soundPlayBar startAudioRecord];
  711. } else {
  712. return;
  713. }
  714. }
  715. - (void)PDFListViewPerformPlay:(CPDFListView *)pdfView forAnnotation:(CPDFSoundAnnotation *)annotation {
  716. NSString *filePath = [annotation mediaPath];
  717. if (filePath) {
  718. NSURL *URL = [NSURL fileURLWithPath:filePath];
  719. _soundPlayBar = [[CPDFSoundPlayBar alloc] initWithStyle:self.annotationManage.annotStyle];
  720. _soundPlayBar.delegate = self;
  721. [_soundPlayBar showInView:self.pdfListView soundState:CPDFSoundStatePlay];
  722. [_soundPlayBar setURL:URL];
  723. [_soundPlayBar startAudioPlay];
  724. [CPDFMediaManager shareManager].mediaState = CPDFMediaStateVedioPlaying;
  725. }
  726. }
  727. - (void)PDFListViewPerformSignatureWidget:(CPDFListView *)pdfView forAnnotation:(CPDFSignatureWidgetAnnotation *)annotation {
  728. if(CToolModelAnnotation == self.pdfListView.toolModel) {
  729. [self.annotationBar openSignatureAnnotation:annotation];
  730. }else if(CToolModelViewer == self.pdfListView.toolModel) {
  731. self.signatureAnnotation = annotation;
  732. CPDFSignature *signature = [annotation signature];
  733. if((signature.signers.count > 0) && (self.signatures.count > 0)) {
  734. CPDFSigntureVerifyDetailsViewController *vc = [[CPDFSigntureVerifyDetailsViewController alloc] init];
  735. CNavigationController *nav = [[CNavigationController alloc]initWithRootViewController:vc];
  736. vc.signature = signature;
  737. [self presentViewController:nav animated:YES completion:nil];
  738. } else {
  739. if ([self.digitalSignatureBar superview]) {
  740. self.signatureAnnotation = annotation;
  741. CSignatureTypeSelectView *signatureTypeSelectView = [[CSignatureTypeSelectView alloc] initWithFrame:self.view.frame height:216.0];
  742. signatureTypeSelectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  743. signatureTypeSelectView.delegate = self;
  744. [signatureTypeSelectView showinView:self.view];
  745. } else {
  746. self.signatureAnnotation = annotation;
  747. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  748. CPDFSignatureViewController *signatureVC = [[CPDFSignatureViewController alloc] initWithStyle:nil];
  749. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:signatureVC presentingViewController:self];
  750. signatureVC.delegate = self;
  751. signatureVC.transitioningDelegate = presentationController;
  752. [self presentViewController:signatureVC animated:YES completion:nil];
  753. }
  754. }
  755. }
  756. }
  757. - (void)PDFListViewEditProperties:(CPDFListView *)pdfListView forAnnotation:(CPDFAnnotation *)annotation {
  758. if(CToolModelAnnotation == self.pdfListView.toolModel){
  759. [self.annotationBar buttonItemClicked_openAnnotation:self.titleButton];
  760. }else if(CToolModelForm == self.pdfListView.toolModel) {
  761. [self.formBar buttonItemClicked_open:annotation];
  762. }
  763. }
  764. - (void)PDFListViewContentEditProperty:(CPDFListView *)pdfView point:(CGPoint)point {
  765. if([pdfView.editingArea isKindOfClass:[CPDFEditImageArea class]]) {
  766. self.editMode = CPDFEditModeImage;
  767. } else if([pdfView.editingArea isKindOfClass:[CPDFEditTextArea class]]) {
  768. self.editMode = CPDFEditModeText;
  769. }
  770. [self showMenuList];
  771. [self.toolBar updateButtonState];
  772. }
  773. - (void)PDFViewCurrentPageDidChanged:(CPDFView *)pdfView {
  774. if([pdfView.editingArea isKindOfClass:[CPDFEditImageArea class]]) {
  775. self.editMode = CPDFEditModeImage;
  776. }else if([pdfView.editingArea isKindOfClass:[CPDFEditTextArea class]]) {
  777. self.editMode = CPDFEditModeText;
  778. }
  779. [self.toolBar updateButtonState];
  780. [super PDFViewCurrentPageDidChanged:pdfView];
  781. }
  782. #pragma mark - CPDFKeyboardToolbarDelegate
  783. - (void)keyboardShouldDissmiss:(CPDFKeyboardToolbar *)toolbar {
  784. [self.pdfListView commitEditAnnotationFreeText];
  785. self.pdfListView.annotationMode = CPDFViewAnnotationModeNone;
  786. [self.annotationBar reloadData];
  787. }
  788. #pragma mark - CPDFAnnotationBarDelegate
  789. - (void)annotationBarClick:(CPDFAnnotationToolBar *)annotationBar clickAnnotationMode:(CPDFViewAnnotationMode)annotationMode forSelected:(BOOL)isSelected forButton:(UIButton *)button {
  790. if(CPDFViewAnnotationModeInk == annotationMode || CPDFViewAnnotationModePencilDrawing == annotationMode) {
  791. CGFloat tPosY = 0;
  792. if(isSelected) {
  793. [self.navigationController setNavigationBarHidden:YES animated:YES];
  794. [UIView animateWithDuration:0.3 animations:^{
  795. CGRect frame = self.annotationBar.frame;
  796. frame.origin.y = self.view.bounds.size.height;
  797. self.annotationBar.frame = frame;
  798. self.pdfListView.pageSliderView.alpha = 0.0;
  799. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  800. inset.bottom = 0;
  801. self.pdfListView.documentView.contentInset = inset;
  802. }];
  803. } else {
  804. [self.navigationController setNavigationBarHidden:NO animated:YES];
  805. [UIView animateWithDuration:0.3 animations:^{
  806. CGRect frame = self.annotationBar.frame;
  807. frame.origin.y = self.view.bounds.size.height-frame.size.height;
  808. self.annotationBar.frame = frame;
  809. self.pdfListView.pageSliderView.alpha = 1.0;
  810. }];
  811. CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
  812. tPosY = self.navigationController.navigationBar.frame.size.height + rectStatus.size.height;
  813. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  814. inset.bottom = self.annotationBar.frame.size.height;
  815. self.pdfListView.documentView.contentInset = inset;
  816. }
  817. } else if (CPDFViewAnnotationModeSound == annotationMode && !isSelected) {
  818. if(CPDFSoundStateRecord == self.soundPlayBar.soundState) {
  819. [self.soundPlayBar stopRecord];
  820. } else if (CPDFSoundStatePlay== self.soundPlayBar.soundState) {
  821. [self.soundPlayBar stopAudioPlay];
  822. }
  823. }
  824. }
  825. #pragma mark - CPDFNoteOpenViewControllerDelegate
  826. - (void)getNoteOpenViewController:(CPDFNoteOpenViewController *)noteOpenVC content:(NSString *)content isDelete:(BOOL)isDelete {
  827. if (isDelete) {
  828. [noteOpenVC.annotation.page removeAnnotation:noteOpenVC.annotation];
  829. [self.pdfListView setNeedsDisplayForPage:noteOpenVC.annotation.page];
  830. if([self.pdfListView.activeAnnotations containsObject:noteOpenVC.annotation]) {
  831. NSMutableArray *activeAnnotations = [NSMutableArray arrayWithArray:self.pdfListView.activeAnnotations];
  832. [activeAnnotations removeObject:noteOpenVC.annotation];
  833. [self.pdfListView updateActiveAnnotations:activeAnnotations];
  834. }
  835. } else {
  836. if([noteOpenVC.annotation isKindOfClass:[CPDFMarkupAnnotation class]]) {
  837. CPDFMarkupAnnotation *markupAnnotation = (CPDFMarkupAnnotation *)noteOpenVC.annotation;
  838. [markupAnnotation setContents:content?:@""];
  839. } else if(([noteOpenVC.annotation isKindOfClass:[CPDFTextAnnotation class]])){
  840. if(content && content.length > 0) {
  841. noteOpenVC.annotation.contents = content?:@"";
  842. } else {
  843. if([self.pdfListView.activeAnnotations containsObject:noteOpenVC.annotation]) {
  844. [self.pdfListView updateActiveAnnotations:@[]];
  845. }
  846. [noteOpenVC.annotation.page removeAnnotation:noteOpenVC.annotation];
  847. [self.pdfListView setNeedsDisplayForPage:noteOpenVC.annotation.page];
  848. }
  849. } else {
  850. noteOpenVC.annotation.contents = content?:@"";
  851. }
  852. }
  853. }
  854. #pragma mark - CPDFSoundPlayBarDelegate
  855. - (void)soundPlayBarRecordFinished:(CPDFSoundPlayBar *)soundPlayBar withFile:(NSString *)filePath {
  856. CPDFPage *page = [self.pdfListView.document pageAtIndex:[CPDFMediaManager shareManager].pageNum];
  857. CPDFSoundAnnotation *annotation = [[CPDFSoundAnnotation alloc] initWithDocument:self.pdfListView.document];
  858. if ([annotation setMediaPath:filePath]) {
  859. CGRect bounds = annotation.bounds;
  860. bounds.origin.x = [CPDFMediaManager shareManager].ptInPdf.x-bounds.size.width/2.0;
  861. bounds.origin.y = [CPDFMediaManager shareManager].ptInPdf.y-bounds.size.height/2.0;
  862. annotation.bounds = bounds;
  863. [self.pdfListView addAnnotation:annotation forPage:page];
  864. }
  865. [CPDFMediaManager shareManager].mediaState = CPDFMediaStateStop;
  866. [self.pdfListView stopRecord];
  867. }
  868. - (void)soundPlayBarRecordCancel:(CPDFSoundPlayBar *)soundPlayBar {
  869. if(CPDFSoundStateRecord == self.soundPlayBar.soundState) {
  870. [self.pdfListView stopRecord];
  871. }
  872. [CPDFMediaManager shareManager].mediaState = CPDFMediaStateStop;
  873. }
  874. - (void)soundPlayBarPlayClose:(CPDFSoundPlayBar *)soundPlayBar {
  875. [CPDFMediaManager shareManager].mediaState = CPDFMediaStateStop;
  876. }
  877. #pragma mark - UIDocumentPickerDelegate
  878. - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
  879. if (self.pkcs12DocumentPickerViewController == controller) {
  880. BOOL fileUrlAuthozied = [urls.firstObject startAccessingSecurityScopedResource];
  881. if(fileUrlAuthozied){
  882. NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
  883. NSError *error;
  884. [fileCoordinator coordinateReadingItemAtURL:urls.firstObject options:0 error:&error byAccessor:^(NSURL *newURL) {
  885. NSString *documentFolder = [NSHomeDirectory() stringByAppendingFormat:@"/%@/%@", @"Documents",@"Files"];
  886. if (![[NSFileManager defaultManager] fileExistsAtPath:documentFolder])
  887. [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:documentFolder] withIntermediateDirectories:YES attributes:nil error:nil];
  888. NSString * documentPath = [documentFolder stringByAppendingPathComponent:[newURL lastPathComponent]];
  889. if (![[NSFileManager defaultManager] fileExistsAtPath:documentPath]) {
  890. [[NSFileManager defaultManager] copyItemAtPath:newURL.path toPath:documentPath error:NULL];
  891. }
  892. NSURL *url = [NSURL fileURLWithPath:documentPath];
  893. CImportCertificateViewController *certificateViewController = [[CImportCertificateViewController alloc] initWithP12FilePath:url Annotation:self.signatureAnnotation];
  894. certificateViewController.delegate = self;
  895. [self presentViewController:certificateViewController animated:YES completion:nil];
  896. }];
  897. [urls.firstObject stopAccessingSecurityScopedResource];
  898. }
  899. } else if (self.documentPickerViewController == controller) {
  900. BOOL fileUrlAuthozied = [urls.firstObject startAccessingSecurityScopedResource];
  901. if(fileUrlAuthozied){
  902. if (self.pdfListView.isEditing) {
  903. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  904. if(self.pdfListView.isEdited)
  905. [self.pdfListView commitEditing];
  906. dispatch_async(dispatch_get_main_queue(), ^{
  907. [self.pdfListView endOfEditing];
  908. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  909. if(self.pdfListView.document.isModified)
  910. [self.pdfListView.document writeToURL:self.pdfListView.document.documentURL];
  911. dispatch_async(dispatch_get_main_queue(), ^{
  912. [self openFileWithUrls:urls];
  913. if([self.signtureViewController.view superview]) {
  914. [self.signtureViewController.view removeFromSuperview];
  915. }
  916. NSArray *signatures = [self.pdfListView.document signatures];
  917. [self.digitalSignatureBar updateStatusWithsignatures:signatures];
  918. });
  919. });
  920. });
  921. });
  922. } else {
  923. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  924. if(self.pdfListView.document.isModified) {
  925. [self.pdfListView.document writeToURL:self.pdfListView.document.documentURL];
  926. }
  927. dispatch_async(dispatch_get_main_queue(), ^{
  928. [self openFileWithUrls:urls];
  929. if([self.signtureViewController.view superview]) {
  930. [self.signtureViewController.view removeFromSuperview];
  931. }
  932. NSArray *signatures = [self.pdfListView.document signatures];
  933. [self.digitalSignatureBar updateStatusWithsignatures:signatures];
  934. });
  935. });
  936. }
  937. }
  938. }
  939. }
  940. #pragma mark - CDigitalTypeSelectViewDelegate
  941. - (void)CDigitalTypeSelectViewUse:(CDigitalTypeSelectView *)digitalTypeSelectView {
  942. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  943. dispatch_async(dispatch_get_main_queue(), ^{
  944. NSArray *documentTypes = @[(NSString *)kUTTypePKCS12];
  945. self.pkcs12DocumentPickerViewController = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:documentTypes inMode:UIDocumentPickerModeOpen];
  946. self.pkcs12DocumentPickerViewController.delegate = self;
  947. [self presentViewController:self.pkcs12DocumentPickerViewController animated:YES completion:nil];
  948. });
  949. });
  950. }
  951. - (void)CDigitalTypeSelectViewCreate:(CDigitalTypeSelectView *)digitalTypeSelectView {
  952. CCreateCertificateInfoViewController *createCertificateViewController = [[CCreateCertificateInfoViewController alloc] initWithAnnotation:self.signatureAnnotation];
  953. createCertificateViewController.delegate = self;
  954. [self presentViewController:createCertificateViewController animated:YES completion:nil];
  955. }
  956. #pragma mark - CCertificateViewControllerDelegate
  957. - (void)importCertificateViewControllerSave:(CImportCertificateViewController *)importCertificateViewController PKCS12Cert:(nonnull NSString *)path password:(nonnull NSString *)password config:(nonnull CPDFSignatureConfig *)config {
  958. [self dismissViewControllerAnimated:NO completion:nil];
  959. [self writeSignatureToWidget:self.signatureAnnotation PKCS12Cert:path password:password config:config lockDocument:self.pdfListView.document.isLocked];
  960. }
  961. - (void)importCertificateViewControllerCancel:(CImportCertificateViewController *)importCertificateViewController {
  962. [self.signatureAnnotation reset];
  963. [self.signatureAnnotation updateAppearanceStream];
  964. [self dismissViewControllerAnimated:NO completion:nil];
  965. [self.pdfListView setNeedsDisplayForPage:self.signatureAnnotation.page];
  966. CSignatureTypeSelectView *signatureTypeSelectView = [[CSignatureTypeSelectView alloc] initWithFrame:self.view.frame height:216.0];
  967. signatureTypeSelectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  968. signatureTypeSelectView.delegate = self;
  969. [signatureTypeSelectView showinView:self.view];
  970. }
  971. #pragma mark - CCreateCertificateInfoViewControllerDelegate
  972. - (void)createCertificateInfoViewControllerSave:(CCreateCertificateInfoViewController *)createCertificateInfoViewController PKCS12Cert:(nonnull NSString *)path password:(nonnull NSString *)password config:(nonnull CPDFSignatureConfig *)config {
  973. [self.pdfListView setNeedsDisplayForPage:self.signatureAnnotation.page];
  974. [self dismissViewControllerAnimated:NO completion:nil];
  975. [self writeSignatureToWidget:self.signatureAnnotation PKCS12Cert:path password:password config:config lockDocument:self.pdfListView.document.isLocked];
  976. }
  977. - (void)createCertificateInfoViewControllerCancel:(CCreateCertificateInfoViewController *)createCertificateInfoViewController {
  978. [self.signatureAnnotation reset];
  979. [self.signatureAnnotation updateAppearanceStream];
  980. [self.pdfListView setNeedsDisplayForPage:self.signatureAnnotation.page];
  981. [self dismissViewControllerAnimated:NO completion:nil];
  982. CSignatureTypeSelectView *signatureTypeSelectView = [[CSignatureTypeSelectView alloc] initWithFrame:self.view.frame height:216.0];
  983. signatureTypeSelectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  984. signatureTypeSelectView.delegate = self;
  985. [signatureTypeSelectView showinView:self.view];
  986. }
  987. -(void)verifySignature {
  988. NSArray *signatures = self.signatures;
  989. if (signatures.count > 0) {
  990. [self.navigationController.view setUserInteractionEnabled:NO];
  991. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  992. for (CPDFSignature *sign in self.signatures) {
  993. [sign verifySignatureWithDocument:self.pdfListView.document];
  994. }
  995. dispatch_async(dispatch_get_main_queue(), ^{
  996. [self.navigationController.view setUserInteractionEnabled:YES];
  997. __block __typeof(self) blockSelf = self;
  998. [self.signtureViewController updateCertState:signatures];
  999. self.signtureViewController.callback = ^{
  1000. blockSelf.signtureVerifyViewController = [[CPDFSigntureVerifyViewController alloc] init];
  1001. blockSelf.signtureVerifyViewController.delegate = blockSelf;
  1002. blockSelf.signtureVerifyViewController.signatures = signatures;
  1003. blockSelf.signtureVerifyViewController.PDFListView = blockSelf.pdfListView;
  1004. CNavigationController *nav = [[CNavigationController alloc]initWithRootViewController:blockSelf.signtureVerifyViewController];
  1005. [blockSelf presentViewController:nav animated:YES completion:nil];
  1006. };
  1007. if(![self.signtureViewController.view superview]) {
  1008. [self.view addSubview:self.signtureViewController.view];
  1009. [self.view bringSubviewToFront:self.signtureViewController.view];
  1010. }
  1011. CGFloat height = CGRectGetMaxY(self.navigationController.navigationBar.frame) ;
  1012. self.signtureViewController.view.frame = CGRectMake(0, height, self.view.frame.size.width, 44.0);
  1013. CGFloat tPosY = 0;
  1014. if([self.signtureViewController.view superview]) {
  1015. tPosY = self.signtureViewController.view.frame.size.height;
  1016. }
  1017. if (CPDFDisplayDirectionVertical == [CPDFKitConfig sharedInstance].displayDirection) {
  1018. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  1019. inset.top = tPosY;
  1020. self.pdfListView.documentView.contentInset = inset;
  1021. } else{
  1022. UIEdgeInsets inset = self.pdfListView.documentView.contentInset;
  1023. self.pdfListView.documentView.contentInset = inset;
  1024. }
  1025. });
  1026. });
  1027. } else {
  1028. if([self.signtureViewController.view superview]) {
  1029. [self.signtureViewController.view removeFromSuperview];
  1030. self.signtureViewController = nil;
  1031. }
  1032. }
  1033. }
  1034. #pragma mark - CPDFSignatureViewControllerDelegate
  1035. - (void)verifySignatureBar:(CPDFDigitalSignatureToolBar *)pdfSignatureBar souceButton:(UIButton *)souceButton {
  1036. [self verifySignature];
  1037. }
  1038. - (void)addSignatureBar:(CPDFDigitalSignatureToolBar *)pdfSignatureBar souceButton:(UIButton *)souceButton {
  1039. self.isSelctSignature = souceButton.isSelected;
  1040. if(self.isSelctSignature) {
  1041. self.pdfListView.toolModel = CToolModelForm;
  1042. self.pdfListView.annotationMode = CPDFViewFormModeSign;
  1043. } else {
  1044. self.pdfListView.toolModel = CToolModelViewer;
  1045. }
  1046. }
  1047. #pragma mark - CSignatureTypeSelectViewDelegate
  1048. - (void)signatureTypeSelectViewElectronic:(CSignatureTypeSelectView *)signatureTypeSelectView {
  1049. AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  1050. CPDFSignatureViewController *signatureVC = [[CPDFSignatureViewController alloc] initWithStyle:nil];
  1051. presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:signatureVC presentingViewController:self];
  1052. signatureVC.delegate = self;
  1053. signatureVC.transitioningDelegate = presentationController;
  1054. [self presentViewController:signatureVC animated:YES completion:nil];
  1055. }
  1056. - (void)signatureTypeSelectViewDigital:(CSignatureTypeSelectView *)signatureTypeSelectView {
  1057. CDigitalTypeSelectView *digitalTypeSelect = [[CDigitalTypeSelectView alloc] init];
  1058. digitalTypeSelect.delegate = self;
  1059. digitalTypeSelect.frame = self.view.frame;
  1060. digitalTypeSelect.pageFromTo = ^(NSInteger from, NSInteger to) {
  1061. };
  1062. [digitalTypeSelect showinView:self.view];
  1063. }
  1064. #pragma mark - Notification
  1065. -(void)signatureHaveChangeDidChangeNotification:(NSNotification*)notification {
  1066. if (self.pdfListView == notification.object) {
  1067. // [self.pdfListView layoutDocumentView];
  1068. NSArray *signatures = [self.pdfListView.document signatures];
  1069. NSMutableArray *mSignatures = [NSMutableArray array];
  1070. for (CPDFSignature *sign in signatures) {
  1071. if (sign.signers.count > 0) {
  1072. [mSignatures addObject:sign];
  1073. }
  1074. }
  1075. self.signatures = mSignatures;
  1076. }
  1077. [self verifySignature];
  1078. }
  1079. -(void)signatureTrustCerDidChangeNotification:(NSNotification*)notification {
  1080. NSArray *signatures = [self.pdfListView.document signatures];
  1081. NSMutableArray *mSignatures = [NSMutableArray array];
  1082. for (CPDFSignature *sign in signatures) {
  1083. if (sign.signers.count > 0) {
  1084. [mSignatures addObject:sign];
  1085. }
  1086. }
  1087. self.signatures = mSignatures;
  1088. [self verifySignature];
  1089. if (self.signtureVerifyViewController) {
  1090. self.signtureVerifyViewController.signatures = signatures;
  1091. self.signtureVerifyViewController.PDFListView = self.pdfListView;
  1092. [self.signtureVerifyViewController reloadData];
  1093. }
  1094. }
  1095. - (void)PDFPageDidRemoveAnnotationNotification:(NSNotification *)notification {
  1096. CPDFAnnotation *annotation = [notification object];
  1097. if ([annotation isKindOfClass:[CPDFSoundAnnotation class]]) {
  1098. [self.soundPlayBar stopAudioPlay];
  1099. if ([self.soundPlayBar isDescendantOfView:self.view]) {
  1100. [self.soundPlayBar removeFromSuperview];
  1101. }
  1102. }
  1103. }
  1104. #pragma mark - CPDFToolsViewControllerDelegate
  1105. - (void)CPDFToolsViewControllerDismiss:(CPDFToolsViewController *) viewController selectItemAtIndex:(CToolModel)selectIndex {
  1106. if(CToolModelViewer == selectIndex) {
  1107. //viewwer
  1108. [self enterViewerMode];
  1109. }else if(CToolModelEdit == selectIndex) {
  1110. [self enterEditMode];
  1111. }else if(CToolModelAnnotation == selectIndex){
  1112. //Annotation
  1113. [self enterAnnotationMode];
  1114. }else if(CToolModelForm == selectIndex) {
  1115. [self.formBar updateStatus];
  1116. [self enterFormMode];
  1117. }else if(CToolModelSignatureMode == selectIndex) {
  1118. [self enterSignatureMode];
  1119. }
  1120. }
  1121. #pragma mark - CPDFBOTAViewControllerDelegate
  1122. - (void)botaViewControllerDismiss:(CPDFBOTAViewController *)botaViewController {
  1123. [self.navigationController dismissViewControllerAnimated:YES completion:nil];
  1124. }
  1125. #pragma mark - CPDFSignatureViewControllerDelegate
  1126. - (void)signatureViewControllerDismiss:(CPDFSignatureViewController *)signatureViewController {
  1127. self.signatureAnnotation = nil;
  1128. }
  1129. - (void)signatureViewController:(CPDFSignatureViewController *)signatureViewController image:(UIImage *)image {
  1130. if(self.signatureAnnotation) {
  1131. [self.signatureAnnotation signWithImage:image];
  1132. [self.pdfListView setNeedsDisplayForPage:self.signatureAnnotation.page];
  1133. self.signatureAnnotation = nil;
  1134. }
  1135. }
  1136. #pragma mark - Action
  1137. - (void)buttonItemClicked_thumbnail:(id)sender {
  1138. if(self.pdfListView.activeAnnotations.count > 0) {
  1139. [self.pdfListView updateActiveAnnotations:@[]];
  1140. [self.pdfListView setNeedsDisplayForVisiblePages];
  1141. }
  1142. if(self.pdfListView.isEditing) {
  1143. if(self.pdfListView.isEdited) {
  1144. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1145. [self.pdfListView commitEditing];
  1146. dispatch_async(dispatch_get_main_queue(), ^{
  1147. [self enterThumbnail];
  1148. });
  1149. });
  1150. } else {
  1151. [self enterThumbnail];
  1152. }
  1153. } else {
  1154. [self enterThumbnail];
  1155. }
  1156. }
  1157. - (void)enterThumbnail {
  1158. CPDFPageEditViewController *pageEditViewcontroller = [[CPDFPageEditViewController alloc] initWithPDFView:self.pdfListView];
  1159. pageEditViewcontroller.pageEditDelegate = self;
  1160. CNavigationController *nav = [[CNavigationController alloc]initWithRootViewController:pageEditViewcontroller];
  1161. nav.modalPresentationStyle = UIModalPresentationFullScreen;
  1162. [self.navigationController presentViewController:nav animated:YES completion:nil];
  1163. }
  1164. #pragma mark - CPDFPageEditViewControllerDelegate
  1165. - (void)pageEditViewControllerDone:(CPDFPageEditViewController *)pageEditViewController {
  1166. __weak typeof(self) weakSelf = self;
  1167. [pageEditViewController dismissViewControllerAnimated:YES completion:^{
  1168. if (pageEditViewController.isPageEdit) {
  1169. [weakSelf reloadDocumentWithFilePath:self.filePath password:self.pdfListView.document.password completion:^(BOOL result) {
  1170. [weakSelf.pdfListView reloadInputViews];
  1171. [weakSelf selectDocumentRefresh];
  1172. }];
  1173. [weakSelf.pdfListView reloadInputViews];
  1174. }
  1175. }];
  1176. }
  1177. - (void)pageEditViewController:(CPDFPageEditViewController *)pageEditViewController pageIndex:(NSInteger)pageIndex isPageEdit:(BOOL)isPageEdit {
  1178. __weak typeof(self) weakSelf = self;
  1179. [pageEditViewController dismissViewControllerAnimated:YES completion:^{
  1180. if (isPageEdit) {
  1181. [weakSelf reloadDocumentWithFilePath:self.filePath password:self.pdfListView.document.password completion:^(BOOL result) {
  1182. [weakSelf.pdfListView reloadInputViews];
  1183. [weakSelf selectDocumentRefresh];
  1184. [self.pdfListView goToPageIndex:pageIndex animated:NO];
  1185. }];
  1186. [weakSelf.pdfListView reloadInputViews];
  1187. } else {
  1188. [self.pdfListView goToPageIndex:pageIndex animated:NO];
  1189. }
  1190. }];
  1191. }
  1192. #pragma mark - imagePickerDelegate
  1193. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  1194. NSURL * url = nil;
  1195. if (@available(iOS 11.0, *))
  1196. url = info[UIImagePickerControllerImageURL];
  1197. else
  1198. url = info[UIImagePickerControllerMediaURL];
  1199. UIImage * image = [[UIImage alloc] initWithContentsOfFile:url.path];
  1200. CGFloat img_width = 0;
  1201. CGFloat img_height = 0;
  1202. CGFloat scaled_width = 149;
  1203. CGFloat scaled_height = 210;
  1204. if(image.imageOrientation!=UIImageOrientationUp){
  1205. UIGraphicsBeginImageContext(image.size);
  1206. [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
  1207. image = UIGraphicsGetImageFromCurrentImageContext();
  1208. UIGraphicsEndImageContext();
  1209. img_width = image.size.height;
  1210. img_height = image.size.width;
  1211. } else {
  1212. img_width = image.size.width;
  1213. img_height = image.size.height;
  1214. }
  1215. CGFloat scaled = MIN(scaled_width/img_width, scaled_height/img_height);
  1216. scaled_height = img_height*scaled;
  1217. scaled_width = img_width*scaled;
  1218. CGRect rect = CGRectMake(self.addImageRect.origin.x, self.addImageRect.origin.y- scaled_height, scaled_width, scaled_height);
  1219. [self.pdfListView createEmptyImage:rect page:self.addImagePage path:url.path];
  1220. [picker dismissViewControllerAnimated:YES completion:nil];
  1221. }
  1222. @end