From 356faf31df23e922c041869c0115dac4c022548d Mon Sep 17 00:00:00 2001 From: Adrian Perez de Castro Date: Thu, 26 Sep 2013 18:46:04 +0300 Subject: [PATCH v17 3/5] glib: Accessors for form fields of structure elements Implements two methods which can be used on PopplerStructureElement objects of type POPPLER_STRUCTURE_ELEMENT_FORM: - poppler_structure_element_get_form_field() - poppler_structure_element_get_form_field_mapping() The only difference is that the later returns a PopplerFormFieldMapping, which itself contains the PopplerFormField, plus the rectangle covered by the form widget in the page. --- glib/poppler-structure-element.cc | 65 +++++++++++++++++++++++++++++++++++++++ glib/poppler-structure-element.h | 2 ++ 2 files changed, 67 insertions(+) diff --git a/glib/poppler-structure-element.cc b/glib/poppler-structure-element.cc index 60bb6c2..f9e0b74 100644 --- a/glib/poppler-structure-element.cc +++ b/glib/poppler-structure-element.cc @@ -764,6 +764,71 @@ text_span_poppler_text_span (const TextSpan& span) } /** + * poppler_structure_element_get_form_field: + * @poppler_structure_element: A #PopplerStructureElement + * + * Return value: (transfer full): A #PopplerFormField, or %NULL if + * the element is not a %POPPLER_STRUCTURE_ELEMENT_FORM. + */ +PopplerFormField * +poppler_structure_element_get_form_field (PopplerStructureElement *poppler_structure_element) +{ + g_return_val_if_fail (POPPLER_IS_STRUCTURE_ELEMENT (poppler_structure_element), NULL); + g_return_val_if_fail (poppler_structure_element->elem != NULL, NULL); + + if (poppler_structure_element->elem->getType () != StructElement::Form) + return NULL; + + // TODO Handle elements which have a Role attribute (used sometimes for + // non-editable widgets, to describe their appearance). Editable + // fields have only a single child, with the field identifier. + if (poppler_structure_element->elem->getNumElements () != 1) + return NULL; + + gint field_id = -1; + const StructElement *child = poppler_structure_element->elem->getElement (0); + if (child->isContent ()) + { + if (child->isObjectRef ()) + { + // TODO Handle this case -- I have yet to see a PDF using this. + } + else + { + // Element contains the form field ID as the MCID attribute. + field_id = child->getMCID (); + } + } + + if (field_id < 0) + return NULL; + + return (field_id < 0) ? NULL : poppler_document_get_form_field (poppler_structure_element->document, + field_id); +} + +/** + * poppler_structure_element_get_form_field_mapping: + * @poppler_structure_element: a #PopplerStructureElement + * + * Return value: (transfer full): A #PopplerFormFieldMapping, or %NULL if + * the element is not a %POPPLER_STRUCTURE_ELEMENT_FORM + */ +PopplerFormFieldMapping * +poppler_structure_element_get_form_field_mapping (PopplerStructureElement *poppler_structure_element) +{ + g_return_val_if_fail (POPPLER_IS_STRUCTURE_ELEMENT (poppler_structure_element), NULL); + g_return_val_if_fail (poppler_structure_element->elem != NULL, NULL); + + PopplerFormField *field = poppler_structure_element_get_form_field (poppler_structure_element); + if (field == NULL) + return NULL; + + gint page_num = poppler_structure_element_get_page (poppler_structure_element); + return _poppler_form_field_mapping_new_from_form_field (field, page_num); +} + +/** * poppler_text_span_copy: * @poppler_text_span: a #PopplerTextSpan * diff --git a/glib/poppler-structure-element.h b/glib/poppler-structure-element.h index 249db58..975a0e2 100644 --- a/glib/poppler-structure-element.h +++ b/glib/poppler-structure-element.h @@ -103,6 +103,8 @@ gchar *poppler_structure_element_get_alt_text gchar *poppler_structure_element_get_actual_text (PopplerStructureElement *poppler_structure_element); PopplerTextSpan **poppler_structure_element_get_text_spans (PopplerStructureElement *poppler_structure_element, guint *n_text_spans); +PopplerFormField *poppler_structure_element_get_form_field (PopplerStructureElement *poppler_structure_element); +PopplerFormFieldMapping *poppler_structure_element_get_form_field_mapping (PopplerStructureElement *poppler_structure_element); #define POPPLER_TYPE_STRUCTURE_ELEMENT_ITER (poppler_structure_element_iter_get_type ()) GType poppler_structure_element_iter_get_type (void) G_GNUC_CONST; -- 1.9.0