0

Anybody has an example of how to sign a pdf file with using files only with memory in Android with iTextG 5.4.3. I tried with java examples but it throws a NullPointerException when I call to preClosed() of PdfSignatureAppearance.

Here is the code that I use:

UUID idPDF = UUID.randomUUID();

InputStream input = new ByteArrayInputStream(pdfOriginal);
OutputStream output = new ByteArrayOutputStream();
PdfReader reader = new PdfReader(input);
PdfStamper stamper = PdfStamper.createSignature(reader, output, '\0');

PdfSignatureAppearance sap = stamper.getSignatureAppearance();

String urx = String.valueOf(Integer.parseInt(FIRMA_POSICION_X) + 250);
String lly = String.valueOf(Integer.parseInt(FIRMA_POSICION_Y) - 50);

stamper.addSignature("Signature" + idPDF.toString(), 1, 0, 0, 0, 0);        
sap.setVisibleSignature(new Rectangle(
                Float.parseFloat(FIRMA_POSICION_X), 
                Float.parseFloat(FIRMA_POSICION_Y), 
                Float.parseFloat(urx), 
                Float.parseFloat(lly)),1,"Signature" + idPDF.toString());

// I define the sign and date
sap.setSignatureGraphic(getSello());
sap.setSignDate(new GregorianCalendar());

sap.setReason(razon);
sap.setLocation(ubicacion);
sap.setContact(contacto);
sap.setRenderingMode(PdfSignatureAppearance.RenderingMode.GRAPHIC_AND_DESCRIPTION);


HashMap<PdfName, Integer> exc = new HashMap<PdfName, Integer>(); 
exc.put(PdfName.CONTENTS, new Integer(0x2502));     
sap.preClose(exc);

MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
byte buf[] = new byte[8192];
int n;
InputStream inp = sap.getRangeStream();
while ((n = inp.read(buf)) > 0) {
 messageDigest.update(buf, 0, n);
}
byte hash[] = messageDigest.digest();
PdfLiteral slit = new PdfLiteral(buf);
byte[] outc = new byte[(slit.getPosLength() - 2) / 2];          
Signature sign = Signature.getInstance("SHA1withRSA");
sign.initSign(key);
sign.update(hash);          
PdfDictionary dic = new PdfDictionary();
dic.put(PdfName.CONTENTS, new PdfString(outc).setHexWriting(true));     
sap.close(dic); 

stamper.close();
reader.close();     
output.flush();
output.close();
input.close();
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • 2
    Standard practice is to show what you have tried so far. As it stands, this is likely to be closed as it's not clear what problem you are trying to solve. – thecoshman Mar 12 '15 at 15:53
  • While this probably is not the cause of your issue, I'd anyway propose you switch to the current iText version to start with. – mkl Mar 13 '15 at 16:36

0 Answers0