PDFAnnotationListViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. //
  2. // PDFAnnotationListViewController.m
  3. // PDFReader
  4. //
  5. // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // The PDF Reader Sample applications are licensed with a modified BSD license.
  8. // Please see License for details. This notice may not be removed from this file.
  9. //
  10. #import "PDFAnnotationListViewController.h"
  11. #import "PDFFilterView.h"
  12. @interface PDFAnnotationViewCell : UITableViewCell
  13. @property (nonatomic,assign) IBOutlet UIImageView *typeImageView;
  14. @property (nonatomic,assign) IBOutlet UILabel *contentsLabel;
  15. @property (nonatomic,assign) IBOutlet UILabel *dateLabel;
  16. @end
  17. @implementation PDFAnnotationViewCell
  18. @end
  19. @interface PDFAnnotationListViewController () <UITableViewDelegate,UITableViewDataSource>
  20. @property (nonatomic,retain) NSArray *annotations;
  21. @property (nonatomic,retain) UITableView *tableView;
  22. @property (nonatomic,retain) UIButton *filterButton;
  23. @property (nonatomic,retain) PDFFilterView *filterView;
  24. @property (nonatomic,retain) UILabel *noDataLabel;
  25. @property (nonatomic,assign) BOOL isHighlightSelected;
  26. @property (nonatomic,assign) BOOL isUnderlineSelected;
  27. @property (nonatomic,assign) BOOL isWavelineSelected;
  28. @property (nonatomic,assign) BOOL isNoteSelected;
  29. @property (nonatomic,assign) BOOL isOtherSelected;
  30. @end
  31. @implementation PDFAnnotationListViewController
  32. #pragma mark - Init Methods
  33. - (void)dealloc {
  34. [_annotations release];
  35. _tableView.delegate = nil;
  36. _tableView.dataSource = nil;
  37. [_tableView release];
  38. [_filterButton release];
  39. [_filterView release];
  40. [_noDataLabel release];
  41. [super dealloc];
  42. }
  43. #pragma mark - UIViewController Methods
  44. - (void)viewDidLoad {
  45. [super viewDidLoad];
  46. // Do any additional setup after loading the view.
  47. self.tableView = [[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain] autorelease];
  48. self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  49. self.tableView.dataSource = self;
  50. self.tableView.delegate = self;
  51. self.tableView.rowHeight = UITableViewAutomaticDimension;
  52. self.tableView.estimatedRowHeight = 60;
  53. self.tableView.tableFooterView = [[[UIView alloc] init] autorelease];
  54. [self.tableView registerNib:[UINib nibWithNibName:@"PDFAnnotationViewCell" bundle:nil]
  55. forCellReuseIdentifier:@"cell"];
  56. [self.view addSubview:self.tableView];
  57. self.noDataLabel = [[[UILabel alloc] init] autorelease];
  58. self.noDataLabel.textColor = [UIColor grayColor];
  59. self.noDataLabel.text = NSLocalizedString(@"No annotations", nil);
  60. [self.noDataLabel sizeToFit];
  61. self.noDataLabel.center = CGPointMake(self.view.bounds.size.width/2.0, self.view.bounds.size.height/2.0);
  62. self.noDataLabel.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  63. [self.view addSubview:self.noDataLabel];
  64. UITableViewHeaderFooterView *filterView = [[[UITableViewHeaderFooterView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)] autorelease];
  65. filterView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  66. filterView.backgroundColor = [UIColor whiteColor];
  67. self.tableView.tableHeaderView = filterView;
  68. self.tableView.contentInset = UIEdgeInsetsMake(-44, 0, 0, 0);
  69. self.tableView.contentOffset = CGPointMake(0, 44);
  70. self.filterButton = [UIButton buttonWithType:UIButtonTypeSystem];
  71. self.filterButton.frame = filterView.bounds;
  72. self.filterButton.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  73. [self.filterButton setTitle:NSLocalizedString(@"Filter", nil) forState:UIControlStateNormal];
  74. [self.filterButton setTitleColor:[UIColor colorWithRed:74.0/255.0 green:74.0/255.0 blue:74.0/255.0 alpha:1.0] forState:UIControlStateNormal];
  75. self.filterButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  76. self.filterButton.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
  77. [self.filterButton addTarget:self action:@selector(buttonItemClicked_Filter:) forControlEvents:UIControlEventTouchUpInside];
  78. [filterView.contentView addSubview:self.filterButton];
  79. UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.filterButton.bounds.size.width-31, (self.filterButton.bounds.size.height-16)/2.0, 16, 16)];
  80. imageView.image = [UIImage imageNamed:@"btn_bota_more.png"];
  81. imageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  82. [self.filterButton addSubview:imageView];
  83. UIView *line = [[[UIView alloc] initWithFrame:CGRectMake(0, filterView.bounds.size.height-1, filterView.bounds.size.width, 1)] autorelease];
  84. line.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin;
  85. line.backgroundColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0];
  86. [filterView addSubview:line];
  87. self.isHighlightSelected = YES;
  88. self.isUnderlineSelected = YES;
  89. self.isWavelineSelected = YES;
  90. self.isNoteSelected = YES;
  91. self.isOtherSelected= YES;
  92. }
  93. - (void)viewDidAppear:(BOOL)animated {
  94. [super viewDidAppear:animated];
  95. [self reloadData];
  96. }
  97. - (void)viewWillAppear:(BOOL)animated {
  98. [super viewWillAppear:animated];
  99. UIBarButtonItem *deleteItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
  100. target:self
  101. action:@selector(buttonItemClicked_Delete:)];
  102. [self.parentViewController.navigationItem setRightBarButtonItem:deleteItem animated:YES];
  103. [deleteItem release];
  104. }
  105. - (void)viewWillDisappear:(BOOL)animated {
  106. [super viewWillDisappear:animated];
  107. [self.parentViewController.navigationItem setRightBarButtonItem:nil animated:YES];
  108. }
  109. #pragma mark - Button Actions
  110. - (void)buttonItemClicked_Delete:(id)sender {
  111. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Yes", nil)
  112. style:UIAlertActionStyleDefault
  113. handler:^(UIAlertAction *action){
  114. self.pdfViewController.pdfView.activeAnnotation = nil;
  115. for (int i=0; i<self.pdfViewController.pdfView.document.pageCount; i++) {
  116. CPDFPage *page = [self.pdfViewController.pdfView.document pageAtIndex:i];
  117. [page removeAllAnnotations];
  118. }
  119. [self.pdfViewController.pdfView setNeedsDisplayForVisiblePages];
  120. [self reloadData];
  121. }];
  122. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"No", nil)
  123. style:UIAlertActionStyleCancel
  124. handler:nil];
  125. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
  126. message:NSLocalizedString(@"This will permanently remove all annotations. Are you sure to continue?", nil)
  127. preferredStyle:UIAlertControllerStyleAlert];
  128. [alert addAction:okAction];
  129. [alert addAction:cancelAction];
  130. [self presentViewController:alert animated:YES completion:nil];
  131. }
  132. - (void)buttonItemClicked_Filter:(id)sender {
  133. if (self.filterView) {
  134. [self.filterView dismiss];
  135. self.filterView = nil;
  136. return;
  137. }
  138. NSMutableArray *selectedIndexes = [NSMutableArray array];
  139. if (self.isHighlightSelected) {
  140. [selectedIndexes addObject:@(0)];
  141. }
  142. if (self.isUnderlineSelected) {
  143. [selectedIndexes addObject:@(1)];
  144. }
  145. if (self.isWavelineSelected) {
  146. [selectedIndexes addObject:@(2)];
  147. }
  148. if (self.isNoteSelected) {
  149. [selectedIndexes addObject:@(3)];
  150. }
  151. if (self.isOtherSelected) {
  152. [selectedIndexes addObject:@(4)];
  153. }
  154. PDFFilterView *filterView = [[PDFFilterView alloc] init];
  155. filterView.items = @[[UIImage imageNamed:@"highlight.png"],
  156. [UIImage imageNamed:@"underline.png"],
  157. [UIImage imageNamed:@"squiggly.png"],
  158. [UIImage imageNamed:@"note.png"],
  159. [UIImage imageNamed:@"other.png"]];
  160. filterView.allowsMultipleSelection = YES;
  161. filterView.selectedIndexes = selectedIndexes;
  162. filterView.didSelected = ^(NSInteger index) {
  163. if (index >= 0) {
  164. [self setSelectedIndex:index];
  165. } else {
  166. self.filterView = nil;
  167. }
  168. };
  169. filterView.frame = CGRectMake(0, 44, self.view.bounds.size.width, self.view.bounds.size.height-44);
  170. [filterView showInView:self.view];
  171. [filterView release];
  172. self.filterView = filterView;
  173. }
  174. - (void)setSelectedIndex:(NSInteger)index {
  175. if (index == 0) {
  176. self.isHighlightSelected = !self.isHighlightSelected;
  177. } else if (index == 1) {
  178. self.isUnderlineSelected = !self.isUnderlineSelected;;
  179. } else if (index == 2) {
  180. self.isWavelineSelected = !self.isWavelineSelected;
  181. } else if (index == 3) {
  182. self.isNoteSelected = !self.isNoteSelected;
  183. } else if (index == 4) {
  184. self.isOtherSelected = !self.isOtherSelected;
  185. }
  186. [self reloadData];
  187. }
  188. - (void)reloadData {
  189. NSMutableArray *array = [NSMutableArray array];
  190. for (int i=0; i<self.pdfViewController.pdfView.document.pageCount; i++) {
  191. CPDFPage *page = [self.pdfViewController.pdfView.document pageAtIndex:i];
  192. NSArray *annotations = page.annotations;
  193. NSMutableArray *newAnnotations = [NSMutableArray array];
  194. for (CPDFAnnotation *annotation in annotations) {
  195. if ([annotation isKindOfClass:[CPDFLinkAnnotation class]] ||
  196. [annotation isKindOfClass:[CPDFWidgetAnnotation class]] ||
  197. [annotation isKindOfClass:[CPDFSignatureAnnotation class]]) {
  198. continue;
  199. }
  200. if ([annotation isKindOfClass:[CPDFMarkupAnnotation class]]) {
  201. CPDFMarkupType markupType = [(CPDFMarkupAnnotation *)annotation markupType];
  202. if (CPDFMarkupTypeHighlight == markupType) {
  203. if (self.isHighlightSelected) {
  204. [newAnnotations addObject:annotation];
  205. }
  206. } else if (CPDFMarkupTypeUnderline == markupType) {
  207. if (self.isUnderlineSelected) {
  208. [newAnnotations addObject:annotation];
  209. }
  210. } else if (CPDFMarkupTypeSquiggly == markupType) {
  211. if (self.isWavelineSelected) {
  212. [newAnnotations addObject:annotation];
  213. }
  214. } else {
  215. if (self.isOtherSelected) {
  216. [newAnnotations addObject:annotation];
  217. }
  218. }
  219. } else if ([annotation isKindOfClass:[CPDFTextAnnotation class]]) {
  220. if (self.isNoteSelected) {
  221. [newAnnotations addObject:annotation];
  222. }
  223. } else {
  224. if (self.isOtherSelected) {
  225. [newAnnotations addObject:annotation];
  226. }
  227. }
  228. }
  229. if (newAnnotations.count > 0) {
  230. NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
  231. dictionary[@"TPageIndex"] = @(i);
  232. dictionary[@"TAnnotations"] = newAnnotations;
  233. [array addObject:dictionary];
  234. }
  235. }
  236. self.annotations = array;
  237. [self.tableView reloadData];
  238. }
  239. #pragma mark - UITableViewDataSource
  240. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  241. if (self.annotations.count > 0) {
  242. self.noDataLabel.hidden = YES;
  243. } else {
  244. self.noDataLabel.hidden = NO;
  245. }
  246. return self.annotations.count;
  247. }
  248. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  249. return [self.annotations[section][@"TAnnotations"] count];
  250. }
  251. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  252. PDFAnnotationViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
  253. NSDictionary *dictionary = self.annotations[indexPath.section];
  254. CPDFAnnotation *annotation = dictionary[@"TAnnotations"][indexPath.row];
  255. NSString *type = annotation.type;
  256. NSString *string = annotation.contents;
  257. NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
  258. formatter.dateFormat = @"yyyy-MM-dd hh:mm a";
  259. if ([annotation isKindOfClass:[CPDFMarkupAnnotation class]]) {
  260. string = [(CPDFMarkupAnnotation *)annotation markupText];
  261. }
  262. cell.dateLabel.text = [formatter stringFromDate:annotation.modificationDate];
  263. cell.contentsLabel.text = string;
  264. cell.typeImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png", [type lowercaseString]]];
  265. return cell;
  266. }
  267. #pragma mark - UITableViewDelegate
  268. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  269. return 20;
  270. }
  271. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  272. NSDictionary *dictionary = self.annotations[section];
  273. NSInteger pageIndex = [dictionary[@"TPageIndex"] integerValue];
  274. NSInteger annotationCount = [dictionary[@"TAnnotations"] count];
  275. UITableViewHeaderFooterView *headerView = [[[UITableViewHeaderFooterView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 20)] autorelease];
  276. headerView.contentView.backgroundColor = [UIColor colorWithRed:56.0/255.0 green:194.0/255.0 blue:254.0/255.0 alpha:1.0];
  277. UILabel *pageLabel = [[[UILabel alloc] initWithFrame:CGRectMake(5, 0, 200, 20)] autorelease];
  278. pageLabel.backgroundColor = [UIColor clearColor];
  279. pageLabel.textColor = [UIColor whiteColor];
  280. pageLabel.font = [UIFont boldSystemFontOfSize:13.0f];
  281. pageLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Page %ld",nil), pageIndex+1];
  282. [headerView.contentView addSubview:pageLabel];
  283. UILabel *countLabel = [[[UILabel alloc] initWithFrame:CGRectMake(headerView.frame.size.width-105, 0, 100, 20)] autorelease];
  284. countLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  285. countLabel.backgroundColor = [UIColor clearColor];
  286. countLabel.textColor = [UIColor whiteColor];
  287. countLabel.font = [UIFont boldSystemFontOfSize:13.0f];
  288. countLabel.textAlignment = NSTextAlignmentRight;
  289. countLabel.text = [NSString stringWithFormat:@"%ld", annotationCount];
  290. [headerView.contentView addSubview:countLabel];
  291. return headerView;
  292. }
  293. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  294. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  295. NSDictionary *dictionary = self.annotations[indexPath.section];
  296. NSInteger pageIndex = [dictionary[@"TPageIndex"] integerValue];
  297. [self dismissViewControllerAnimated:YES completion:^{
  298. [self.pdfViewController.pdfView goToPageIndex:pageIndex animated:NO];
  299. }];
  300. }
  301. #pragma mark - UIScrollViewDelegate
  302. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  303. if (scrollView == self.tableView) {
  304. CGFloat sectionHeaderHeight = 44;
  305. if (scrollView.contentOffset.y <= sectionHeaderHeight &&
  306. scrollView.contentOffset.y >= 0) {
  307. scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
  308. } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
  309. scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
  310. } else if (scrollView.contentOffset.y <= 0) {
  311. scrollView.contentOffset = CGPointMake(0, 0);
  312. }
  313. }
  314. }
  315. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
  316. if (scrollView == self.tableView) {
  317. CGFloat sectionHeaderHeight = 44;
  318. if (scrollView.contentOffset.y >= 0 &&
  319. scrollView.contentOffset.y <= sectionHeaderHeight/2.0) {
  320. [UIView animateWithDuration:0.3 animations:^{
  321. scrollView.contentOffset = CGPointMake(0, 0);
  322. }];
  323. } else if (scrollView.contentOffset.y >= sectionHeaderHeight/2.0 &&
  324. scrollView.contentOffset.y <= sectionHeaderHeight) {
  325. [UIView animateWithDuration:0.3 animations:^{
  326. scrollView.contentOffset = CGPointMake(0, sectionHeaderHeight);
  327. }];
  328. }
  329. }
  330. }
  331. @end