Search code examples
javahellosign-api

Add Image to Document/Template stored on HelloSign using JAVA


  • USECASE: I have a document stored on HELLOSIGN which is supposed to be sent to a signer after prepopulating it with some data. Additionally, I have a field in the document where in I should be able to upload the signer image from my DB.

  • What I have done:

TemplateSignatureRequest request = new TemplateSignatureRequest();
request.setTitle(title);
request.setSubject(emailSubject);
request.setMessage(message);
request.setSigner("ROLE", "<<email_id>>", name);
request.setClientId(CLIENT_ID);
request.setTemplateId(TEMPLATE_ID);
request.setTestMode(true);
request.setCustomFields(customFields);
HelloSignClient client = new HelloSignClient(API_KEY);
client.sendTemplateSignatureRequest(request);

QUESTION : Is there a way I can directly populate the image in the request object by using something like:

request.setDocuments(docs);

Or is there any other way I can achieve this?

Note: I could not mark the image part in the doc as a custom field since I could not find an option to do it on HelloSign

I am trying to replace the Picture section in the image below

I am trying to replace the Picture section in the image below


Solution

  • I reached out to [email protected] to ask them if there is any way to achieve this, and this is the response I got:

    "This is currently not available, However, We're always looking for ways to improve HelloSign API and we regularly release new versions of our products with better performance, additional features, and security enhancements. I'll reach out to our product team and pass this idea along as a feature enhancement for them to review to see if this is something we can place on our roadmap"

    So, I figured out a work around using PDF stamper

    private byte[] stampImageToDoc() throws Exception { 
    
      try {
            PdfReader pdfReader = new PdfReader(<<template_pdf_path>>);
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            PdfStamper pdfStamper = new PdfStamper(pdfReader, os);
            PdfContentByte cb = pdfStamper.getOverContent(1);
    
            File file = new File(<<imagePath>>);
            byte[] imageFile = FileUtils.readFileToByteArray(file);
    
            if (imageFile != null) {
                Image image = Image.getInstance(imageFile);
                image.scaleAbsoluteHeight(150);
                image.scaleAbsoluteWidth(150);
                image.setAbsolutePosition(29, 500); //position
                cb.addImage(image);
            }
            pdfStamper.close();
            return os.toByteArray();
        } catch (DocumentException e) {
            e.printStackTrace();
            throw e;
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        }
      }
    
    }
    

    Instead of using TemplateSignatureRequest we will be using SignatureRequest and add the stamped doc to send request::

            SignatureRequest request = new SignatureRequest();
            List<Signer> signers = new ArrayList<>();
            Signer signer = new Signer(req.getStudentEmail(), "DME");
            signers.add(signer);
            request.setTitle(title);
            request.setSubject(emailSubject);
            request.setMessage(message);
            request.setSigners(signers);
            request.setClientId(CLIENT_ID);
            request.setTestMode(true);
    
            // Image
            byte[] docBytes = stampImageToDoc();
            List<Document> docs = new ArrayList<>();
            Document d = new Document();
            File tempFile = new File(<<temporary_path>>);
            FileUtils.writeByteArrayToFile(tempFile, docBytes);
            d.setFile(tempFile);
            docs.add(d);
            request.setDocuments(docs);
    
            HelloSignClient client = new HelloSignClient(API_KEY);
            client.sendSignatureRequest(request);
    

    Note: This might not be the best solution, but its just a workaround i could think of