importXfdf method

Future<int> importXfdf(
  1. String xml
)

Implementation

Future<int> importXfdf(String xml) async {
  final List<UPdfFormField> fields = await formFields();
  final Map<String, String> values = <String, String>{};
  for (final RegExpMatch match in RegExp('<field[^>]*name="([^"]*)"[^>]*>(.*?)</field>', dotAll: true).allMatches(xml)) {
    final String name = match.group(1) ?? "";
    final String body = match.group(2) ?? "";
    final RegExpMatch? value = RegExp("<value>(.*?)</value>", dotAll: true).firstMatch(body);
    if (name.isEmpty) continue;
    values[name] = (value?.group(1) ?? "").replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("&amp;", "&");
  }
  int applied = 0;
  for (final UPdfFormField field in fields) {
    final String? value = values[field.name];
    if (value == null) continue;
    if (await setFieldValue(field, field.kind == UDocFieldKind.checkBox || field.kind == UDocFieldKind.radioButton ? value != "Off" && value.isNotEmpty : value)) applied++;
  }
  return applied;
}