rumfords

Dynamic Field Extraction

0 votes

Hi,

With the standard PackageBuilder/DocumentBuilder coding, as per the example below, every specific field needs to be included in the single PackageBuilder statement.  With this, every document type must be coded separately and the statements are often very long in nature.

            DocumentPackage dp = PackageBuilder.NewPackageNamed(packageName)
                .WithEmailMessage(emailMsg)
                .WithSenderInfo(SenderInfoBuilder.NewSenderInfo(senderEmail))
                .WithSigner(SignerBuilder.NewSignerWithEmail(member.EmailAddress)
                    .WithFirstName(member.FirstName)
                    .WithLastName(member.LastName)
                    .WithTitle(member.Title)
                    .WithCompany(member.Company)
                    .WithCustomId("Signer1")
                )
                .WithDocument(DocumentBuilder.NewDocumentNamed(documentName)
                    .FromStream(new MemoryStream(bytePDF), DocumentType.PDF)
                    .EnableExtraction()
                    .WithSignature(SignatureBuilder.SignatureFor(member.EmailAddress)
                        .WithName("txtSignature")
                        .WithPositionExtracted()
                        .WithField(FieldBuilder.SignatureDate()
                            .WithId("txtSignatureDate")
                            .WithPositionExtracted()
                            .WithName("txtSignatureDate")
                            .WithFontSize(defFontSize)
                            )
                        .WithField(FieldBuilder.Label()
                            .WithId("txtFirstName")
                            .WithPositionExtracted()
                            .WithName("txtFirstName")
                            .WithValue(member.FirstName)
                            .WithFontSize(defFontSize)
                        )
                        .WithField(FieldBuilder.Label()
                            .WithId("txtLastName")
                            .WithPositionExtracted()
                            .WithName("txtLastName")
                            .WithValue(member.LastName)
                            .WithFontSize(defFontSize)
                        )
                        .WithField(FieldBuilder.TextField()
                            .WithId("txtCompanyName")
                            .WithPositionExtracted()
                            .WithName("txtCompanyName")
                            .WithFontSize(defFontSize)
                        )
                        .WithField(FieldBuilder.RadioButton("Group1")
                            .WithId("txtLocationOffice")
                            .WithPositionExtracted()
                            .WithName("txtLocationOffice")
                            .WithFontSize(defFontSize)
                        )
                        .WithField(FieldBuilder.RadioButton("Group1")
                            .WithId("txtLocationHome")
                            .WithPositionExtracted()
                            .WithName("txtLocationHome")
                            .WithFontSize(defFontSize)
                        )
                        .WithField(FieldBuilder.TextField()
                            .WithId("txtPctAvailable")
                            .WithPositionExtracted()
                            .WithName("txtPctAvailable")
                            .WithValidation(pctValidator)
                            .WithFontSize(defFontSize)
                        )
                    )
                )
                .Build();

Is there any method of making this more dynamic so that the extracted fields names, types, groups, validations, etc. can be passed into a single "field builder" routine and added to the document for a signature more dynamically?

E.g. A method along the lines:
    AddField(Document document, Signature signature, String fieldID, String fieldName, String groupName, Validator validator, int fontSize....)
    {
        //Build the field based on the input parameters and add it to the document
    }

The ability to do this could lead to using another PDF tool for pulling field information out of an Adobe document, passing into this process to build the OneSpan package and document, without the code knowing anything about the fields involved.  This could also reduce lines of code required to create multiple package types.

Thanks


Reply to: Dynamic Field Extraction

0 votes

Hi rumfords,

 

Codewise, you don't always have to add fields with a chain of builder, but a similar way to below code snippet. On top of that, not sure if your PDF library could tell the form type (PDF form vs PDF radio button, etc), you may also want to design a naming convention so that you can dynamically extract the field type and other relative data like label value, radio group id, or the text field validation pattern.

 

        public void Execute() {
     
            EslClient eslClient = new EslClient(apiKey, apiUrl);

            PackageBuilder pb = PackageBuilder.NewPackageNamed("packageName")
            .WithEmailMessage("emailMsg")
            .WithSenderInfo(SenderInfoBuilder.NewSenderInfo("senderEmail"))
            .WithSigner(SignerBuilder.NewSignerWithEmail("member.EmailAddress")
                .WithFirstName("member.FirstName")
                .WithLastName("member.LastName")
                .WithTitle("member.Title")
                .WithCompany("member.Company")
                .WithCustomId("Signer1")
            );

            DocumentBuilder db = DocumentBuilder.NewDocumentNamed("documentName")
                .FromFile(filePath)
                .EnableExtraction();
            SignatureBuilder sb = SignatureBuilder.SignatureFor("member.EmailAddress")
                    .WithName("txtSignature")
                    .WithPositionExtracted();
 

            sb.WithField(AddField("SignatureDate", "txtSignatureDate", null, null, null, 12));
            sb.WithField(AddField("Label", "txtFirstName", "member.FirstName", null, null, 12));
            sb.WithField(AddField("Label", "txtLastName", "member.LastName", null, null, 12));
            sb.WithField(AddField("TextField", "txtCompanyName", null, null, null, 12));
            sb.WithField(AddField("RadioButton", "txtLocationOffice", null, "Group1", null, 12));
            sb.WithField(AddField("RadioButton", "txtLocationHome", null, "Group1", null, 12));
            sb.WithField(AddField("TextField", "txtPctAvailable", null, null, FieldValidatorBuilder.Basic().MaxLength(100).Build(), 12));

            db.WithSignature(sb);
            pb.WithDocument(db);
            eslClient.CreatePackageOneStep(pb.Build());

        }

        private Field AddField(string fieldType, string fieldID, string fieldValue, string groupName, FieldValidator validator, int fontSize)
        {

            switch (fieldType)
            {
                case "TextField":
                    return FieldBuilder.TextField().WithPositionExtracted().WithId(fieldID).WithName(fieldID).WithValidation(validator).WithFontSize(fontSize).Build();
                case "Label":
                    return FieldBuilder.Label().WithPositionExtracted().WithId(fieldID).WithName(fieldID).WithValue(fieldValue).Build();
                case "RadioButton":
                    return FieldBuilder.RadioButton(groupName).WithPositionExtracted().WithId(fieldID).WithName(fieldID).Build();
                case "SignatureDate":
                    return FieldBuilder.SignatureDate().WithPositionExtracted().WithId(fieldID).WithName(fieldID).Build();
                default:
                    return null;
            }
        }

 

Duo

Duo Liang OneSpan Evangelism and Partner Integrations Developer


Reply to: Dynamic Field Extraction

0 votes

Thank-you.  This is exactly what I was looking for.


Hello! Looks like you're enjoying the discussion, but haven't signed up for an account.

When you create an account, we remember exactly what you've read, so you always come right back where you left off