Discussion:
[iText-questions] trying to remove a signature from pdf file
Massa Alfonso
2016-01-21 15:09:44 UTC
Permalink
Hi,
I'm trying to remove a signature from the attached sample PDF with itextpdf 5.5.8 and save the result in another file using this java code:

Provider provider = new BouncyCastleProvider();
Security.addProvider(provider);

PdfReader reader = new PdfReader(inputStream, null);
AcroFields af = reader.getAcroFields();
ArrayList<String> names = af.getSignatureNames();
for (String name : names) {
System.out.println("Signature name: " + name);
System.out.println("Signature covers whole document: " + af.signatureCoversWholeDocument(name));
PdfPKCS7 pk = af.verifySignature(name, provider.getName());
System.out.println("SignatureDate: " + pk.getSignDate());
System.out.println("Certificate: " + pk.getSigningCertificate());
System.out.println("Document modified: " + !pk.verify());
af.removeField(name);
}
PdfStamper stamper = new PdfStamper(reader, outputStream, '\0');
stamper.close();

But I have this exception in removeField:
Exception in thread "main" java.lang.NullPointerException
at com.itextpdf.text.pdf.AcroFields.removeField(AcroFields.java:1814)
at com.itextpdf.text.pdf.AcroFields.removeField(AcroFields.java:1854)
at securityframe.cms.VerificaFirmaCerta.verify(VerificaFirmaCerta.java:47)
at securityframe.cms.VerificaFirmaCerta.main(VerificaFirmaCerta.java:67)

The attached file is signed with FirmaCerta (a italian sw).
Anyone can help me? There is a bug in the signature sw or in the itext library?

Thanks in advance.

Alfonso


AVVISO DI RISERVATEZZA Informazioni riservate possono essere contenute nel messaggio o nei suoi allegati. Se non siete i destinatari indicati nel messaggio, o responsabili per la sua consegna alla persona, o se avete ricevuto il messaggio per errore, siete pregati di non trascriverlo, copiarlo o inviarlo ad alcuno. In tal caso vi invitiamo a cancellare il messaggio ed i suoi allegati. Grazie.
CONFIDENTIALITY NOTICE Confidential information may be contained in this message or in its attachments. If you are not the addressee indicated in this message, or responsible for message delivering to that person, or if you have received this message in error, you may not transcribe, copy or deliver this message to anyone. In that case, you should delete this message and its attachments. Thank you.
mkl
2016-01-21 21:58:39 UTC
Permalink
Post by Massa Alfonso
Anyone can help me? There is a bug in the signature sw or in the itext library?
The signature field claims it has a widget annotation on the page
represented by the object 1 0 indicated by the form field type Annot and
subtype Widget (which means that this form field dictionary has a single
widget annotation dictionary merged into it) in combination with the P value
(by which an annotation points to its page object).

21 0 obj
<<
/FT /Sig
/T (firmacertasign_1)
/Type /Annot
/Subtype /Widget
/F 132
/P 1 0 R
/AP <<
Post by Massa Alfonso
/Rect [
0
0
0
0
]/V 22 0 R
endobj

On the other hand the referenced page dictionary claims not to have any
annotations as it has no Annots array:

1 0 obj
<</Type /Page/Parent 6 0 R/Resources 13 0 R/MediaBox [0 0 595 842 ]/Group
&lt;&lt;/S /Transparency/CS /DeviceRGB/I true>>/Contents 2 0 R>>
endobj

This structure is questionable and suspect even if the specification may be
read in a way to call this valid (Annots is an optional page entry but
commonly it is assumed that it is optional only if the page has no
annotations).

The method AcroFields.removeField(String, int) hickups when confronted with
such a situation:

int pageV = item.getPage(k).intValue();
if (page != -1 && page != pageV)
continue;
PdfIndirectReference ref = item.getWidgetRef(k);
PdfDictionary wd = item.getWidget( k );
PdfDictionary pageDic = reader.getPageN(pageV);
PdfArray annots = pageDic.getAsArray(PdfName.ANNOTS);

If a form field is not referenced from any document page, pageV is -1, so
pageDic is null and pageDic.getAsArray results in a NullPointerException.

To answer your question, therefore, this a a dubious construct created by
the signature software which then is processed by iText code not
sufficiently defensively programmed.

Regards, Michael



--
View this message in context: http://itext.2136553.n4.nabble.com/trying-to-remove-a-signature-from-pdf-file-tp4660983p4660984.html
Sent from the iText mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140
_______________________________________________
iText-questions mailing list
iText-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php
Loading...