123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- //
- // StampFileManager.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 "StampFileManager.h"
- @implementation StampFileManager
- #pragma mark - Init Method
- - (id)init
- {
- self = [super init];
- if (self) {
- _deleteList = [[NSMutableArray alloc] init];
- }
-
- return self;
- }
- - (void)dealloc
- {
- [_stampTextList release];
- [_stampImageList release];
-
- //在这里删除不用的图片
- [self removeStampImage];
- [_deleteList release];
-
- [super dealloc];
- }
- - (NSString *)getDateTime
- {
- NSTimeZone* timename = [NSTimeZone systemTimeZone];
- NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init ];
- [outputFormatter setTimeZone:timename ];
-
- NSString *tDate = nil;
-
- [outputFormatter setDateFormat:@"YYYYMMddHHmmss"];
- tDate = [[outputFormatter stringFromDate:[NSDate date]] retain];
-
- [outputFormatter release];
- return tDate;
- }
- #pragma mark - File Manager
- - (void)readStampDataFromFile
- {
- [self readCustomStamp_TextStamp];
- [self readCustomStamp_ImageStamp];
- }
- - (void)readCustomStamp_TextStamp
- {
- NSFileManager *tManager = [NSFileManager defaultManager];
- if (![tManager fileExistsAtPath:kPDFStampTextList])
- {
- _stampTextList = [[NSMutableArray alloc] init];
- }
- else
- {
- if (_stampTextList)
- {
- [_stampTextList release];
- _stampTextList = nil;
- }
-
- _stampTextList = [[NSMutableArray arrayWithContentsOfFile:kPDFStampTextList] retain];
- if (_stampTextList == nil) {
- _stampTextList = [[NSMutableArray alloc] init];
- }
- }
- }
- - (void)readCustomStamp_ImageStamp
- {
- NSFileManager *tManager = [NSFileManager defaultManager];
- if (![tManager fileExistsAtPath:kPDFStampImageList])
- {
- _stampImageList = [[NSMutableArray alloc] init];
- }
- else
- {
- if (_stampImageList)
- {
- [_stampImageList release];
- _stampImageList = nil;
- }
-
- _stampImageList = [[NSMutableArray arrayWithContentsOfFile:kPDFStampImageList] retain];
- if (_stampImageList == nil) {
- _stampImageList = [[NSMutableArray alloc] init];
- }
- }
- }
- - (NSArray *)getTextStampData
- {
- return _stampTextList;
- }
- - (NSArray *)getImageStampData
- {
- return _stampImageList;
- }
- - (NSString *)saveStampWithImage:(UIImage *)image
- {
- NSFileManager *tManager = [NSFileManager defaultManager];
- NSData *imageData = UIImagePNGRepresentation(image);
-
- if (imageData == nil || [imageData length] <= 0)
- return nil;
-
- NSString *tName = [self getDateTime];
- NSString *tPath = [kPDFStampDataFolder stringByAppendingFormat:@"/%@.png",tName];
- [tName release];
-
- if ([imageData writeToFile:tPath atomically:NO]) {
- return tPath;
- }
- else
- {
- NSString *tPath_dic = kPDFStampDataFolder;
- BOOL tIsDirectory = NO;
- while (1)
- {
- if ([tManager fileExistsAtPath:tPath_dic isDirectory:&tIsDirectory]) {
- if (tIsDirectory)
- break;
- }
- else
- {
- [tManager createDirectoryAtPath:tPath_dic withIntermediateDirectories:NO attributes:nil error:nil];
- }
- tPath_dic = [tPath_dic stringByDeletingLastPathComponent];
- }
- if ([imageData writeToFile:tPath atomically:NO]) {
- return tPath;
- }
- }
-
- return nil;
- }
- - (void)removeStampImage
- {
- for (NSDictionary *tDict in _deleteList)
- {
- NSString *tPath = [tDict objectForKey:@"path"];
- NSFileManager *tFileManager = [NSFileManager defaultManager];
- [tFileManager removeItemAtPath:tPath error:nil];
- }
- }
- - (BOOL)saveStampDataToFile:(PDFStampCustomType)stampType
- {
- NSFileManager *tManager = [NSFileManager defaultManager];
-
- switch (stampType)
- {
- case PDFStampCustomType_Text:
- if ([_stampTextList writeToFile:kPDFStampTextList atomically:NO])
- {
- return YES;
- }
- else
- {
- NSString *tPath_dic = kPDFStampTextList;
- BOOL tIsDirectory = NO;
- while (1)
- {
- tPath_dic = [tPath_dic stringByDeletingLastPathComponent];
-
- if ([tManager fileExistsAtPath:tPath_dic isDirectory:&tIsDirectory]) {
- if (tIsDirectory)
- break;
- }
- else
- {
- [tManager createDirectoryAtPath:tPath_dic withIntermediateDirectories:NO attributes:nil error:nil];
- }
- }
- if ([_stampTextList writeToFile:kPDFStampTextList atomically:NO])
- {
- return YES;
- }
- }
- return NO;
-
- case PDFStampCustomType_Image:
- if ([_stampImageList writeToFile:kPDFStampImageList atomically:NO])
- {
- return YES;
- }
- else
- {
- NSString *tPath_dic = kPDFStampImageList;
- BOOL tIsDirectory = NO;
- while (1)
- {
- tPath_dic = [tPath_dic stringByDeletingLastPathComponent];
-
- if ([tManager fileExistsAtPath:tPath_dic isDirectory:&tIsDirectory]) {
- if (tIsDirectory)
- break;
- }
- else
- {
- [tManager createDirectoryAtPath:tPath_dic withIntermediateDirectories:NO attributes:nil error:nil];
- }
- }
- if ([_stampImageList writeToFile:kPDFStampImageList atomically:NO])
- {
- return YES;
- }
- }
- return NO;
-
- default:
- return NO;
- }
- }
- - (BOOL)insertStampItem:(NSDictionary *)stampItem type:(PDFStampCustomType)stampType
- {
- switch (stampType)
- {
- case PDFStampCustomType_Text:
- if (!_stampTextList) {
- [self readCustomStamp_TextStamp];
- }
-
- if (stampItem)
- {
- [_stampTextList addObject:stampItem];
- if ([self saveStampDataToFile:PDFStampCustomType_Text])
- return YES;
- else
- return NO;
- }
- else
- return NO;
-
- case PDFStampCustomType_Image:
- if (!_stampImageList) {
- [self readCustomStamp_ImageStamp];
- }
-
- if (stampItem)
- {
- [_stampImageList addObject:stampItem];
- if ([self saveStampDataToFile:PDFStampCustomType_Image])
- return YES;
- else
- return NO;
- }
- else
- return NO;
-
- default:
- return NO;
- }
- }
- - (BOOL)removeStampItem:(NSInteger)index type:(PDFStampCustomType)stampType
- {
- switch (stampType)
- {
- case PDFStampCustomType_Text:
- if (!_stampTextList) {
- [self readCustomStamp_TextStamp];
- }
-
- if (index >= 0 && index <= [_stampTextList count])
- {
- /* FIXME: 直接通过数组下角标删除元素,同时删除多个元素,
- * 会造出下传入的下角标混乱,如果是按照下角标从大往小删除
- * 不会出错 */
- [_stampTextList removeObjectAtIndex:index];
- if ([self saveStampDataToFile:PDFStampCustomType_Text])
- return YES;
- else
- return NO;
- }
- else
- return NO;
-
- case PDFStampCustomType_Image:
- if (!_stampImageList) {
- [self readCustomStamp_ImageStamp];
- }
-
- if (index >= 0 && index < [_stampImageList count])
- {
- //FiXME: 将要删除的图片放到_deleteList中,等释放这个类的时候删除图片
- if (!_deleteList) {
- _deleteList = [[NSMutableArray alloc] init];
- }
- NSDictionary *tDict = [_stampImageList objectAtIndex:index];
- [_deleteList addObject:tDict];
-
- [_stampImageList removeObjectAtIndex:index];
- if ([self saveStampDataToFile:PDFStampCustomType_Image])
- return YES;
- else
- return NO;
- }
- else
- return NO;
-
- default:
- return NO;
- }
- }
- @end
|