guillaume.plez | Posts: 2

Add extract anchor to a new document on an existing package

0 votes
Hey there, I have a situation where I have an existing package and I'm trying to add a new document to it with text anchor fields. The part to create the document is fine, but my issue is to add an extract anchor to it. From what I've seen, the SDK offers the option to
sdk.createDocuments(String packageId,Map documentBlobMap)
. This works fine, I'm able to add the documents to the package, but then the only way for me to add a signature to those documents, is to do it in a different call with
sdk.addSignature(packageId, docId, approval); 
This is blocking me to add the extract anchor, because based on this documentation EsignLive Document Extraction :
Text anchor extraction is available only in the API call that uploads the document. It is not available in subsequent calls.
Here is how I'm doing it :
ESignLiveSDK sdk = new ESignLiveSDK();

//Get the package
ESignLiveAPIObjects.Package_x pkg = sdk.getPackage(packageId);
//Set the status to draft
sdk.setStatus(packageId, ESignLiveAPIObjects.PackageStatus.DRAFT);
//Here I'm deleting everything that was in the package (signers, documents, approvals, fields, ...)

//And I Create a new document from a SF page PDF
Blob document = new PageReference('test').getContentAsPDF();
Map  String, Blob > blobMap = new Map  String, Blob > ();
blobMap.put('document1', document);

//Creating a field and an achor
List  ESignLiveAPIObjects.Field > fields = new List  ESignLiveAPIObjects.Field > ();
ESignLiveAPIObjects.ExtractAnchor ea1 = new ESignLiveAPIObjects.ExtractAnchor();
ea1.text = 'myText';
ea1.index = 0;
ea1.width = 180;
ea1.height = 40;
ea1.anchorPoint = ESignLiveAPIObjects.AnchorPoint.TOPLEFT;
ea1.characterIndex = 0;
ea1.leftOffset = -90;
ea1.topOffset = -45;

ESignLiveAPIObjects.Field field1 = new ESignLiveAPIObjects.Field();
field1.width = 150;
field1.height = 40;
field1.extractAnchor = ea1;
field1.type = 'SIGNATURE';
field1.subtype = 'FULLNAME';
fields.add(field1);

//Creating the role (signer )
ESignLiveAPIObjects.Role role = sdk.addRole('guilaume', 'test', '[email protected]',packageId);

//Create the approval
List  ESignLiveAPIObjects.Approval > approvals = new List  ESignLiveAPIObjects.Approval > ();
ESignLiveAPIObjects.Approval approval = new ESignLiveAPIObjects.Approval();  
approval.role = role.name;
approval.fields = fields;
approvals.add(approval); 

//My issue is starting here. At this point I need to create the document in esign, so I'm using the following method :
String addDocResponse = sdk.createDocuments(packageId, blobMap);

//And then im adding the approval to the document. This part is done after the document creation, because I
//need the document Id to add the approval to it
String addSignatureResponse = sdk.addSignature(packageId, docId, approval);
System.debug('=> Response from addSignature: ' + addSignatureResponse); 
Let me know how it would be possible to add the extract anchor on a new document in one call with the Apex SDK (without creating a new package) :) PS : I also tried to do the following :
ESignLiveAPIObjects.Document doc1 = new ESignLiveAPIObjects.Document();
doc1.name = 'document1';
doc1.approvals = new List {approval};
pkg.documents = new List {doc1};
pkg.roles = new List {role};

sdk.updatePackage(ESignLiveAPIObjects.Package_x packageToUpdate, String packageId);
String response = sdk.createDocuments(packageId,blobMap);
But this is still not taking the extract anchor fields in consideration. Thank you, Guillaume

robmtz | Posts: 12

Reply to: Add extract anchor to a new document on an existing package

0 votes
Hi Guillaume! Wanted to let you know that we're looking into this to see what options we have aside from creating a new package. I'll get back to you shortly. Thanks! Rob

robmtz | Posts: 12

Reply to: Add extract anchor to a new document on an existing package

0 votes
Hi Guillaume! Thanks for being patient. The ability to set the anchor fields when creating the document is available in the REST API, however, we don't have it in the SDK. We're going to update the SDK to have this functionality by August 22nd. In the meantime, we have this method we put together (not thoroughly tested but it's working) if you'd like to plug that in to use for now. You can add it to the ESignLiveSDK class.
public String createDocumentWithBinariesAndFields(String packageId, ESignLiveAPIObjects.Document document, Map documentMap)
    {
        Blob multipartFormData;

        String secondHeader = '--' + BOUNDARY + '\nContent-Disposition: form-data; name="payload"';

        String footer = '--' + BOUNDARY + '--';             

        String secondHeaderEncoded = ESignLiveDocumentUtilities.encodeString(secondHeader);

        List encodedStrings = ESignLiveDocumentUtilities.encodeDocuments(documentMap,BOUNDARY,'file');

        String fileNamesJSON = '';

        for(String fileName : documentMap.keySet())
        {              
            fileNamesJSON = fileNamesJSON +'{ "name":"'+fileName+'" }' + ',';
        }
        
        String headerPlusBodyEncoded = encodedStrings[0];
        String footerEncoded = encodedStrings[1];
        
        String documentJSON = JSON.serialize(document);
        documentJSON = ESignLiveJSONHelper.prepareOutboundJSON(documentJSON);        

        String documentJSONEncoded = ESignLiveDocumentUtilities.encodeContent(documentJSON);

        multipartFormData = EncodingUtil.base64Decode(headerPlusBodyEncoded+secondHeaderEncoded+documentJSONEncoded+footerEncoded);

        String packageIdResponse = helper.createDocumentsWithBinaries(packageId, multipartFormData);
        return packageIdResponse;
    }
You'll need to build the Document object that will contain your anchor fields and you'll pass this in as an additional argument. At a minimum, your document should look something like this (serialized):
{
    "approvals": [
        {
            "fields": [
                {
                    "extract": false,
                    "extractAnchor": {
                        "anchorPoint": "TOPRIGHT",
                        "characterIndex": 0,
                        "height": 28,
                        "index": 0,
                        "leftOffset": 4,
                        "text": "Signature:",
                        "topOffset": -18,
                        "width": 200
                    },
                    "left": 0,
                    "subtype": "FULLNAME",
                    "top": 0,
                    "type": "SIGNATURE"
                }
            ],
            "role": "RobMartinez"
        }
    ],
    "name": "sampleTextExtract"
}
Let me know if you have any questions. Thanks! Rob Martinez

guillaume.plez | Posts: 2

Reply to: Add extract anchor to a new document on an existing package

0 votes
Hey Rob, I updated the SDK with the changes that you guys did and it works perfectly ! Thank you very much for having this done quickly and I'm sorry for this delayed reply. Have a good one, Guillaume

robmtz | Posts: 12

Reply to: Add extract anchor to a new document on an existing package

0 votes
Hi Guillaume! So glad to hear that. Let me know if I can be of any further assistance. Thanks and have a great weekend, Rob Martinez

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