2

I generate a PDF file with JAVA and jasper. Such jasper file is designed with iReport. Once the pdf file is created, I would like to place a signature with PdfStamper in a concrete position in the pdf. Possible solutions that I have found:

Use PdfSignatureAppearance.setVisibleSignature method. This does not fit my needs since it locates the signature in coordinates based position.

PdfStamper stp = PdfStamper.createSignature(reader, outStream, '\0', fileTmp);
PdfSignatureAppearance sap = stp.getSignatureAppearance();
sap.setVisibleSignature(new Rectangle(100, 100, 200, 200), 1, null);

Use PdfReader.getAcroFields() and then go through the AcroFields, get the coordinates of a predifined form field and insert the signature as shown in the previous option. The problem is that I am not able to define AcroFields with iReport, so I cannot use it either.

My question: is there any way to define fields with iReport and read after the PDF is created with Java?

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
alvgarvilla
  • 1,026
  • 12
  • 25
  • If you have an image (with signature), you can look at [JasperReports - Images Sample](http://jasperreports.sourceforge.net/sample.reference/images/index.html). There are several supported types of fields: *java.io.InputStream*, *java.awt.Image*, etc – Alex K Feb 25 '16 at 12:25

1 Answers1

3

You can use the PdfReaderContentParser to find image and text inside the pdf.

Example (showing how to find location of both text and image in pdf)

PdfReader reader = new PdfReader(src);
int pageILikeToCheck =reader.getNumberOfPages(); //set the page or loop them all        
final String matchStr = "FIND THIS TEXT";

PdfReaderContentParser parser = new PdfReaderContentParser(reader);
parser.processContent(pageILikeToCheck, new RenderListener() {  

    @Override
    public void renderImage(ImageRenderInfo renderInfo) {
        PdfImageObject image;
        try {
            image = renderInfo.getImage();
            if (image == null) return;
            System.out.println("Found image");
            System.out.println(renderInfo.getStartPoint());
        } catch (IOException e) {
            e.printStackTrace();
        }     
    }

    @Override
    public void renderText(TextRenderInfo renderInfo) {

        if(renderInfo.getText().length()>0 && matchStr.contains(renderInfo.getText())){
            System.out.println("FOUND MY TEXT");
            System.out.println(renderInfo.getBaseline().getStartPoint());
            System.out.println(renderInfo.getBaseline().getEndPoint());
        }
    }

    @Override
    public void endTextBlock() {
    }

    @Override
    public void beginTextBlock() {
    }
});

However I normally add the signature in pdf to a pre-defined space (using pageFooter or lastPageFooter band) using the PdfStamper

PdfReader reader = new PdfReader(src);
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
PdfStamper stamper = new PdfStamper(reader, baos);
int pageSignature=1;
stamper.addSignature("Signature", pageSignature, 320, 570, 550, 620);

and then write baos to file.

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
  • Hi Petter! Thanks for your answer. The first approach works perfectly! thank you very much. The problem with the second one is that I cannot locate the sign in a static position, it depends on the PDF – alvgarvilla Feb 26 '16 at 12:03
  • @alvgarvilla Hmm, I can't understand you do not need to locate, you put the (image of signature) in example the lastPageFooter band (this way it will always have an exact location (on last page), then you add the pdf signature there with fixed positions. – Petter Friberg Feb 26 '16 at 12:10
  • True that the signature this way is at fixed position in all pdfs but thats more of an opinion reasons if report is nice or not with signature on the bottom. However thanks for the accept. – Petter Friberg Feb 26 '16 at 12:12