0

Following is the code I am using to place digital signatures on standard four coordinates ie top left, top right, bottom left, bottom right. However, its not working correctly for top left.

 PdfReader reader = new PdfReader(src);//src being file path of pdf being signed
 Rectangle cropBox = reader.getCropBox(1);
 if(signaturePosition.equalsIgnoreCase("bottom-left"))
           appearance.setVisibleSignature(new Rectangle(cropBox.getLeft(), cropBox.getBottom(),
                  cropBox.getLeft(width), cropBox.getBottom(height)), 1,    "first");
           if(signaturePosition.equalsIgnoreCase("bottom-right"))
               appearance.setVisibleSignature(new Rectangle(cropBox.getRight(width), cropBox.getBottom(),
                       cropBox.getRight(), cropBox.getBottom(height)), 1,    "first");
           if(signaturePosition.equalsIgnoreCase("top-left"))
               appearance.setVisibleSignature(new Rectangle(72, 732, 144, 780), 1,    "first");
           if(signaturePosition.equalsIgnoreCase("top right"))
               appearance.setVisibleSignature(new Rectangle(cropBox.getRight(width), cropBox.getTop(height),
                     cropBox.getRight(), cropBox.getTop()), 1,    "first");

Its required to position the signature anywhere on pdf depending upon coordinates provided by the user. To get user's coordinates, we are using a pdf xpro template with a textbox placed over it(user will upload the template and place the textbox where he requires signature to be placed), using that textbox coordinates with its height and width as reference we will position the signature on the pdf.

I need help on understanding how appearance.setVisibleSignature() method works wrt the Rectangle object passed to it, because for each quadrant of the page(considering the center of the page as origin) the parameters are being passed differently for top left, top right, bottom left and bottom right respectively.

Devanshu Kashyap
  • 185
  • 1
  • 22
  • *its not working correctly for top left* - more exactly, you have problems in the case of your fixed coordinates `72, 732, 144, 780`. Each page in each PDF may have a coordinate system of its own. Thus, fixed coordinates are bound to fail sooner or later. *how `appearance.setVisibleSignature()` method works wrt the `Rectangle` object passed to it* - Fairly simple... It uses those exact `Rectangle` coordinates to place the signature. *or each quadrant of the page(considering the center of the page as origin) the parameters are being passed differently* - other positions, other coordinates. – mkl Mar 12 '15 at 11:26
  • @mkl :When I am trying to put put custom coordinates, its giving large distorted rectangles. keeping the 72, 732, 144, 780 hard coding for top left as refrence. I just need insights of passing these as parameters. – Devanshu Kashyap Mar 12 '15 at 14:19

1 Answers1

0

The OP clarified in a (meanwhile deleted) comment:

I am asking for explanation of passing height width as parameter to getTop(),getLeft() etc functions. Its not being clear.

Those methods are defined as:

// Returns the lower left y-coordinate, considering a given margin.
public float getBottom(final float margin) {
    return lly + margin;
}

// Returns the lower left x-coordinate, considering a given margin.
public float getLeft(final float margin) {
    return llx + margin;
}

// Returns the upper right x-coordinate, considering a given margin.
public float getRight(final float margin) {
    return urx - margin;
}

// Returns the upper right y-coordinate, considering a given margin.
public float getTop(final float margin) {
    return ury - margin;
}

(Rectangle.java)

I.e. these methods return the maximum or minimum x or y coordinate moved inwards by the given margin distance. In the calculations you copied above these methods were used merely as a replacement of some additions or subtractions.

ideally rectangle is top left coordinate as first two coords and height and width as other 2 coords,

What one considers ideal depends on circumstances. What we have here is the constructor

/**
 * Constructs a <CODE>Rectangle</CODE> -object.
 *
 * @param llx   lower left x
 * @param lly   lower left y
 * @param urx   upper right x
 * @param ury   upper right y
 */
public Rectangle(final float llx, final float lly, final float urx, final float ury)

(ibidem)

So, first you have the lower left x and y, then the upper right x and y. In other words, you have left x, bottom y, right x, top y.

but its being done differently. Request insights into this.

If you have the upper left coordinates and the width and height, those required coordinates are easy to calculate.

BTW, I hope you are aware that in PDF the default user space coordinate system can have its origin anywhere on or off the page (check the crop box), and that (as common in mathematics) x coordinates are increasing to the right and y coordinates are increasing upwards.

mkl
  • 90,588
  • 15
  • 125
  • 265
  • Thanks @mkl. Actually itext and xmlworkers dont yet have proper latest documentation which makes it extremely difficult to understand. I was really unaware of this complex Rectangle constructor in context with pdfs as i have used rectangle objects differently. Thank you for clarifying, – Devanshu Kashyap Mar 12 '15 at 17:17