Skip to content

Commit

Permalink
load updates file and effect its changes on links #141
Browse files Browse the repository at this point in the history
  • Loading branch information
AlirezaAlgo committed Mar 29, 2015
1 parent f52fa54 commit 324f514
Showing 1 changed file with 108 additions and 25 deletions.
133 changes: 108 additions & 25 deletions Classes/WAPDFParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,27 @@ -(UIImage*) drawTileForPage:(int)page withTileRect:(CGRect)tileRect withImageRec
#pragma mark -
#pragma mark Links handling

- (NSDictionary*)getLinksUpdateOnPageByIndexAtPage:(int)pageNumber inLinksUpdate:(NSDictionary*)linksUpdate
{
NSArray *links;
if(!linksUpdate || (links = [linksUpdate objectForKey:[NSString stringWithFormat:@"p%d", pageNumber]]) == nil)
return nil;
NSMutableDictionary *mret = [[[NSMutableDictionary alloc] init] retain];
for(NSDictionary *link in links)
{
id index = [link objectForKey:@"IndexAtPage"];
if(index != nil)
[mret setObject:link forKey:[NSString stringWithFormat:@"%@", index]];
}
NSDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:mret];
[mret release];
return dict;
}

- (NSArray*)getLinksOnPage:(int)page{
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL(pdfURL);
NSString *updatesPath = [[NSBundle mainBundle] pathOfFileWithUrl:[[urlString noArgsPartOfUrlString] urlByChangingExtensionOfUrlStringToSuffix:@"_updates.plist"]];
NSDictionary *linksUpdate = updatesPath ? [NSDictionary dictionaryWithContentsOfFile:updatesPath] : nil;
NSMutableArray *tempArray= [NSMutableArray array];
int min= page;
int max=page;
Expand All @@ -314,15 +333,21 @@ - (NSArray*)getLinksOnPage:(int)page{
CGPDFArrayRef annotsArray;
if (!CGPDFDictionaryGetArray(pageDict, "Annots", &annotsArray)) continue;
size_t annotsCount = CGPDFArrayGetCount(annotsArray);

// update links at page `j`
NSDictionary *linksUpdateById = [self getLinksUpdateOnPageByIndexAtPage:j inLinksUpdate:linksUpdate];

for (size_t i = 0; i < annotsCount ; i++)
{
NSDictionary * tempDic;
NSString * tempUrl = nil;
CGPDFDictionaryRef annotDict;
if (!CGPDFArrayGetDictionary(annotsArray, i, &annotDict)) continue;
CGRect linkRect = [self getAnnotationRect:annotDict inPageRef:pageRef];
NSValue *rectValue = [NSValue valueWithCGRect:linkRect];



//Check what type of annotation this is
const char * subType = nil;
if (!CGPDFDictionaryGetName(annotDict, "Subtype", &subType )) continue;
Expand All @@ -332,24 +357,50 @@ - (NSArray*)getLinksOnPage:(int)page{
CGPDFDictionaryRef aLink;
if (CGPDFDictionaryGetDictionary(annotDict, "A", &aLink))
{
NSString * tempString = [self getLinkFromActionDictionary:aLink];
//SLog(@"Link:%@",tempString);
tempDic = [NSDictionary dictionaryWithObjectsAndKeys:rectValue,@"Rect",tempString,@"URL",nil];
[tempArray addObject:tempDic];

tempUrl = [self getLinkFromActionDictionary:aLink];
}
else {
CGPDFArrayRef anotherArray;
if (CGPDFDictionaryGetArray(annotDict, "Dest", &anotherArray))
{
NSString * tempString = [self getLinkFromDestArray:anotherArray];
tempDic = [NSDictionary dictionaryWithObjectsAndKeys:rectValue,@"Rect",tempString,@"URL",nil];
[tempArray addObject:tempDic];


tempUrl = [self getLinkFromDestArray:anotherArray];
}
}

NSDictionary *updateLink = linksUpdateById ? [linksUpdateById objectForKey:[NSString stringWithFormat:@"%d", (int)i]] : nil;
// override data if updateLink exists
if(updateLink != nil)
{
NSString *v = [updateLink objectForKey:@"Action"];
if([v isKindOfClass:[NSString class]])
{
if([v isEqualToString:@"Remove"])
continue;
else if([v isKindOfClass:[NSString class]] && [v isEqualToString:@"Edit"])
{
v = [updateLink objectForKey:@"Rect"];
if([v isKindOfClass:[NSString class]])
{
CGFloat rectArr[4], *fp = rectArr;
for(NSString *str in [v componentsSeparatedByString:@" "])
*(fp++) = [str integerValue];
if(fp - rectArr == 4)
{
rectValue = [NSValue valueWithCGRect:[self CGRectFromPDFRect:rectArr inPageRef:pageRef]];
}
}
v = [updateLink objectForKey:@"Link"];
if([v isKindOfClass:[NSString class]])
tempUrl = v;
}
}
}

if(tempUrl != nil)
{
tempDic = [NSDictionary dictionaryWithObjectsAndKeys:rectValue,@"Rect",tempUrl,@"URL",nil];
[tempArray addObject:tempDic];
}

}
else if (!strcmp(subType, "RichMedia")) //Annotation is a rich media
Expand Down Expand Up @@ -427,6 +478,41 @@ - (NSArray*)getLinksOnPage:(int)page{

}

// add update links
NSArray *links = linksUpdate ? [linksUpdate objectForKey:[NSString stringWithFormat:@"p%d", (int)j]] : nil;
if([links isKindOfClass:[NSArray class]])
{
for(NSDictionary *link in links)
{
NSValue *rectValue = nil;
NSString *tempUrl = nil;
NSString *v = [link objectForKey:@"Action"];
if([v isKindOfClass:[NSString class]] && [v isEqualToString:@"Add"])
{
v = [link objectForKey:@"Rect"];
if([v isKindOfClass:[NSString class]])
{
CGFloat rectArr[4], *fp = rectArr;
for(NSString *str in [v componentsSeparatedByString:@" "])
*(fp++) = [str integerValue];
if(fp - rectArr == 4)
{
rectValue = [NSValue valueWithCGRect:[self CGRectFromPDFRect:rectArr inPageRef:pageRef]];
}
}
v = [link objectForKey:@"Link"];
if([v isKindOfClass:[NSString class]])
tempUrl = v;


if(tempUrl != nil && rectValue != nil)
{
NSDictionary *tempDic = [NSDictionary dictionaryWithObjectsAndKeys:rectValue,@"Rect",tempUrl,@"URL",nil];
[tempArray addObject:tempDic];
}
}
}
}
}


Expand All @@ -435,7 +521,6 @@ - (NSArray*)getLinksOnPage:(int)page{
//CGPDFPageRelease(pageRef);//This crashes the app
CGPDFDocumentRelease(pdf);


return tempArray;


Expand Down Expand Up @@ -518,10 +603,19 @@ - (NSString*) getLinkFromDestArray:(CGPDFArrayRef)destArray{

}

- (CGRect)CGRectFromPDFRect:(CGFloat*)linkPoints inPageRef:(CGPDFPageRef)pageRef{
CGRect rect;
CGRect documentRect = CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox);
rect.origin.x = linkPoints[0]- documentRect.origin.x;
rect.origin.y = documentRect.size.height+documentRect.origin.y - linkPoints[3];
rect.size.width = linkPoints[2] - linkPoints[0];
rect.size.height = linkPoints[3] - linkPoints[1];
return rect;
}

- (CGRect) getAnnotationRect:(CGPDFDictionaryRef) annotDict inPageRef:(CGPDFPageRef)pageRef{
CGFloat linkPoints[4] = {0.0,0.0,0.0,0.0};
CGPDFArrayRef anArray;
CGRect linkrect;
if (CGPDFDictionaryGetArray(annotDict, "Rect", &anArray))
{
for (size_t j = 0; j < 4; j++)
Expand All @@ -533,20 +627,9 @@ - (CGRect) getAnnotationRect:(CGPDFDictionaryRef) annotDict inPageRef:(CGPDFPage

}


CGRect documentRect = CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox);
//SLog(@"documentRect: %f, %f, %f, %f", documentRect.origin.x,documentRect.origin.y,documentRect.size.width,documentRect.size.height);
//We need to make some conversions in order to match coordinate systems.
linkrect.origin.x = linkPoints[0]- documentRect.origin.x;
linkrect.origin.y = documentRect.size.height+documentRect.origin.y - linkPoints[3];
linkrect.size.width = linkPoints[2] - linkPoints[0];
linkrect.size.height = linkPoints[3] - linkPoints[1];
//SLog(@"Annot Rect: %f, %f, %f, %f", linkrect.origin.x,linkrect.origin.y,linkrect.size.width,linkrect.size.height);

return linkrect;
return [self CGRectFromPDFRect:linkPoints inPageRef:pageRef];
}


CGRect linkrect;
return linkrect;


Expand Down

0 comments on commit 324f514

Please sign in to comment.