Accepter les PDFs avec des boutons radios sans boutons ou des champs sans zone définie (/Rect) (#75698) #155

Merged
bdauvergne merged 2 commits from wip/75698-connecteur-pdf-comparaison-rects into main 2023-03-24 15:42:51 +01:00
1 changed files with 20 additions and 4 deletions

View File

@ -38,6 +38,8 @@ class Rect(typing.NamedTuple):
@classmethod
def from_pdf_annotation(cls, annotation):
if not annotation.Rect:
raise ValueError('annotation has no rect')
return cls(*map(float, annotation.Rect))
@ -99,6 +101,7 @@ class Widget:
self.field_type == pdfrw.PdfName.Btn
and self.field_flags.is_radio
and not self.field_flags.is_push_button
and self.kids
):
return 'radio'
elif (
@ -119,14 +122,23 @@ class Widget:
@property
def rect(self):
return self.rects[0]
rects = self.rects
return self.rects[0] if rects else Rect(9999, 9999, 9999, 9999)
@property
def rects(self):
annotations = []
if self.widget_type == 'radio':
return [Rect.from_pdf_annotation(kid) for kid in self.kids_ordered_by_rect]
annotations = self.kids_ordered_by_rect
else:
return [Rect.from_pdf_annotation(self.annotation)]
annotations = [self.annotation]
rects = []
for annotation in annotations:
try:
rects.append(Rect.from_pdf_annotation(annotation))
except ValueError:
pass
return rects
@property
def digest_id(self):
@ -150,10 +162,14 @@ class Widget:
true_values.remove(pdfrw.PdfName.Off)
return true_values[0]
@property
def kids(self):
return self.annotation.Kids or []
@property
def kids_ordered_by_rect(self):
assert self.widget_type == 'radio'
kids = list(self.annotation.Kids or [])
kids = list(self.kids)
def compare(kid1, kid2):
return rect_compare(Rect.from_pdf_annotation(kid1), Rect.from_pdf_annotation(kid2))