Search code examples
javapdfdigital-signatureitext7sequential

How to add signature form to an existing pdf (using iText7), so that the output file can be served as a input to pdf (sequential signature)?


I am using iText7 for pdf signature workflow, i followed the samples provided with i7js-signatures. However my requirement is to take an input pdf file, add sequential signatures to it, and further pass it for signature.

i tried splitting up the process in two steps.

  1. Take input pdf and add sequential signature panel, in the intermediate_output file.

    public void createForm() throws IOException {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(FORM),new PdfWriter(TMP));
        //PdfDocument pdfDoc = new PdfDocument(new PdfWriter(FORM));
        pdfDoc.addNewPage();
        Document doc = new Document(pdfDoc);
        Table table = new Table(1);
        table.addCell("Signer 1: Alice");
        table.addCell(createSignatureFieldCell("sig1"));
        table.addCell("Signer 2: Bob");
        table.addCell(createSignatureFieldCell("sig2"));
        table.addCell("Signer 3: Carol");
        table.addCell(createSignatureFieldCell("sig3"));
        doc.add(table);
        doc.close();
    }
    
  2. Take intermediate_output file and sign it.

While running the step two with output of step 1, i am getting com.itextpdf.kernel.PdfException: error.reading.objstm

Exception in thread "main" com.itextpdf.kernel.PdfException: error.reading.objstm
    at com.itextpdf.kernel.pdf.PdfReader.readObjectStream(PdfReader.java:508)
    at com.itextpdf.kernel.pdf.PdfReader.readObject(PdfReader.java:1014)
    at com.itextpdf.kernel.pdf.PdfReader.readObject(PdfReader.java:533)
    at com.itextpdf.kernel.pdf.PdfIndirectReference.getRefersTo(PdfIndirectReference.java:128)
    at com.itextpdf.kernel.pdf.PdfIndirectReference.getRefersTo(PdfIndirectReference.java:132)
    at com.itextpdf.kernel.pdf.PdfArray.get(PdfArray.java:376)
    at com.itextpdf.kernel.pdf.PdfArray.get(PdfArray.java:237)

Input pdf:

Input pdf

Intermediate pdf page 1:

Intermediate pdf page 1

Intermediate pdf page 2:

Intermediate pdf page 2

Please guide me in case, i am doing something wrong here.


Solution

  • The underlying iText 7 bug/peculiarity is the same as described in this answer where the table is built across four pages but all the fields turns up on the last page.

    As you clarified, though, in a comment, you want the table and the fields on the last page anyways. Thus, all we need to do is move the table to the last page, too.

    This actually is quite simple, merely add an appropriate AreaBreak before adding the table:

    doc.add(new AreaBreak(AreaBreakType.LAST_PAGE));
    doc.add(table);
    

    (AddSignatureField test testAddSignaturesInTable)


    You updated your iText version in the context of this question. Meanwhile there have been considerable changes in the table creation code. Thus, you will probably want to also set a width of the signature cell, e.g.

    Cell cell = new Cell();
    cell.setHeight(50);
    cell.setWidth(200);
    cell.setNextRenderer(new SignatureFieldCellRenderer(cell, name));
    return cell;
    

    (AddSignatureField method createSignatureFieldCell)