Skip to main content

apply

Function apply 

Source
pub fn apply<R: Resolve>(
    form: &Form,
    values: &FieldValues,
    catalog: &Dict,
    fonts: Option<&FormFonts>,
    r: &R,
    diags: &mut Diagnostics,
) -> Vec<FieldEdit>
Expand description

Turns an edit buffer into the object replacements that write it to a file.

This is the whole “fill a form and save it” step: it rewrites each edited field’s /V, sets a toggle’s widget /AS to the state chosen, and regenerates the appearance of any widget whose own /AP cannot show the new value.

A field the buffer names but the form does not have is skipped, as is one whose dictionary is inline and therefore has no reference to replace.

use pdfrum_common::{Diagnostics, Limits};
use pdfrum_doc::form::Form;
use pdfrum_object::{Array, Dict, Name, NoResolve, Object, PdfString};

let field = Dict::from_pairs([
    (Name::from("FT"), Object::Name(Name::from("Tx"))),
    (Name::from("T"), Object::Str(PdfString::literal(b"name"))),
    (Name::from("V"), Object::Str(PdfString::literal(b"Ada"))),
]);
let catalog = Dict::from_pairs([(
    Name::from("AcroForm"),
    Object::Dict(Dict::from_pairs([(
        Name::from("Fields"),
        Object::Array(Array::of([Object::Dict(field)])),
    )])),
)]);

let mut diags = Diagnostics::default();
let form = Form::load(&catalog, &NoResolve, &Limits::default(), &mut diags)
    .expect("the catalog declares an /AcroForm");
use pdfrum_doc::form::{FieldValues, apply};

let mut values = FieldValues::new();
values.set("name", "Grace");

// The field is written inline in `/Fields`, so it has no reference to
// replace and no edit is produced. `None` for the fonts draws a widget's
// chrome without laying its value out; pass `FormFonts::load`'s answer to
// get the body too.
assert!(apply(&form, &values, &catalog, None, &NoResolve, &mut diags).is_empty());