CPDFOutlineViewController.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // PDFOutlineViewController.m
  3. // ComPDFKit_Tools
  4. //
  5. // Copyright © 2014-2024 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 "CPDFOutlineViewController.h"
  13. #import "CPDFOutlineModel.h"
  14. #import "CPDFOutlineViewCell.h"
  15. #import <ComPDFKit/ComPDFKit.h>
  16. #define kPDFOutlineItemMaxDeep 10
  17. @interface CPDFOutlineViewController () <UITableViewDataSource, UITableViewDelegate, CPDFOutlineViewCellDelegate>
  18. @property (nonatomic, strong) UITableView *tableView;
  19. @property (nonatomic, strong) NSArray *outlines;
  20. @property (nonatomic, strong) UILabel *noDataLabel;
  21. @end
  22. @implementation CPDFOutlineViewController
  23. #pragma mark - Initializers
  24. - (instancetype)initWithPDFView:(CPDFView *)pdfView {
  25. if (self = [super init]) {
  26. _pdfView = pdfView;
  27. }
  28. return self;
  29. }
  30. #pragma mark - Accessors
  31. #pragma mark - UIViewController Methods
  32. - (void)loadOutline:(CPDFOutline *)outline level:(NSInteger)level forOutlines:(NSMutableArray *)outlines {
  33. for (int i=0; i<[outline numberOfChildren]; i++) {
  34. CPDFOutline *data = [outline childAtIndex:i];
  35. CPDFDestination *destination = [data destination];
  36. if (!destination) {
  37. CPDFAction *action = [data action];
  38. if (action && [action isKindOfClass:[CPDFGoToAction class]]) {
  39. destination = [(CPDFGoToAction *)action destination];
  40. }
  41. }
  42. CPDFOutlineModel *model = [[CPDFOutlineModel alloc] init];
  43. model.level = level;
  44. model.hide = (1 << (kPDFOutlineItemMaxDeep - level)) - 1;
  45. model.title = data.label;
  46. model.count = data.numberOfChildren;
  47. model.number = destination.pageIndex;
  48. model.isShow = NO;
  49. [outlines addObject:model];
  50. [self loadOutline:data level:level+1 forOutlines:outlines];
  51. }
  52. }
  53. - (void)viewDidLoad {
  54. [super viewDidLoad];
  55. CPDFOutline *outlineRoot = [self.pdfView.document outlineRoot];
  56. NSMutableArray *array = [NSMutableArray array];
  57. [self loadOutline:outlineRoot level:0 forOutlines:array];
  58. self.outlines = array;
  59. self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
  60. self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  61. self.tableView.delegate = self;
  62. self.tableView.dataSource = self;
  63. self.tableView.rowHeight = UITableViewAutomaticDimension;
  64. self.tableView.estimatedRowHeight = 80;
  65. self.tableView.tableFooterView = [[UIView alloc] init];
  66. [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
  67. self.noDataLabel = [[UILabel alloc] init];
  68. self.noDataLabel.textColor = [UIColor grayColor];
  69. self.noDataLabel.text = NSLocalizedString(@"No Outlines", nil);
  70. [self.noDataLabel sizeToFit];
  71. self.noDataLabel.center = CGPointMake(self.view.bounds.size.width/2.0, self.view.bounds.size.height/2.0);
  72. self.noDataLabel.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  73. [self.view addSubview:self.tableView];
  74. [self.view addSubview:self.noDataLabel];
  75. }
  76. #pragma mark - UITableViewDataSource
  77. const static int kShowFlag = (1 << kPDFOutlineItemMaxDeep) -1;
  78. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  79. int n = 0;
  80. for (CPDFOutlineModel *outline in self.outlines) {
  81. if (outline.hide == kShowFlag)
  82. n ++ ;
  83. }
  84. if (n > 0) {
  85. self.noDataLabel.hidden = YES;
  86. } else {
  87. self.noDataLabel.hidden = NO;
  88. }
  89. return n;
  90. }
  91. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  92. int t = -1;
  93. CPDFOutlineModel *outline = nil;
  94. for (CPDFOutlineModel *model in self.outlines) {
  95. if (model.hide == kShowFlag)
  96. t++;
  97. if (t == indexPath.row) {
  98. outline = model;
  99. break;
  100. }
  101. }
  102. CPDFOutlineViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  103. if (cell == nil) {
  104. cell = [[CPDFOutlineViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
  105. }
  106. cell.outline = outline;
  107. cell.delegate = self;
  108. return cell;
  109. }
  110. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  111. return 32;
  112. }
  113. #pragma mark - UITableViewDelegate
  114. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  115. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  116. CPDFOutlineViewCell *cell = (CPDFOutlineViewCell *)[tableView cellForRowAtIndexPath:indexPath];
  117. CPDFOutlineModel *outline = cell.outline;
  118. if([self.delegate respondsToSelector:@selector(outlineViewController:pageIndex:)]) {
  119. [self.delegate outlineViewController:self pageIndex:outline.number];
  120. }
  121. }
  122. #pragma mark - Action
  123. - (void)buttonItemClicked_Arrow:(CPDFOutlineViewCell *)cell {
  124. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  125. int t = -1, p = 0;
  126. CPDFOutlineModel *outline = nil;
  127. for (CPDFOutlineModel *model in self.outlines) {
  128. if (model.hide == kShowFlag) t++;
  129. if (t == indexPath.row) {
  130. outline = model;
  131. break;
  132. }
  133. p++;
  134. }
  135. if (outline.level == kPDFOutlineItemMaxDeep-1) return;
  136. p++;
  137. if (p == self.outlines.count) return;
  138. CPDFOutlineModel *nextOutline = self.outlines[p];
  139. if (nextOutline.level > outline.level) {
  140. if (nextOutline.hide == kShowFlag) {
  141. [(CPDFOutlineViewCell *)[self.tableView cellForRowAtIndexPath:indexPath] setIsShow:NO];
  142. NSMutableArray *array = [NSMutableArray array];
  143. while (true) {
  144. if (nextOutline.hide == kShowFlag) {
  145. t ++;
  146. NSIndexPath* path = [NSIndexPath indexPathForRow:t inSection:0];
  147. [array addObject:path];
  148. }
  149. nextOutline.hide ^= 1 << (kPDFOutlineItemMaxDeep - outline.level - 1);
  150. p++;
  151. if (p == self.outlines.count) break;
  152. nextOutline = self.outlines[p];
  153. if (nextOutline.level <= outline.level) break;
  154. }
  155. [self.tableView deleteRowsAtIndexPaths:array
  156. withRowAnimation:UITableViewRowAnimationFade];
  157. } else {
  158. [(CPDFOutlineViewCell *)[self.tableView cellForRowAtIndexPath:indexPath] setIsShow:YES];
  159. NSMutableArray *array = [NSMutableArray array];
  160. while (true) {
  161. nextOutline.hide ^= 1 << (kPDFOutlineItemMaxDeep - outline.level - 1);
  162. if (nextOutline.hide == kShowFlag) {
  163. t ++;
  164. NSIndexPath* path = [NSIndexPath indexPathForRow:t inSection:0];
  165. [array addObject:path];
  166. }
  167. p++;
  168. if (p == self.outlines.count) break;
  169. nextOutline = self.outlines[p];
  170. if (nextOutline.level <= outline.level) break;
  171. }
  172. [self.tableView insertRowsAtIndexPaths:array
  173. withRowAnimation:UITableViewRowAnimationFade];
  174. }
  175. }
  176. }
  177. @end