问题
I have a little app to add a signature to a PDF in JAVA using iText. This is a fragment of the code:
PdfReader reader = new PdfReader(pdfBytes);
FileOutputStream fos = new FileOutputStream(new File("/home/john/signedPdf.pdf"));
PdfStamper stamper = PdfStamper.createSignature(
reader,
fos,
'\0',
new File("/home/john/"),
true
);
PdfSignatureAppearance signatureAppearance = stamper.getSignatureAppearance();
signatureAppearance.setRenderingMode(PdfSignatureAppearance.RenderingMode.DESCRIPTION);
Rectangle rectangle = new Rectangle(
36,
748 - 20 * (next - 1) ,
144,
780 - 20 * (next - 1)
);
rectangle.normalize();
signatureAppearance.setVisibleSignature(
rectangle,
1, contact);
The PDF is signed good, but the visible sign in the rectangle has a padding and one rectangle get over the second, and second get over third, etc. This is the example image: Exist a way to delete this padding and evit one rectangle get over other. Thanks in advance
回答1:
You use rectangles created like this for your signatures
Rectangle rectangle = new Rectangle(
36,
748 - 20 * (next - 1) ,
144,
780 - 20 * (next - 1)
);
where (as clarified in comments) the integer next
can have consecutive values for the signatures, e.g. 1 and 2.
But this means that you actually ask for overlapping signature rectangles! E.g. for for the values 1 and 2 you get:
next == 1
- rectangle top y: 780; rectangle bottom y: 748next == 2
- rectangle top y: 760; rectangle bottom y: 728
So these rectangles overlap for y between 760 and 748.
If you don't want your rectangle to overlap, the y step factor (currently 20) must be at least as large as the difference between the top and bottom y coordinate start values (currently 780 - 748 = 32).
E.g. you can use a step factor of 32
Rectangle rectangle = new Rectangle(
36,
748 - 32 * (next - 1) ,
144,
780 - 32 * (next - 1)
);
or a rectangle height of 20
Rectangle rectangle = new Rectangle(
36,
760 - 20 * (next - 1) ,
144,
780 - 20 * (next - 1)
);
instead of your current rectangle dimensions and locations.
来源:https://stackoverflow.com/questions/49861696/delete-padding-of-rectangle-in-itext-pdf-signature