Thursday, April 13, 2017

Display a PDF, allow user to sign it, and then send the signed document to a database

1 comment

I am trying to figure out how to allow users to sign a pdf that will be rendered with the PdfRenderer class in Android. Basically it will display a form that will need to be signed and dated and then submitted. I can handle showing the pdf with the PdfRenderer class but I am not seeing a decent way to allow the user to sign and date that rendered pdf and then save the signed pdf to a database. Is there a way to do this without using a third party? And if not is there a free third party product out there?

3 Answers

Answers 1

Well, I really hope there is some solution without the use of any third party library. But, I had a similar requirement of signing the Pdf(i.e annotating the pdf in my case) where I used iText. It is licensed as AGPL software

you can add itext to your android project by adding jar or the following to the build.gradle file :-

dependencies {     compile 'com.itextpdf:itextg:5.5.11' //Or the latest variant  } 

And the java code would be :-

Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("ThisIsYourPdfFile.pdf")); document.open(); Image img = Image.getInstance("GetTheSignatureInAnImageFormat.png"); document.add(new Paragraph("Sample 1: This is where you can add text stuff say you want to add date")); document.add(img); document.close(); 

See the imports if you are confused

import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Image; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; 

Now,coming to your last part of question. Saving them in database. Now your Pdf is just a file. Which is nothing but a bunch of bytes. So you can read the file into byte using nio as below or

From Java7

import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.Files;  Path pdfPath = Paths.get("/path/to/file.pdf"); byte[] pdf = Files.readAllBytes(pdfPath); 

You can do a simple search for converting file to bytes array to save in database. There are alot of ways.

And you can write this to SQLite as Blob object.

Hope this helps.

Answers 2

This is just a rough guess how other applications are doing it as I have not done it myself.

  1. There are three ways of getting the signature
    • Take a signature picture (should be easy)
    • Load signature from file/url (another easy one)
    • Signature handwritten by the user (a bit more complex, but there are libs already doing it, check for example this lib)
  2. Once you have the image of the signature use some PDF editing lib (PDF Clown for example) to place the image to the exact position where the signature should be. You might also need to resize the image accordingly, but I guess it should not be the problem.
  3. Use HttpClient HttpUrlConnection or OKHttp to send the final (edited) document (as attachment) to the HTTP server, which will either store the file in the file system and associate the path with some kind of document identifier or store the file directly into the database as a BLOB.

I have not used any of those libs to claim if they work properly or not, but I leave it for you to find out :P.

Answers 3

To get it done You need to have a HTMl developed similar to your PDF then capture signature inside a popup on HTML5 canvas using sketch.js and that will give you an image which you will render inside the signature box of your HTML and then finally that HTML you will convert to PDF using EVO HTML to PDF Converter http://www.evopdf.com/?gclid=CMDxuua-odMCFYQYGwodKpsA-w . This is a paid service once you bring the license you can integrate it in a simple way. That is how I did it :)

If You Enjoyed This, Take 5 Seconds To Share It

1 comment:

  1. I have tried zetpdf.com to generate a PDF file in c#. It was really helpful and easy. 

    ReplyDelete