0

Im working on a digital signature solution

im looking for signature panels within a pdf document using a solution i found here

Itext get field coordinates from existing pdf

i then apply a signature to the location of the signature field, and it should look like the user has signed the document

however when the following line executes it exits the sub

for(String signame : fields.getBlankSignatureNames()) 

its a bit of a mystery as there are two signature fields within the fields object

any help would be appreciated

heres my full code

import android.os.Environment;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class PDFStamper {

    public static void main()  {



        try {
            PdfReader pdfReader = new PdfReader(Environment.getExternalStorageDirectory()+"/sample.pdf");

            PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(Environment.getExternalStorageDirectory()+"/sample-signed.pdf"));

            Image image = Image.getInstance(Environment.getExternalStorageDirectory()+"/signature.png");

            float scaler = 10;

            image.scalePercent(scaler);



        String pdfFile = Environment.getExternalStorageDirectory()+"/sample.pdf";

        PdfReader reader = new PdfReader(pdfFile);

        AcroFields fields = reader.getAcroFields();


        for(String signame : fields.getBlankSignatureNames()) {
            List<AcroFields.FieldPosition> positions = fields.getFieldPositions(signame);
            Rectangle rect = positions.get(0).position; // In points:
            float left = rect.getLeft();
            float bTop = rect.getTop();
            float width = rect.getWidth();
            float height = rect.getHeight();

            int page = positions.get(0).page;
            Rectangle pageSize = reader.getPageSize(page);
            float pageHeight = pageSize.getTop();
            float top = pageHeight - bTop;

            System.out.print(signame + "::" + page + "::" + left + "::" + top + "::" + width + "::" + height + "\n");

            //put content over
            PdfContentByte content = pdfStamper.getUnderContent(page);

            content = pdfStamper.getOverContent(page);

            image.setAbsolutePosition(left, bTop);
            content.addImage(image);

        }

                pdfStamper.close();

            } catch (IOException e) {
                e.printStackTrace();
            } catch (DocumentException e) {
                e.printStackTrace();
            }
        }



        }
Community
  • 1
  • 1
PowerMan2015
  • 1,307
  • 5
  • 19
  • 40
  • Please share the PDF in question. – mkl Aug 14 '15 at 04:10
  • When encountering problems with iText on Android, it is always good to check whether you're using the Android port [iTextG](http://itextpdf.com/product/itextg) or the Java version. The Java version of iText is not suited for use on Android because it has some dependencies on `java.awt`, `javax.nio`, etc... This can result in strange exceptions. With every new elease of iText, we also provide a release of iTextG which is tailored for use on Android and Google App Engine. – Bruno Lowagie Aug 14 '15 at 06:58
  • @BrunoLowagie thanks for your input. I do believe I have used the iTextG release. Is there any way I can confirm this? I'm wondering if it's the java import I make in the class? – PowerMan2015 Aug 14 '15 at 10:22
  • @mkl I will upload this later today after work – PowerMan2015 Aug 14 '15 at 10:23

0 Answers0