StampFileManager.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. //
  2. // StampFileManager.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 "StampFileManager.h"
  11. @implementation StampFileManager
  12. #pragma mark - Init Method
  13. - (id)init
  14. {
  15. self = [super init];
  16. if (self) {
  17. _deleteList = [[NSMutableArray alloc] init];
  18. }
  19. return self;
  20. }
  21. - (void)dealloc
  22. {
  23. [_stampTextList release];
  24. [_stampImageList release];
  25. //在这里删除不用的图片
  26. [self removeStampImage];
  27. [_deleteList release];
  28. [super dealloc];
  29. }
  30. - (NSString *)getDateTime
  31. {
  32. NSTimeZone* timename = [NSTimeZone systemTimeZone];
  33. NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init ];
  34. [outputFormatter setTimeZone:timename ];
  35. NSString *tDate = nil;
  36. [outputFormatter setDateFormat:@"YYYYMMddHHmmss"];
  37. tDate = [[outputFormatter stringFromDate:[NSDate date]] retain];
  38. [outputFormatter release];
  39. return tDate;
  40. }
  41. #pragma mark - File Manager
  42. - (void)readStampDataFromFile
  43. {
  44. [self readCustomStamp_TextStamp];
  45. [self readCustomStamp_ImageStamp];
  46. }
  47. - (void)readCustomStamp_TextStamp
  48. {
  49. NSFileManager *tManager = [NSFileManager defaultManager];
  50. if (![tManager fileExistsAtPath:kPDFStampTextList])
  51. {
  52. _stampTextList = [[NSMutableArray alloc] init];
  53. }
  54. else
  55. {
  56. if (_stampTextList)
  57. {
  58. [_stampTextList release];
  59. _stampTextList = nil;
  60. }
  61. _stampTextList = [[NSMutableArray arrayWithContentsOfFile:kPDFStampTextList] retain];
  62. if (_stampTextList == nil) {
  63. _stampTextList = [[NSMutableArray alloc] init];
  64. }
  65. }
  66. }
  67. - (void)readCustomStamp_ImageStamp
  68. {
  69. NSFileManager *tManager = [NSFileManager defaultManager];
  70. if (![tManager fileExistsAtPath:kPDFStampImageList])
  71. {
  72. _stampImageList = [[NSMutableArray alloc] init];
  73. }
  74. else
  75. {
  76. if (_stampImageList)
  77. {
  78. [_stampImageList release];
  79. _stampImageList = nil;
  80. }
  81. _stampImageList = [[NSMutableArray arrayWithContentsOfFile:kPDFStampImageList] retain];
  82. if (_stampImageList == nil) {
  83. _stampImageList = [[NSMutableArray alloc] init];
  84. }
  85. }
  86. }
  87. - (NSArray *)getTextStampData
  88. {
  89. return _stampTextList;
  90. }
  91. - (NSArray *)getImageStampData
  92. {
  93. return _stampImageList;
  94. }
  95. - (NSString *)saveStampWithImage:(UIImage *)image
  96. {
  97. NSFileManager *tManager = [NSFileManager defaultManager];
  98. NSData *imageData = UIImagePNGRepresentation(image);
  99. if (imageData == nil || [imageData length] <= 0)
  100. return nil;
  101. NSString *tName = [self getDateTime];
  102. NSString *tPath = [kPDFStampDataFolder stringByAppendingFormat:@"/%@.png",tName];
  103. [tName release];
  104. if ([imageData writeToFile:tPath atomically:NO]) {
  105. return tPath;
  106. }
  107. else
  108. {
  109. NSString *tPath_dic = kPDFStampDataFolder;
  110. BOOL tIsDirectory = NO;
  111. while (1)
  112. {
  113. if ([tManager fileExistsAtPath:tPath_dic isDirectory:&tIsDirectory]) {
  114. if (tIsDirectory)
  115. break;
  116. }
  117. else
  118. {
  119. [tManager createDirectoryAtPath:tPath_dic withIntermediateDirectories:NO attributes:nil error:nil];
  120. }
  121. tPath_dic = [tPath_dic stringByDeletingLastPathComponent];
  122. }
  123. if ([imageData writeToFile:tPath atomically:NO]) {
  124. return tPath;
  125. }
  126. }
  127. return nil;
  128. }
  129. - (void)removeStampImage
  130. {
  131. for (NSDictionary *tDict in _deleteList)
  132. {
  133. NSString *tPath = [tDict objectForKey:@"path"];
  134. NSFileManager *tFileManager = [NSFileManager defaultManager];
  135. [tFileManager removeItemAtPath:tPath error:nil];
  136. }
  137. }
  138. - (BOOL)saveStampDataToFile:(PDFStampCustomType)stampType
  139. {
  140. NSFileManager *tManager = [NSFileManager defaultManager];
  141. switch (stampType)
  142. {
  143. case PDFStampCustomType_Text:
  144. if ([_stampTextList writeToFile:kPDFStampTextList atomically:NO])
  145. {
  146. return YES;
  147. }
  148. else
  149. {
  150. NSString *tPath_dic = kPDFStampTextList;
  151. BOOL tIsDirectory = NO;
  152. while (1)
  153. {
  154. tPath_dic = [tPath_dic stringByDeletingLastPathComponent];
  155. if ([tManager fileExistsAtPath:tPath_dic isDirectory:&tIsDirectory]) {
  156. if (tIsDirectory)
  157. break;
  158. }
  159. else
  160. {
  161. [tManager createDirectoryAtPath:tPath_dic withIntermediateDirectories:NO attributes:nil error:nil];
  162. }
  163. }
  164. if ([_stampTextList writeToFile:kPDFStampTextList atomically:NO])
  165. {
  166. return YES;
  167. }
  168. }
  169. return NO;
  170. case PDFStampCustomType_Image:
  171. if ([_stampImageList writeToFile:kPDFStampImageList atomically:NO])
  172. {
  173. return YES;
  174. }
  175. else
  176. {
  177. NSString *tPath_dic = kPDFStampImageList;
  178. BOOL tIsDirectory = NO;
  179. while (1)
  180. {
  181. tPath_dic = [tPath_dic stringByDeletingLastPathComponent];
  182. if ([tManager fileExistsAtPath:tPath_dic isDirectory:&tIsDirectory]) {
  183. if (tIsDirectory)
  184. break;
  185. }
  186. else
  187. {
  188. [tManager createDirectoryAtPath:tPath_dic withIntermediateDirectories:NO attributes:nil error:nil];
  189. }
  190. }
  191. if ([_stampImageList writeToFile:kPDFStampImageList atomically:NO])
  192. {
  193. return YES;
  194. }
  195. }
  196. return NO;
  197. default:
  198. return NO;
  199. }
  200. }
  201. - (BOOL)insertStampItem:(NSDictionary *)stampItem type:(PDFStampCustomType)stampType
  202. {
  203. switch (stampType)
  204. {
  205. case PDFStampCustomType_Text:
  206. if (!_stampTextList) {
  207. [self readCustomStamp_TextStamp];
  208. }
  209. if (stampItem)
  210. {
  211. [_stampTextList addObject:stampItem];
  212. if ([self saveStampDataToFile:PDFStampCustomType_Text])
  213. return YES;
  214. else
  215. return NO;
  216. }
  217. else
  218. return NO;
  219. case PDFStampCustomType_Image:
  220. if (!_stampImageList) {
  221. [self readCustomStamp_ImageStamp];
  222. }
  223. if (stampItem)
  224. {
  225. [_stampImageList addObject:stampItem];
  226. if ([self saveStampDataToFile:PDFStampCustomType_Image])
  227. return YES;
  228. else
  229. return NO;
  230. }
  231. else
  232. return NO;
  233. default:
  234. return NO;
  235. }
  236. }
  237. - (BOOL)removeStampItem:(NSInteger)index type:(PDFStampCustomType)stampType
  238. {
  239. switch (stampType)
  240. {
  241. case PDFStampCustomType_Text:
  242. if (!_stampTextList) {
  243. [self readCustomStamp_TextStamp];
  244. }
  245. if (index >= 0 && index <= [_stampTextList count])
  246. {
  247. /* FIXME: 直接通过数组下角标删除元素,同时删除多个元素,
  248. * 会造出下传入的下角标混乱,如果是按照下角标从大往小删除
  249. * 不会出错 */
  250. [_stampTextList removeObjectAtIndex:index];
  251. if ([self saveStampDataToFile:PDFStampCustomType_Text])
  252. return YES;
  253. else
  254. return NO;
  255. }
  256. else
  257. return NO;
  258. case PDFStampCustomType_Image:
  259. if (!_stampImageList) {
  260. [self readCustomStamp_ImageStamp];
  261. }
  262. if (index >= 0 && index < [_stampImageList count])
  263. {
  264. //FiXME: 将要删除的图片放到_deleteList中,等释放这个类的时候删除图片
  265. if (!_deleteList) {
  266. _deleteList = [[NSMutableArray alloc] init];
  267. }
  268. NSDictionary *tDict = [_stampImageList objectAtIndex:index];
  269. [_deleteList addObject:tDict];
  270. [_stampImageList removeObjectAtIndex:index];
  271. if ([self saveStampDataToFile:PDFStampCustomType_Image])
  272. return YES;
  273. else
  274. return NO;
  275. }
  276. else
  277. return NO;
  278. default:
  279. return NO;
  280. }
  281. }
  282. @end