Element coordinates in pdf file using iText

I am creating a pdf using the BIRT reporting library. Later, I need to digitally sign these files. I am using iText to digitally sign a document.

The problem I am facing is that I need to place the signature in different places in different reports. I already have the code to digitally sign the document, now I always put my signature at the bottom of the last page in every report.

Ultimately I need each report to point out where I need to sign. Then I have to read the location using iText and then put the signature at that location.

Is it possible to achieve using BIRT and iText

thank

+2


source to share


2 answers


If you want to cheat a little, you can use the link ... which BIRT maintains as per my little dive into my docs just now.

A link is an annotation. Unfortunately, iText does not support high-level annotations, only generating them, so you have to use low-level object calls.

The code to extract it might look something like this:

// getPageN is looking for a page number, not a page index
PdfDictionary lastPageDict = myReader.getPageN(myReader.getNumberOfPages());

PdfArray annotations = lastPageDict.getAsArray( PdfName.ANNOTS );
PdfArray linkRect = null;
if (annotations != null) {
  int numAnnots = annotations.size();
  for (int i = 0; i < numAnnots; ++i) {
    PdfDictionary annotDict = annotations.getAsDict( i );
    if (annotDict == null) 
      continue; // it'll never happen, unless you're dealing with a Really Messed Up PDF.

    if (PdfName.LINK.equals( annotDict.getAsName( PdfName.SUBTYPE ) )) {
      // if this isn't the only link on the last page, you'll have to check the URL, which
      // is a tad more work.
      linkRect = annotDict.getAsArray( PdfName.RECT );

      // a little sanity check here wouldn't hurt, but I have yet to come across a PDF
      // that was THAT screwed up, and I've seen some Really Messed Up PDFs over the years.

      // and kill the link, it just there for a placeholder anyway.
      // iText doesn't maintain any extra info on links, so no need for other calls.
      annotations.remove( i ); 
      break;
    }
  }
}

if (linkRect != null) {
  // linkRect is an array, thusly: [ llx, lly, urx, ury ].
  // you could use floats instead, but I wouldn't go with integers.  
  double llx = linkRect.getAsNumber( 0 ).getDoubleValue(); 
  double lly = linkRect.getAsNumber( 1 ).getDoubleValue();
  double urx = linkRect.getAsNumber( 2 ).getDoubleValue();
  double ury = linkRect.getAsNumber( 3 ).getDoubleValue();

  // make your signature
  magic();
}

      



If BIRT generates some text in the page content from the link to represent it visually, this is only a minor issue. Your signature must cover it completely.

You're definitely better off if you can generate the signature directly from BIRT, but my little review of their documents didn't quite fill me with confidence in the PDF customization capabilities ... despite sitting on top of iText itself. It is a report generator that is possibly capable of generating PDF files ... I shouldn't expect too much. `

Edit: if you need to find a specific url you need to look at the "12.5.6.5 Link to Annotations" section in the PDF reference which can be found here: http://www.adobe.com/content/dam/Adobe/en/ devnet / pdf / pdfs / PDF32000_2008.pdf

+3


source


I don't know anything about BIRT and am a little familiar with iText. But maybe it works ...

Can BIRT generate a signature box outline as a regular form field with a given field name? If so, then you should be able to:



  • Find this field by name in the hashmap iText AcroFields using getField;
  • Create a new signature using pdf stamp and set its geometry based on the values โ€‹โ€‹of the old field object; and
  • Remove the old field with removeField.
+1


source







All Articles