Git Product home page Git Product logo

acro-form-filler's Introduction

dotnet package

Acro Form Evaluator

The package is designed to fill the fields in PDF document in the same way as the biding of the fields in WPF form. The description of fields is stored in the XML file with following structure (example of W4 form from IRS site):

<form pdfFile='fw4.pdf'>
  <dataFields>
    <topmostSubform>
      <Page1>
        <Step1a>
          <f1_01 expr='FirstName + MiddleName'/>
          <f1_02 expr='Ssn'/>
          <f1_03 expr='Address.Street'/>
          <f1_04 expr='Address.City + " " + Address.State + ","+Address.Zip'/>
        </Step1a>
        <f1_05 />
        <c1_1 expr='Status==Statuses.Single || Status==Statuses.MarriedFillingSeparately' onValue='1' />
        <c1_1 expr='Status==Statuses.MarriedFillingJointly' onValue='2' />
        <c1_1 expr='Status==Statuses.HeadOfHousehold' onValue='3' />
      </Page1>
    </topmostSubform>
  </dataFields>
</form>

The above file is generated by using the FieldsDefExtractor utility (may be found in the samples folder), or you can get it by using the Acrobat Reader (Edit->Form Options->Export Data; some editing will be required). All you need to do is set an expression for each field based on the properties of your model. For the checkbox fields, you need to set the Boolean expression when a checkbox is on and the value for the checkbox when it is checked.

The code to fill the PDF is straightforward:

 // read the form definition
 var formDef = PdfFormDefinition.LoadFrom("fw4.xml");
 // load model
 var employee = LoadEmployeeFromDb();

 var evaluator = new AcroFieldsEvaluator();
 //it returns the collection or KeyValuePair, where Key is the field name, Value is the field's value
 var pdfFormFieldValues = evaluator.CalcPdfFields(formDef.PdfFielsdDef, employee);
 // update PDF form using iTextSharp
 var pdfReader = new PdfReader("fw4.pdf");
 using (var outputPdf = new FileStream("fw4-result.pdf", FileMode.Create))
 {
     var pdfStamper = new PdfStamper(pdfReader, outputPdf);
     var pdfFormFields = pdfStamper.AcroFields;
     // set values for the fields
     foreach (var formField in pdfFormFieldValues)
     {
         pdfFormFields.SetField(formField.Key, formField.Value);
     }
     pdfStamper.FormFlattening = true;
     pdfStamper.Close();
 }

The package uses NReco LambdaParser for expression evaluation. The samples folder contains a simple WebApi to fill IRS 1040 form via web requests.

XML Fields Defenition

<Table_Dependents condition='Dependents != null'>
  <BodyRow1 condition='Dependents.Count > 0' var-name='dependent' var-expr='Dependents[0]'>
    <f1_16 expr='dependent.Person.FirstName+" "+dependent.Person.LastName'/>
    <f1_17 expr='dependent.Person.Ssn'/>
    <f1_18 expr='dependent.Relation'/>
    <c1_13 expr='dependent.ChildTaxCredit'/>
    <c1_14 expr='dependent.CreditForOtherDependents'/>
  </BodyRow1>
  <BodyRow2 condition='Dependents.Count > 1' var-name='dependent' var-expr='Dependents[1]'>
    <f1_19 expr='dependent.Person.FirstName+" "+dependent.Person.LastName'/>
    <f1_20 expr='dependent.Person.Ssn'/>
    <f1_21 expr='dependent.Relation'/>
    <c1_15 expr='dependent.ChildTaxCredit'/>
    <c1_16 expr='dependent.CreditForOtherDependents'/>
  </BodyRow2>
 </Table_Dependents>

Data group

The data group has the following attributes:

  • condition – an expression to determine if the group should be eavluated; if the condition is false, all data groups and fields inside this group are not calculated;
  • var-name – a variable defined for this group. This variable may be used in excpressions for inner groups/fields
  • var-variable – an expression to for group’s variable;

Field

The field has the following attributes:

  • condition – an expression to determine if the field has a value; if the condition is false, the field’s value is not calculated;
  • expr – an expression used to calculate field’s value;
  • displayFormat – representation of field’s value. It should be in {0:FORMAT} format; it’s used as a parameter in string.Format(DisplayFormat, value) call
  • onValue – used only for checkboxes to set the value of the checked box

License

Copyright 2020 Alexander Vologin Distributed under the MIT license

acro-form-filler's People

Contributors

alecvolo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.