IText Stamping - Java

I am having problems embossing PDF documents without revoking digital signatures.

Current, I've done well at embossing PDF. However, if the document was previously signed, the signature is no longer valid. I understand why this is happening, but if I use Acrobat to add text or markup with annotation, the signature is valid.

I tried adding annotations or comments, but that still didn't validate the signature. Is there a way to add a stamp to PDF using iText without undoing digital signatures?

Here is the code snippet I'm using for the stamp:

        PdfReader reader = new PdfReader(inputstream);

        stamp = new PdfStamper(reader, new FileOutputStream(file));


        PdfContentByte pcb;
        BaseFont bf = BaseFont.createFont("Courier", BaseFont.CP1250,BaseFont.EMBEDDED);

        Rectangle r = reader.getPageSizeWithRotation(1);

        pcb = stamp.getOverContent(1);

        // set the font and size
        float size = 12;
        pcb.setFontAndSize(bf, size);

        float width = 90;
        float centerX = 0, startY = 0;
        centerX = r.getWidth() - (width / 2) - 20;
        startY = r.getHeight() - (15 * 2) - 145;

        pcb.beginText();
        pcb.showTextAligned(PdfContentByte.ALIGN_CENTER, stampText, centerX, startY, 0);

        pcb.setFontAndSize(bf, 10);
        pcb.showTextAligned(PdfContentByte.ALIGN_CENTER, date, centerX-9, startY-8, 0);
        pcb.endText();
        stamp.close();          

      

Any help would be much appreciated, Thanks

+3


source to share


1 answer


I did it. ':)

Here is the code used to add custom text to a document using iText without revoking digital signatures.

//Read the source PDF
PdfReader reader = new PdfReader(inputstream);
//Create PdfStamp object
stamp = new PdfStamper(reader, new FileOutputStream(file), '\0', true);

//Create the proper annotation
PdfAnnotation annot = PdfAnnotation.createFreeText(stamp.getWriter(),  new Rectangle(150, 150, 200, 200), "Annotation 1", pcb);
annot.setFlags(PdfAnnotation.FLAGS_PRINT);

//Insert the annotation         
stamp.addAnnotation(annot, 1);
//Close the stamp
stamp.close();  

      



EDIT:

To insert an image stamp into a document without revoking digital signatures, I used this code:

//Read the pdf 
PdfReader reader = new PdfReader(inputstream);
//Use PdfStamper in append mode
stamp = new PdfStamper(reader, new FileOutputStream(file), '\0', true); 

//Read the image
Image img = Image.getInstance(ImageIO.read(imgStream), null);

float w = img.getScaledWidth();
float h = img.getScaledHeight();
Rectangle location = new Rectangle(70, 770 - h, 70 + w, 770);

//Create stamp annotation           
PdfAnnotation stampAnnot = PdfAnnotation.createStamp(stamp.getWriter(), location, null, "ITEXT");
img.setAbsolutePosition(0, 0);
//Create new PdfContentByte from the stamp writer
//If you use cd = stamp.getOverContent(1) - you'll invalidate the signatures
PdfContentByte cb = new PdfContentByte(stamp.getWriter());
PdfAppearance app = cb.createAppearance(w, h);
app.addImage(img);
stampAnnot.setAppearance(PdfName.N, app);
stampAnnot.setFlags(PdfAnnotation.FLAGS_PRINT);

stamp.addAnnotation(stampAnnot, 1);
reader.close();

      

+4


source







All Articles