Java PDF Stamper written below checkbox (text spreads over checkbox)

I'm trying to write to an existing pdf using java pdf stamper, but for some reason there is a certain checkbox in the PDF that appears to be drawn.

Pdf reading code:

PdfReader reader = new PdfReader(Testing.getImagePath() + "form.pdf");
File dir = new File(Testing.getResourcePath() + id + "/");
String destination = Testing.getResourcePath() + id + "form" + id + ".pdf";
File exist = new File(destination);

dir.mkdirs();
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destination));
stamper.setFormFlattening(true);
PdfContentByte over;
over = stamper.getOverContent(1);

      

Code for drawing text:

over.beginText();
over.setFontAndSize(bf, 11);
over.setTextMatrix(169, 322);
over.showText("X");
over.endText();

      

+3


source to share


1 answer


First of all:

Go through FormFields to your PDF and find out the valid values:

        AcroFields form = stamper.getAcroFields();
        for(Entry<String, Item> field : form.getFields().entrySet()) {
            System.out.println(field.getKey() + ": " + field.getValue());
            String[] values = form.getAppearanceStates(field.getKey());
            StringJoiner sb = new StringJoiner(",");
            for (String value : values) {
                sb.add(value);
            }
            System.out.println("Possible Options: " + sb.toString());
        }

      



Now you can check the box by setting it to a valid value:

        form.setField("myCheckbox", "myYesValue");

      

+1


source







All Articles