CPDFInfoViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. //
  2. // CPDFInfoViewController.m
  3. // compdfkit-tools
  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 "CPDFInfoViewController.h"
  13. #import "CPDFInfoTableCell.h"
  14. #import <ComPDFKit/ComPDFKit.h>
  15. #import "CPDFColorUtils.h"
  16. @interface CPDFInfoViewController()<UITableViewDelegate,UITableViewDataSource>
  17. @property (nonatomic, retain) UITableView* curTableView;
  18. @property (nonatomic, retain) NSArray* curTableArray;
  19. @property (nonatomic, strong) UILabel *titleLabel;
  20. @property (nonatomic, strong) UIButton *doneBtn;
  21. @end
  22. @implementation CPDFInfoViewController
  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 life cycle
  31. - (void)viewDidLoad
  32. {
  33. [super viewDidLoad];
  34. if (@available(iOS 12.0, *)) {
  35. BOOL isDark = (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark);
  36. if(isDark){
  37. self.view.backgroundColor = [UIColor blackColor];
  38. }else{
  39. self.view.backgroundColor = [UIColor colorWithRed:240/255. green:240/255. blue:240/255. alpha:1.];
  40. }
  41. } else {
  42. self.view.backgroundColor = [UIColor colorWithRed:240/255. green:240/255. blue:240/255. alpha:1.];
  43. }
  44. self.titleLabel = [[UILabel alloc] init];
  45. self.titleLabel.text = NSLocalizedString(@"Information", nil);
  46. self.titleLabel.font = [UIFont boldSystemFontOfSize:17];
  47. self.titleLabel.adjustsFontSizeToFitWidth = YES;
  48. self.titleLabel.textAlignment = NSTextAlignmentCenter;
  49. [self.view addSubview:self.titleLabel];
  50. [self loadDocumentInfo];
  51. [self.view addSubview:self.curTableView];
  52. self.view.backgroundColor = [CPDFColorUtils CPDFViewControllerBackgroundColor];
  53. [self updatePreferredContentSizeWithTraitCollection:self.traitCollection];
  54. self.doneBtn = [UIButton buttonWithType:UIButtonTypeSystem];
  55. self.doneBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  56. [self.doneBtn setTitle:NSLocalizedString(@"Done", nil) forState:UIControlStateNormal];
  57. [self.doneBtn addTarget:self action:@selector(buttonItemClicked_back:) forControlEvents:UIControlEventTouchUpInside];
  58. [self.view addSubview:self.doneBtn];
  59. }
  60. - (void)buttonItemClicked_back:(UIButton *)button {
  61. [self dismissViewControllerAnimated:YES completion:^{
  62. }];
  63. }
  64. - (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  65. [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
  66. [self updatePreferredContentSizeWithTraitCollection:newCollection];
  67. }
  68. - (void)updatePreferredContentSizeWithTraitCollection:(UITraitCollection *)traitCollection
  69. {
  70. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width,traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact ? (self.view.bounds.size.width*0.9) : 800);
  71. }
  72. - (void)viewWillLayoutSubviews {
  73. self.titleLabel.frame = CGRectMake((self.view.frame.size.width - 120)/2, 5, 120, 50);
  74. self.curTableView.frame = CGRectMake(0, 70, self.view.frame.size.width, self.view.frame.size.height - 50);
  75. self.doneBtn.frame = CGRectMake(self.view.frame.size.width - 60, 5, 50, 50);
  76. }
  77. -(void)viewWillDisappear:(BOOL)animated
  78. {
  79. [super viewWillDisappear:animated];
  80. }
  81. #pragma mark - getter && setter
  82. - (UITableView*) curTableView {
  83. if (!_curTableView) {
  84. _curTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  85. _curTableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
  86. _curTableView.delegate = self;
  87. _curTableView.dataSource = self;
  88. _curTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
  89. _curTableView.backgroundColor = [UIColor clearColor];
  90. }
  91. return _curTableView;
  92. }
  93. #pragma mark - private method
  94. - (NSString *)fileSizeStr {
  95. NSFileManager *defaultManager = [NSFileManager defaultManager];
  96. if (![defaultManager fileExistsAtPath:self.currentPath]) {
  97. return @"";
  98. }
  99. NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:self.currentPath error:nil];
  100. float tFileSize = [[attrib objectForKey:NSFileSize] floatValue];
  101. float fileSize = tFileSize / 1024;
  102. float size = fileSize >= 1024 ?(fileSize < 1048576 ? fileSize/1024.0 : fileSize/1048576.0) : fileSize;
  103. char unit = fileSize >= 1024 ? (fileSize < 1048576 ? 'M':'G'):'K';
  104. return [NSString stringWithFormat:@"%0.1f%c", size, unit];
  105. }
  106. - (void) loadDocumentInfo {
  107. NSDictionary *documentAttributes = [self.pdfView.document documentAttributes];
  108. self.currentPath = self.pdfView.document.documentURL.path;
  109. NSMutableArray* tableArray = [NSMutableArray array];
  110. // 1.abstract
  111. NSMutableArray* mArray = [NSMutableArray array];
  112. if (self.currentPath) {
  113. [mArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"File Name:",kDocumentInfoTitle nil),kDocumentInfoTitle, [self.currentPath lastPathComponent], kDocumentInfoValue, nil]];
  114. [mArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Size:",kDocumentInfoTitle nil),kDocumentInfoTitle, [self fileSizeStr], kDocumentInfoValue, nil]];
  115. }
  116. if (documentAttributes[CPDFDocumentTitleAttribute]) {
  117. [mArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Title:",kDocumentInfoTitle nil),kDocumentInfoTitle, documentAttributes[CPDFDocumentTitleAttribute], kDocumentInfoValue, nil]];
  118. }
  119. if (documentAttributes[CPDFDocumentAuthorAttribute]) {
  120. [mArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Author:",kDocumentInfoTitle nil),kDocumentInfoTitle, documentAttributes[CPDFDocumentAuthorAttribute], kDocumentInfoValue, nil]];
  121. }
  122. if (documentAttributes[CPDFDocumentSubjectAttribute]) {
  123. [mArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Subject:",kDocumentInfoTitle nil),kDocumentInfoTitle, documentAttributes[CPDFDocumentSubjectAttribute], kDocumentInfoValue, nil]];
  124. }
  125. if (documentAttributes[CPDFDocumentKeywordsAttribute]) {
  126. [mArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Keywords:",kDocumentInfoTitle nil),kDocumentInfoTitle, documentAttributes[CPDFDocumentKeywordsAttribute], kDocumentInfoValue, nil]];
  127. }
  128. [tableArray addObject:mArray];
  129. // 2.create
  130. mArray = [NSMutableArray array];
  131. NSString * versionString = [NSString stringWithFormat:@"%ld.%ld",(long)self.pdfView.document.majorVersion,(long)self.pdfView.document.minorVersion];
  132. [mArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Version:",kDocumentInfoTitle nil),kDocumentInfoTitle, versionString, kDocumentInfoValue, nil]];
  133. [mArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Pages:",kDocumentInfoTitle nil),kDocumentInfoTitle, [NSString stringWithFormat:@"%zd", self.pdfView.document.pageCount], kDocumentInfoValue, nil]];
  134. if (documentAttributes[CPDFDocumentCreatorAttribute]) {
  135. [mArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Creator:",kDocumentInfoTitle nil),kDocumentInfoTitle, documentAttributes[CPDFDocumentCreatorAttribute], kDocumentInfoValue, nil]];
  136. }
  137. if (documentAttributes[CPDFDocumentCreationDateAttribute]){
  138. NSMutableString* mSting = [NSMutableString string];
  139. NSString* tstring = [NSString stringWithFormat:@"%@",documentAttributes[CPDFDocumentCreationDateAttribute]];
  140. if (tstring.length >= 16) {
  141. NSRange range;
  142. range.location = 2;range.length=4;
  143. [mSting appendString:[tstring substringWithRange:range]];
  144. range.location = 6;range.length=2;
  145. [mSting appendFormat:@"-%@",[tstring substringWithRange:range]];
  146. range.location = 8;range.length=2;
  147. [mSting appendFormat:@"-%@",[tstring substringWithRange:range]];
  148. range.location = 10;range.length=2;
  149. [mSting appendFormat:@" %@",[tstring substringWithRange:range]];
  150. range.location = 12;range.length=2;
  151. [mSting appendFormat:@":%@",[tstring substringWithRange:range]];
  152. range.location = 14;range.length=2;
  153. [mSting appendFormat:@":%@",[tstring substringWithRange:range]];
  154. [mArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Creation Date:",kDocumentInfoTitle nil),kDocumentInfoTitle, mSting, kDocumentInfoValue, nil]];
  155. }
  156. }
  157. if (documentAttributes[CPDFDocumentModificationDateAttribute]){
  158. NSMutableString* mSting = [NSMutableString string];
  159. NSString* tstring = [NSString stringWithFormat:@"%@",documentAttributes[CPDFDocumentModificationDateAttribute]];
  160. if (tstring.length >= 16) {
  161. NSRange range;
  162. range.location = 2;range.length=4;
  163. [mSting appendString:[tstring substringWithRange:range]];
  164. range.location = 6;range.length=2;
  165. [mSting appendFormat:@"-%@",[tstring substringWithRange:range]];
  166. range.location = 8;range.length=2;
  167. [mSting appendFormat:@"-%@",[tstring substringWithRange:range]];
  168. range.location = 10;range.length=2;
  169. [mSting appendFormat:@" %@",[tstring substringWithRange:range]];
  170. range.location = 12;range.length=2;
  171. [mSting appendFormat:@":%@",[tstring substringWithRange:range]];
  172. range.location = 14;range.length=2;
  173. [mSting appendFormat:@":%@",[tstring substringWithRange:range]];
  174. [mArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Modification Date:",kDocumentInfoTitle nil),kDocumentInfoTitle, mSting, kDocumentInfoValue, nil]];
  175. }
  176. }
  177. [tableArray addObject:mArray];
  178. // 3.execute
  179. mArray = [NSMutableArray array];
  180. [mArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Allow Printing:",kDocumentInfoTitle nil),kDocumentInfoTitle, (self.pdfView.document.allowsPrinting ? NSLocalizedString(@"YES", nil) : NSLocalizedString(@"NO", nil)), kDocumentInfoValue, nil]];
  181. [mArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Allow Copying:",kDocumentInfoTitle nil),kDocumentInfoTitle, (self.pdfView.document.allowsCopying ? NSLocalizedString(@"YES", nil) : NSLocalizedString(@"NO", nil)), kDocumentInfoValue, nil]];
  182. [mArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Allow Document Change:",kDocumentInfoTitle nil),kDocumentInfoTitle, (self.pdfView.document.allowsDocumentChanges ? NSLocalizedString(@"YES", nil) : NSLocalizedString(@"NO", nil)), kDocumentInfoValue, nil]];
  183. [mArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Allow Document Assembly:",kDocumentInfoTitle nil),kDocumentInfoTitle, (self.pdfView.document.allowsDocumentAssembly ? NSLocalizedString(@"YES", nil) : NSLocalizedString(@"NO", nil)), kDocumentInfoValue, nil]];
  184. [mArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Allow Document Commenting:",kDocumentInfoTitle nil),kDocumentInfoTitle, ((self.pdfView.document.allowsCommenting == YES) ? NSLocalizedString(@"YES", nil) : NSLocalizedString(@"NO", nil)), kDocumentInfoValue, nil]];
  185. [mArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Allow Document Form Field Entry:",kDocumentInfoTitle nil),kDocumentInfoTitle, ((self.pdfView.document.allowsFormFieldEntry == YES) ? NSLocalizedString(@"YES", nil) : NSLocalizedString(@"NO", nil)), kDocumentInfoValue, nil]];
  186. [tableArray addObject:mArray];
  187. self.curTableArray = tableArray;
  188. }
  189. #pragma mark - tableview delegate & datasource
  190. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  191. {
  192. return [self.curTableArray count];
  193. }
  194. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  195. {
  196. NSString *title;
  197. switch (section)
  198. {
  199. case 0:
  200. {
  201. title = NSLocalizedString(@"Abstract:", nil);
  202. break;
  203. }
  204. case 1:
  205. {
  206. title = NSLocalizedString(@"Create Information:", nil);
  207. break;
  208. }
  209. case 2:
  210. {
  211. title = NSLocalizedString(@"Access Permissions:", nil);
  212. break;
  213. }
  214. }
  215. return title;
  216. }
  217. -(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  218. {
  219. UILabel *headerLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, tableView.bounds.size.width, 20)];
  220. NSString *title;
  221. switch (section)
  222. {
  223. case 0:
  224. {
  225. title = NSLocalizedString(@"Abstract:", nil);
  226. break;
  227. }
  228. case 1:
  229. {
  230. title = NSLocalizedString(@"Create Information:", nil);
  231. break;
  232. }
  233. case 2:
  234. {
  235. title = NSLocalizedString(@"Access Permissions:", nil);
  236. break;
  237. }
  238. }
  239. title = [NSString stringWithFormat:@" %@",title];
  240. headerLabel.text = title;
  241. headerLabel.font = [UIFont boldSystemFontOfSize:22];
  242. headerLabel.textColor = [UIColor blackColor];
  243. return headerLabel;
  244. }
  245. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  246. {
  247. NSMutableArray* array = [self.curTableArray objectAtIndex:section];
  248. return [array count];
  249. }
  250. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  251. {
  252. return 44;
  253. }
  254. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  255. {
  256. CPDFInfoTableCell *cell = (CPDFInfoTableCell*)[tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
  257. if (cell == nil)
  258. {
  259. cell = [[CPDFInfoTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MyIdentifier"];
  260. }
  261. NSMutableArray* array = [self.curTableArray objectAtIndex:indexPath.section];
  262. cell.dataDictionary = [array objectAtIndex:indexPath.row];
  263. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  264. return cell;
  265. }
  266. @end