utils.pdf: simplify PDF dict accesses (#75089)
This commit is contained in:
parent
25344cbe88
commit
132508a15d
|
@ -55,11 +55,11 @@ class Widget:
|
|||
@property
|
||||
def value(self):
|
||||
if self.widget_type == 'text':
|
||||
if self.annotation[pdfrw.PdfName.V]:
|
||||
return self.annotation[pdfrw.PdfName.V].decode()
|
||||
if self.annotation.V:
|
||||
return self.annotation.V.decode()
|
||||
return ''
|
||||
elif self.widget_type == 'checkbox':
|
||||
return self.annotation[pdfrw.PdfName.V] == self.on_value
|
||||
return self.annotation.V == self.on_value
|
||||
|
||||
def set(self, value):
|
||||
# allow rendering of values in Acrobat Reader
|
||||
|
@ -86,20 +86,20 @@ class Page:
|
|||
@property
|
||||
def fields(self):
|
||||
fields = []
|
||||
for annotation in self.page[pdfrw.PdfName.Annots] or ():
|
||||
if annotation[pdfrw.PdfName.Subtype] != pdfrw.PdfName.Widget:
|
||||
for annotation in self.page.Annots or ():
|
||||
if annotation.Subtype != pdfrw.PdfName.Widget:
|
||||
continue
|
||||
if not annotation[pdfrw.PdfName.T]:
|
||||
if not annotation.T:
|
||||
continue
|
||||
name = annotation[pdfrw.PdfName.T].decode()
|
||||
parent = annotation[pdfrw.PdfName.Parent]
|
||||
while parent and parent[pdfrw.PdfName.T]:
|
||||
name = f'{parent[pdfrw.PdfName.T].decode()}.{name}'
|
||||
parent = parent[pdfrw.PdfName.Parent]
|
||||
if not annotation[pdfrw.PdfName.FT]:
|
||||
name = annotation.T.decode()
|
||||
parent = annotation.Parent
|
||||
while parent and parent.T:
|
||||
name = f'{parent.T.decode()}.{name}'
|
||||
parent = parent.Parent
|
||||
if not annotation.FT:
|
||||
continue
|
||||
pdf_field_type = annotation[pdfrw.PdfName.FT]
|
||||
pdf_field_flags = annotation[pdfrw.PdfName.Ff] or 0
|
||||
pdf_field_type = annotation.FT
|
||||
pdf_field_flags = annotation.Ff or 0
|
||||
RADIO_FLAG = 2**16
|
||||
PUSH_BUTTON_FLAG = 2**17
|
||||
if (
|
||||
|
@ -115,7 +115,7 @@ class Page:
|
|||
on_value = None
|
||||
if widget_type == 'checkbox':
|
||||
try:
|
||||
on_values = list(annotation[pdfrw.PdfName.AP][pdfrw.PdfName.N].keys())
|
||||
on_values = list(annotation.AP.N.keys())
|
||||
except KeyError:
|
||||
on_value = pdfrw.PdfName.On
|
||||
else:
|
||||
|
@ -126,7 +126,7 @@ class Page:
|
|||
Widget(
|
||||
name=name,
|
||||
widget_type=widget_type,
|
||||
rect=Rect(*map(float, annotation[pdfrw.PdfName.Rect])),
|
||||
rect=Rect(*map(float, annotation.Rect)),
|
||||
on_value=on_value,
|
||||
page=self,
|
||||
annotation=annotation,
|
||||
|
@ -137,7 +137,7 @@ class Page:
|
|||
|
||||
@property
|
||||
def media_box(self):
|
||||
return Rect(*map(float, self.page[pdfrw.PdfName.MediaBox]))
|
||||
return Rect(*map(float, self.page.MediaBox))
|
||||
|
||||
def thumbnail_png(self, width=None):
|
||||
width = width or self.THUMBNAIL_DEFAULT_WIDTH
|
||||
|
|
Loading…
Reference in New Issue