123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- //
- // PDFAnnotationListViewController.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 "PDFAnnotationListViewController.h"
- #import "PDFFilterView.h"
- @interface PDFAnnotationViewCell : UITableViewCell
- @property (nonatomic,assign) IBOutlet UIImageView *typeImageView;
- @property (nonatomic,assign) IBOutlet UILabel *contentsLabel;
- @property (nonatomic,assign) IBOutlet UILabel *dateLabel;
- @end
- @implementation PDFAnnotationViewCell
- @end
- @interface PDFAnnotationListViewController () <UITableViewDelegate,UITableViewDataSource>
- @property (nonatomic,retain) NSArray *annotations;
- @property (nonatomic,retain) UITableView *tableView;
- @property (nonatomic,retain) UIButton *filterButton;
- @property (nonatomic,retain) PDFFilterView *filterView;
- @property (nonatomic,retain) UILabel *noDataLabel;
- @property (nonatomic,assign) BOOL isHighlightSelected;
- @property (nonatomic,assign) BOOL isUnderlineSelected;
- @property (nonatomic,assign) BOOL isWavelineSelected;
- @property (nonatomic,assign) BOOL isNoteSelected;
- @property (nonatomic,assign) BOOL isOtherSelected;
- @end
- @implementation PDFAnnotationListViewController
- #pragma mark - Init Methods
- - (void)dealloc {
- [_annotations release];
- _tableView.delegate = nil;
- _tableView.dataSource = nil;
- [_tableView release];
- [_filterButton release];
- [_filterView release];
- [_noDataLabel release];
- [super dealloc];
- }
- #pragma mark - UIViewController Methods
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- // Do any additional setup after loading the view.
- self.tableView = [[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain] autorelease];
- self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
- self.tableView.dataSource = self;
- self.tableView.delegate = self;
- self.tableView.rowHeight = UITableViewAutomaticDimension;
- self.tableView.estimatedRowHeight = 60;
- self.tableView.tableFooterView = [[[UIView alloc] init] autorelease];
- [self.tableView registerNib:[UINib nibWithNibName:@"PDFAnnotationViewCell" bundle:nil]
- forCellReuseIdentifier:@"cell"];
- [self.view addSubview:self.tableView];
-
- self.noDataLabel = [[[UILabel alloc] init] autorelease];
- self.noDataLabel.textColor = [UIColor grayColor];
- self.noDataLabel.text = NSLocalizedString(@"No annotations", nil);
- [self.noDataLabel sizeToFit];
- self.noDataLabel.center = CGPointMake(self.view.bounds.size.width/2.0, self.view.bounds.size.height/2.0);
- self.noDataLabel.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
- [self.view addSubview:self.noDataLabel];
-
- UITableViewHeaderFooterView *filterView = [[[UITableViewHeaderFooterView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)] autorelease];
- filterView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
- filterView.backgroundColor = [UIColor whiteColor];
- self.tableView.tableHeaderView = filterView;
- self.tableView.contentInset = UIEdgeInsetsMake(-44, 0, 0, 0);
- self.tableView.contentOffset = CGPointMake(0, 44);
-
- self.filterButton = [UIButton buttonWithType:UIButtonTypeSystem];
- self.filterButton.frame = filterView.bounds;
- self.filterButton.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
- [self.filterButton setTitle:NSLocalizedString(@"Filter", nil) forState:UIControlStateNormal];
- [self.filterButton setTitleColor:[UIColor colorWithRed:74.0/255.0 green:74.0/255.0 blue:74.0/255.0 alpha:1.0] forState:UIControlStateNormal];
- self.filterButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
- self.filterButton.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
- [self.filterButton addTarget:self action:@selector(buttonItemClicked_Filter:) forControlEvents:UIControlEventTouchUpInside];
- [filterView.contentView addSubview:self.filterButton];
-
- UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.filterButton.bounds.size.width-31, (self.filterButton.bounds.size.height-16)/2.0, 16, 16)];
- imageView.image = [UIImage imageNamed:@"btn_bota_more.png"];
- imageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
- [self.filterButton addSubview:imageView];
-
- UIView *line = [[[UIView alloc] initWithFrame:CGRectMake(0, filterView.bounds.size.height-1, filterView.bounds.size.width, 1)] autorelease];
- line.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin;
- line.backgroundColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0];
- [filterView addSubview:line];
-
- self.isHighlightSelected = YES;
- self.isUnderlineSelected = YES;
- self.isWavelineSelected = YES;
- self.isNoteSelected = YES;
- self.isOtherSelected= YES;
- }
- - (void)viewDidAppear:(BOOL)animated {
- [super viewDidAppear:animated];
-
- [self reloadData];
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
-
- UIBarButtonItem *deleteItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
- target:self
- action:@selector(buttonItemClicked_Delete:)];
- [self.parentViewController.navigationItem setRightBarButtonItem:deleteItem animated:YES];
- [deleteItem release];
- }
- - (void)viewWillDisappear:(BOOL)animated {
- [super viewWillDisappear:animated];
-
- [self.parentViewController.navigationItem setRightBarButtonItem:nil animated:YES];
- }
- #pragma mark - Button Actions
- - (void)buttonItemClicked_Delete:(id)sender {
- UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Yes", nil)
- style:UIAlertActionStyleDefault
- handler:^(UIAlertAction *action){
- self.pdfViewController.pdfView.activeAnnotation = nil;
- for (int i=0; i<self.pdfViewController.pdfView.document.pageCount; i++) {
- CPDFPage *page = [self.pdfViewController.pdfView.document pageAtIndex:i];
- [page removeAllAnnotations];
- }
- [self.pdfViewController.pdfView setNeedsDisplayForVisiblePages];
- [self reloadData];
- }];
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"No", nil)
- style:UIAlertActionStyleCancel
- handler:nil];
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
- message:NSLocalizedString(@"This will permanently remove all annotations. Are you sure to continue?", nil)
- preferredStyle:UIAlertControllerStyleAlert];
- [alert addAction:okAction];
- [alert addAction:cancelAction];
- [self presentViewController:alert animated:YES completion:nil];
- }
- - (void)buttonItemClicked_Filter:(id)sender {
- if (self.filterView) {
- [self.filterView dismiss];
- self.filterView = nil;
- return;
- }
-
- NSMutableArray *selectedIndexes = [NSMutableArray array];
- if (self.isHighlightSelected) {
- [selectedIndexes addObject:@(0)];
- }
- if (self.isUnderlineSelected) {
- [selectedIndexes addObject:@(1)];
- }
- if (self.isWavelineSelected) {
- [selectedIndexes addObject:@(2)];
- }
- if (self.isNoteSelected) {
- [selectedIndexes addObject:@(3)];
- }
- if (self.isOtherSelected) {
- [selectedIndexes addObject:@(4)];
- }
- PDFFilterView *filterView = [[PDFFilterView alloc] init];
- filterView.items = @[[UIImage imageNamed:@"highlight.png"],
- [UIImage imageNamed:@"underline.png"],
- [UIImage imageNamed:@"squiggly.png"],
- [UIImage imageNamed:@"note.png"],
- [UIImage imageNamed:@"other.png"]];
- filterView.allowsMultipleSelection = YES;
- filterView.selectedIndexes = selectedIndexes;
- filterView.didSelected = ^(NSInteger index) {
- if (index >= 0) {
- [self setSelectedIndex:index];
- } else {
- self.filterView = nil;
- }
- };
- filterView.frame = CGRectMake(0, 44, self.view.bounds.size.width, self.view.bounds.size.height-44);
- [filterView showInView:self.view];
- [filterView release];
-
- self.filterView = filterView;
- }
- - (void)setSelectedIndex:(NSInteger)index {
- if (index == 0) {
- self.isHighlightSelected = !self.isHighlightSelected;
- } else if (index == 1) {
- self.isUnderlineSelected = !self.isUnderlineSelected;;
- } else if (index == 2) {
- self.isWavelineSelected = !self.isWavelineSelected;
- } else if (index == 3) {
- self.isNoteSelected = !self.isNoteSelected;
- } else if (index == 4) {
- self.isOtherSelected = !self.isOtherSelected;
- }
- [self reloadData];
- }
- - (void)reloadData {
- NSMutableArray *array = [NSMutableArray array];
- for (int i=0; i<self.pdfViewController.pdfView.document.pageCount; i++) {
- CPDFPage *page = [self.pdfViewController.pdfView.document pageAtIndex:i];
- NSArray *annotations = page.annotations;
- NSMutableArray *newAnnotations = [NSMutableArray array];
- for (CPDFAnnotation *annotation in annotations) {
- if ([annotation isKindOfClass:[CPDFLinkAnnotation class]] ||
- [annotation isKindOfClass:[CPDFWidgetAnnotation class]] ||
- [annotation isKindOfClass:[CPDFSignatureAnnotation class]]) {
- continue;
- }
- if ([annotation isKindOfClass:[CPDFMarkupAnnotation class]]) {
- CPDFMarkupType markupType = [(CPDFMarkupAnnotation *)annotation markupType];
- if (CPDFMarkupTypeHighlight == markupType) {
- if (self.isHighlightSelected) {
- [newAnnotations addObject:annotation];
- }
- } else if (CPDFMarkupTypeUnderline == markupType) {
- if (self.isUnderlineSelected) {
- [newAnnotations addObject:annotation];
- }
- } else if (CPDFMarkupTypeSquiggly == markupType) {
- if (self.isWavelineSelected) {
- [newAnnotations addObject:annotation];
- }
- } else {
- if (self.isOtherSelected) {
- [newAnnotations addObject:annotation];
- }
- }
- } else if ([annotation isKindOfClass:[CPDFTextAnnotation class]]) {
- if (self.isNoteSelected) {
- [newAnnotations addObject:annotation];
- }
- } else {
- if (self.isOtherSelected) {
- [newAnnotations addObject:annotation];
- }
- }
- }
- if (newAnnotations.count > 0) {
- NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
- dictionary[@"TPageIndex"] = @(i);
- dictionary[@"TAnnotations"] = newAnnotations;
- [array addObject:dictionary];
- }
- }
- self.annotations = array;
- [self.tableView reloadData];
- }
- #pragma mark - UITableViewDataSource
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- if (self.annotations.count > 0) {
- self.noDataLabel.hidden = YES;
- } else {
- self.noDataLabel.hidden = NO;
- }
- return self.annotations.count;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return [self.annotations[section][@"TAnnotations"] count];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- PDFAnnotationViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
- NSDictionary *dictionary = self.annotations[indexPath.section];
- CPDFAnnotation *annotation = dictionary[@"TAnnotations"][indexPath.row];
- NSString *type = annotation.type;
- NSString *string = annotation.contents;
- NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
- formatter.dateFormat = @"yyyy-MM-dd hh:mm a";
- if ([annotation isKindOfClass:[CPDFMarkupAnnotation class]]) {
- string = [(CPDFMarkupAnnotation *)annotation markupText];
- }
- cell.dateLabel.text = [formatter stringFromDate:annotation.modificationDate];
- cell.contentsLabel.text = string;
- cell.typeImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png", [type lowercaseString]]];
- return cell;
- }
- #pragma mark - UITableViewDelegate
- - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
- return 20;
- }
- - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
- NSDictionary *dictionary = self.annotations[section];
- NSInteger pageIndex = [dictionary[@"TPageIndex"] integerValue];
- NSInteger annotationCount = [dictionary[@"TAnnotations"] count];
-
- UITableViewHeaderFooterView *headerView = [[[UITableViewHeaderFooterView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 20)] autorelease];
- headerView.contentView.backgroundColor = [UIColor colorWithRed:56.0/255.0 green:194.0/255.0 blue:254.0/255.0 alpha:1.0];
- UILabel *pageLabel = [[[UILabel alloc] initWithFrame:CGRectMake(5, 0, 200, 20)] autorelease];
- pageLabel.backgroundColor = [UIColor clearColor];
- pageLabel.textColor = [UIColor whiteColor];
- pageLabel.font = [UIFont boldSystemFontOfSize:13.0f];
- pageLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Page %ld",nil), pageIndex+1];
- [headerView.contentView addSubview:pageLabel];
-
- UILabel *countLabel = [[[UILabel alloc] initWithFrame:CGRectMake(headerView.frame.size.width-105, 0, 100, 20)] autorelease];
- countLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
- countLabel.backgroundColor = [UIColor clearColor];
- countLabel.textColor = [UIColor whiteColor];
- countLabel.font = [UIFont boldSystemFontOfSize:13.0f];
- countLabel.textAlignment = NSTextAlignmentRight;
- countLabel.text = [NSString stringWithFormat:@"%ld", annotationCount];
- [headerView.contentView addSubview:countLabel];
-
- return headerView;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
- NSDictionary *dictionary = self.annotations[indexPath.section];
- NSInteger pageIndex = [dictionary[@"TPageIndex"] integerValue];
- [self dismissViewControllerAnimated:YES completion:^{
- [self.pdfViewController.pdfView goToPageIndex:pageIndex animated:NO];
- }];
- }
- #pragma mark - UIScrollViewDelegate
- - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
- if (scrollView == self.tableView) {
- CGFloat sectionHeaderHeight = 44;
- if (scrollView.contentOffset.y <= sectionHeaderHeight &&
- scrollView.contentOffset.y >= 0) {
- scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
- } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
- scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
- } else if (scrollView.contentOffset.y <= 0) {
- scrollView.contentOffset = CGPointMake(0, 0);
- }
- }
- }
- - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
- if (scrollView == self.tableView) {
- CGFloat sectionHeaderHeight = 44;
- if (scrollView.contentOffset.y >= 0 &&
- scrollView.contentOffset.y <= sectionHeaderHeight/2.0) {
- [UIView animateWithDuration:0.3 animations:^{
- scrollView.contentOffset = CGPointMake(0, 0);
- }];
- } else if (scrollView.contentOffset.y >= sectionHeaderHeight/2.0 &&
- scrollView.contentOffset.y <= sectionHeaderHeight) {
- [UIView animateWithDuration:0.3 animations:^{
- scrollView.contentOffset = CGPointMake(0, sectionHeaderHeight);
- }];
- }
- }
- }
- @end
|