Java PDF Stamper写在下面复选框(文本被复选框覆盖)

时间:2017-05-26 15:26:44

标签: java checkbox itext pdfstamper

我试图使用java pdf压模写入现有的pdf,但由于某种原因,pdf中有一个复选框,文本似乎是在下面绘制的。

阅读代码pdf:

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);

绘图文字代码:

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

1 个答案:

答案 0 :(得分:1)

首先关闭:

迭代PDF中的FormFields并找出要设置的有效值:

        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());
        }

现在,您应该可以通过设置允许值来选中该复选框:

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