Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Fix #30 #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions ILPDFKit/Model/PDFFormContainer.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
@interface PDFFormContainer(Private)
- (void)populateNameTreeNode:(NSMutableDictionary *)node withComponents:(NSArray *)components final:(PDFForm *)final;
- (NSArray *)formsDescendingFromTreeNode:(NSDictionary *)node;
- (void)applyAnnotationTypeLeafToForms:(PDFDictionary *)leaf parent:(PDFDictionary *)parent pageMap:(NSDictionary *)pmap;
- (void)enumerateFields:(PDFDictionary *)fieldDict pageMap:(NSDictionary *)pmap;

- (void)applyAnnotationTypeLeafToForms:(PDFDictionary *)leaf parent:(PDFDictionary *)parent pageMap:(NSDictionary *)pmap annotationsMap:(NSDictionary *)amap;

- (void)enumerateFields:(PDFDictionary *)fieldDict pageMap:(NSDictionary *)pmap annotationsMap:(NSDictionary *)amap;

- (NSArray *)allForms;
- (NSString *)formXMLForFormsWithRootNode:(NSDictionary *)node;
- (void)addForm:(PDFForm *)form;
Expand All @@ -49,11 +52,21 @@ - (instancetype)initWithParentDocument:(PDFDocument *)parent {
_nameTree = [[NSMutableDictionary alloc] init];
_document = parent;
NSMutableDictionary *pmap = [NSMutableDictionary dictionary];
for (PDFPage *page in _document.pages) {
NSMutableDictionary *amap = [NSMutableDictionary dictionary];
for (PDFPage *page in _document.pages)
{
PDFArray* annotations = page.dictionary[@"Annots"];
for (PDFDictionary* annotation in annotations)
{
amap[@((NSUInteger)(annotation.dict))] = @(page.pageNumber);
}

pmap[@((NSUInteger)(page.dictionary.dict))] = @(page.pageNumber);
}
for (PDFDictionary *field in _document.catalog[@"AcroForm"][@"Fields"]) {
[self enumerateFields:field pageMap:pmap];

for (PDFDictionary *field in _document.catalog[@"AcroForm"][@"Fields"])
{
[self enumerateFields:field pageMap:pmap annotationsMap:amap];
}
}
return self;
Expand Down Expand Up @@ -113,23 +126,30 @@ - (void)removeForm:(PDFForm *)form {

#pragma mark - Private

- (void)enumerateFields:(PDFDictionary *)fieldDict pageMap:(NSDictionary *)pmap {
- (void)enumerateFields:(PDFDictionary *)fieldDict pageMap:(NSDictionary *)pmap annotationsMap:(NSDictionary *)amap{
if (fieldDict[@"Subtype"]) {
PDFDictionary *parent = fieldDict.parent;
[self applyAnnotationTypeLeafToForms:fieldDict parent:parent pageMap:pmap];
[self applyAnnotationTypeLeafToForms:fieldDict parent:parent pageMap:pmap annotationsMap:amap];
} else {
for (PDFDictionary *innerFieldDictionary in fieldDict[@"Kids"]) {
PDFDictionary *parent = innerFieldDictionary.parent;
if (parent != nil) [self enumerateFields:innerFieldDictionary pageMap:pmap];
else [self applyAnnotationTypeLeafToForms:innerFieldDictionary parent:fieldDict pageMap:pmap];
if (parent != nil) [self enumerateFields:innerFieldDictionary pageMap:pmap annotationsMap:amap];
else [self applyAnnotationTypeLeafToForms:innerFieldDictionary parent:fieldDict pageMap:pmap annotationsMap:amap];
}
}
}

- (void)applyAnnotationTypeLeafToForms:(PDFDictionary *)leaf parent:(PDFDictionary *)parent pageMap:(NSDictionary *)pmap {
- (void)applyAnnotationTypeLeafToForms:(PDFDictionary *)leaf
parent:(PDFDictionary *)parent
pageMap:(NSDictionary *)pmap
annotationsMap:(NSDictionary *)amap
{
NSUInteger targ = (NSUInteger)(((PDFDictionary *)(leaf[@"P"])).dict);
NSUInteger aarg = (NSUInteger)(PDFDictionary *)leaf.dict;
leaf.parent = parent;
NSUInteger index = targ ? ([pmap[@(targ)] unsignedIntegerValue] - 1):0;


NSUInteger index = targ ? ([pmap[@(targ)] unsignedIntegerValue] - 1):([amap[@(aarg)] unsignedIntegerValue] - 1);
PDFForm *form = [[PDFForm alloc] initWithFieldDictionary:leaf page:_document.pages[index] parent:self];
[self addForm:form];
}
Expand Down