Delete padding of Rectangle in iText PDF signature

你离开我真会死。 提交于 2019-12-11 15:05:53

问题


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: 748
  • next == 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!