Skip to content

Instantly share code, notes, and snippets.

@DSDevCenter
Created February 20, 2018 03:02
Show Gist options
  • Save DSDevCenter/cdcd7e6d07b6b3f39b5b30c2b05a742b to your computer and use it in GitHub Desktop.
Save DSDevCenter/cdcd7e6d07b6b3f39b5b30c2b05a742b to your computer and use it in GitHub Desktop.
DocuSign Java SDK Example - Create Envelope with Embedded Recipient using eSignature REST API
// create a byte array that will hold our document bytes
byte[] fileBytes = null;
String pathToDocument = "[PATH/TO/DOCUMENT]";
try
{
String currentDir = System.getProperty("user.dir");
// read file from a local directory
Path path = Paths.get(currentDir + pathToDocument);
fileBytes = Files.readAllBytes(path);
}
catch (IOException ioExcp)
{
// TODO: handle error
System.out.println("Exception: " + ioExcp);
return;
}
// create an envelope that will store the document(s), field(s), and recipient(s)
EnvelopeDefinition envDef = new EnvelopeDefinition();
envDef.setEmailSubject("Please sign this document sent from Java SDK)");
// add a document to the envelope
Document doc = new Document();
String base64Doc = Base64.getEncoder().encodeToString(fileBytes);
doc.setDocumentBase64(base64Doc);
doc.setName("TestFile"); // can be different from actual file name
doc.setFileExtension(".pdf");
doc.setDocumentId("1");
List<Document> docs = new ArrayList<Document>();
docs.add(doc);
envDef.setDocuments(docs);
// add a recipient to sign the document, identified by name and email we used above
Signer signer = new Signer();
signer.setEmail("[RECIPIENT_EMAIL]");
signer.setName("[RECIPIENT_NAME]");
signer.setRecipientId("1");
// Must set |clientUserId| to embed the recipient which in turn makes it possible to
// generate a signing token (link) for them. This is a client defined string value
// which is also needed to create the recipient view URL in the next step
signer.setClientUserId("1001");
// create a |signHere| tab somewhere on the document for the signer to sign
// default unit of measurement is pixels, can be mms, cms, inches also
SignHere signHere = new SignHere();
signHere.setDocumentId("1");
signHere.setPageNumber("1");
signHere.setRecipientId("1");
signHere.setXPosition("100");
signHere.setYPosition("150");
// can have multiple tabs, so need to add to envelope as a single element list
List<SignHere> signHereTabs = new ArrayList<SignHere>();
signHereTabs.add(signHere);
Tabs tabs = new Tabs();
tabs.setSignHereTabs(signHereTabs);
signer.setTabs(tabs);
// add recipients (in this case a single signer) to the envelope
envDef.setRecipients(new Recipients());
envDef.getRecipients().setSigners(new ArrayList<Signer>());
envDef.getRecipients().getSigners().add(signer);
// send the envelope by setting |status| to "sent". To save as a draft set to "created"
envDef.setStatus("sent");
try
{
// instantiate a new EnvelopesApi object
EnvelopesApi envelopesApi = new EnvelopesApi();
// call the createEnvelope() API
EnvelopeSummary envelopeSummary = envelopesApi.createEnvelope(accountId, envDef);
System.out.println("EnvelopeSummary: " + envelopeSummary);
}
catch (com.docusign.esign.client.ApiException ex)
{
System.out.println("Exception: " + ex);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment