A blog mainly about Java

Automating Administrative Tasks with iText and DocuSign

The one thing I hate the most is wasting time in administrative tasks. When you have a company, deal with customers, invoice, pay taxes, sign contracts, etc. you end up spending a lot of time doing admin instead of your real work. What do you do? Well, you don’t have the choice. At the beginning it’s so scary, that you print paper, sign it with a pen, scan it, send it via email, or worth, via post, and do it again and again…. until you go “Ok, let’s automate these boring tasks with code!“.

In this blog post I show you how to generate a PDF with iText and how to send it via DocuSign so your partners can sign it electronically. All through Java code and APIs. This means you don’t have to touch MicroSoft Word to create a document nor log on into DocuSign to send it to someone to be signed.

Generating a PDF with iText

PDF has been around for a long time (1990) and iText was created in 1999 to allow developers to generate PDF files using a Java API (then, iText was ported to other languages). Today it has a dual licence, and the latest version is iText 7 (announced in May 2016 and quite different from iText 5).

Maven Dependencies

First, let’s setup our Maven dependencies. iText 7 is divided into several Maven artifacts. So it just depends on what you need. Here is what I’ve used (I could have avoid kernel as most artifacts depend on it):

Synchronize PDF while Developing

Before coding with iText, let me give you a piece of advise. One annoying thing when generating a PDF, is that it takes time to have it the way you want. So, you change your code, generate the PDF, open the file (this usually opens Adobe Reader), look at it, close the PDF, and so on. I couldn’t find any way for Acrobate Reader to automatically reload the PDF once the file changes, so I looked around and discovered Skim (there are other solutions, but this one worked for me). Skim automatically synchronizes the PDF when the file changes.

I installed it, tweaked it a bit, and set the automatic reload (open Skim -> Preferences -> Synchro). Now I’m ready to code my PDF document.

Three pages to Generate

The contract I want to generate is three pages long. As you can see in the thumbnails below, these three pages have a few images, text, lists, tables, a footer, A4 portrait, A4 landscape… All this text and graphics is doable with the iText APIs.

I won’t go through all the code that generates these 3 pages (just have a look at the GitHub repo), but I want to highlight something important: the zone where the customer needs to sign:

If you look at the iText code below, this zone is modeled as a table. Basically, it is just a 3×2 table, where the text name, date and signature is displayed on the left, and empty cells on the right. That’s all. There is no extra data to tell the customer where to sign. This is actually done with the DocuSign APIs.

What I usually do, is that I go to the DocuSign web interface, I upload a PDF, I add what they call “tags” (basically, the zones on the PDF where the customer signs), the name and email address of the person signing the document, and click send. This sends an email to the customer, he/she opens it, and signes the document automatically. When I need to send the same document for several people, I can upload a PDF that will act as a template. That saves a bit of time, but still, there’s a lot of time wasted.

I discovered recently that DocuSign has a set of APIs and even a SDK for Java. It allows me to send a document, but also to add tags to my generated PDF. So let’s see how to use DocuSign from Java code.

Creating a Developer Sandbox

The first thing you need to do to start using the DocuSign APIs is to create a developer’s sandbox (this way you are sure your documents are not legally signed while developing). Enter your name and email address, you then receive a validation email, log on and generate an integrator key.

The Integrator Key identifies your application and is also used as your client id during OAuth token requests. It looks like this:

4765b81-3558-4289-bf9d-e40977653c4

Maven Dependencies

The DocuSign SDK is quite easy in terms of dependencies, only one is needed.

Adding Tags to the PDF

I won’t go through all the Java code used to send a PDF. Check the code, I hope it is easy to read. Basically, we Base64 encode the PDF file to put it inside a DocuSign envelop, add the recipients, and send the envelop. The interesting part is how to add tags on the needed zones.

As you can see above, the DocuSign tags are the place holders that will hold the information about the customer (name) and his/her signature (date of signature, and signature itself). These tags cannot be added to the PDF itself with iText, you need the DocuSign API.

Below, the getTabFullName method sets the FullName tag at a X Y position on the PDF (on page 1). Look at the code, you will see a getTabDateSigned and a getTabSignHere methods. Based on the same logic, these two methods add the date of the signature and the signature itself.

Conclusion

Here I just gave you a simple example, but I use this technic for more complex things, such as resource bundle (so I can swap languages in the contract) or more advanced DocuSign tags. Basically, with the iText and DocuSign APIs, the sky is the limit ;o)

Administrative tasks are boring and they take a lot of our precious time. Generating PDF with code is fun, using DocuSign APIs to legally sign documents if also fun.