Java 如何将边框添加到复选框并使其始终可见

Java 如何将边框添加到复选框并使其始终可见,java,checkbox,pdfbox,Java,Checkbox,Pdfbox,当我创建PDF时,复选框最初没有任何外观。当我点击它(在模糊上)后,有某种阴影可见,当它聚焦时,它就不可见了。 如何使其始终可见?我怎样才能添加某种边框(而不用使用矩形类手动添加) 公共类CheckBoxWriter{ 公共静态void main(字符串[]args)引发IOException{ PDDocument文档=新PDDocument(); PDPage page=新PDPage(); 文件。添加页(第页); //只有一个矩形才能找到复选框 PDPageContentStream c

当我创建PDF时,复选框最初没有任何外观。当我点击它(在模糊上)后,有某种阴影可见,当它聚焦时,它就不可见了。 如何使其始终可见?我怎样才能添加某种边框(而不用使用矩形类手动添加)

公共类CheckBoxWriter{
公共静态void main(字符串[]args)引发IOException{
PDDocument文档=新PDDocument();
PDPage page=新PDPage();
文件。添加页(第页);
//只有一个矩形才能找到复选框
PDPageContentStream contentStream=新的PDPageContentStream(文档,页面,AppendMode.APPEND,false);
addRect(100,100,50,50);
contentStream.setLineWidth(1);
setNonSrokingColor(Color.WHITE);
contentStream.setStrokingColor(Color.BLACK);
contentStream.stroke();
contentStream.close();
PDAcroForm acroForm=新的PDAcroForm(文件);
PDCheckBox复选框=新的PDCheckBox(acroForm);
复选框.setPartialName(“xxx”);
PDAnnotationWidget=新的PDAnnotationWidget();
setRectangle(新的PDRectangle(100,100,10,10));
widget.setAnnotationFlags(4);
设置页面(第页);
widget.setParent(复选框);
List widgets=new ArrayList();
widgets.add(widget);
page.getAnnotations().add(小部件);
checkBox.setWidgets(widgets);
acroForm.getFields().add(复选框);
document.getDocumentCatalog().setAcroForm(acroForm);
文件=新文件(“test.pdf”);
文件保存(文件);
document.close();
}
}

试试这个-它会为复选框创建外观流,即“是”和“关闭”显示的内容流。我还修改了小部件的使用方式,使文件更简单。只有当一个字段有多个小部件时,您使用的策略(小部件与字段分开)才有用

要查看操作员的操作(这是胡言乱语),请查看,搜索“操作员摘要”

publicstaticvoidmain(字符串[]args)引发IOException
{
PDDocument文档=新PDDocument();
PDPage page=新PDPage();
文件。添加页(第页);
//只有一个矩形才能找到复选框
PDPageContentStream contentStream=新的PDPageContentStream(文档,页面,AppendMode.APPEND,false);
addRect(100,100,50,50);
contentStream.setLineWidth(1);
setNonSrokingColor(Color.WHITE);
contentStream.setStrokingColor(Color.BLACK);
contentStream.stroke();
contentStream.close();
PDAcroForm acroForm=新的PDAcroForm(文件);
PDCheckBox复选框=新的PDCheckBox(acroForm);
复选框.setPartialName(“xxx”);
//PDAnnotationWidget=新的PDAnnotationWidget();
PDAnnotationWidget widget=checkBox.getWidgets().get(0);
setRectangle(新的PDRectangle(100,100,10,10));
widget.setAnnotationFlags(4);
设置页面(第页);
//widget.setParent(复选框);
//灵感来源于PDFBOX-563文件的annot 92
//annot 172具有“复选标记”,但更复杂的是,需要ZaDb
字符串offNString=“1 g\n”
+“0 9.5 9.5 re\n”
+“f\n”
+“0.5 0.5 9.5 9.5 re\n”
+“s”;
字符串offDString=“0.75 g\n”
+“0 9.5 9.5 re\n”
+“f\n”
+“0.5 0.5 9.5 9.5 re\n”
+“s”;
String yesNString=“1 g\n”
+“0 0 9 9.5 re\n”
+“f\n”
+“0.5 0.5 9.5 9.5 re\n”
+“s\n”
+“q\n”
+“1 19 9 re\n”
+“W\n”
+“n\n”
+“28米\n”
+“8 2 l\n”
+“8米\n”
+“2 l\n”
+“s\n”
+“Q”;
String是String=“0.75 g\n”
+“0 0 9 9.5 re\n”
+“f\n”
+“0.5 0.5 9.5 9.5 re\n”
+“s\n”
+“q\n”
+“1 19 9 re\n”
+“W\n”
+“n\n”
+“28米\n”
+“8 2 l\n”
+“8米\n”
+“2 l\n”
+“s\n”
+“Q”;
COSDictionary apNDict=新的COSDictionary();
COSTREAM offNStream=新COSTREAM();
offNStream.setItem(COSName.BBOX,新的PDRectangle(10,10));
offNStream.setItem(COSName.FORMTYPE,cointeger.ONE);
offNStream.setItem(COSName.TYPE,COSName.XOBJECT);
offNStream.setItem(COSName.SUBTYPE,COSName.FORM);
offNStream.setItem(COSName.MATRIX,new MATRIX().toCOSArray());
offNStream.setItem(COSName.RESOURCES,new COSDictionary());
OutputStream os=offNStream.createOutputStream();
write(offNString.getBytes());
os.close();
apNDict.setItem(COSName.Off,offNStream);
costream yesNStream=新costream();
setItem(COSName.BBOX,新的PDRectangle(10,10));
setItem(COSName.FORMTYPE,cointeger.ONE);
yesNStream.setItem(COSName.TYPE,COSName.XOBJECT);
yesNStream.setItem(COSName.SUBTYPE,COSName.FORM);
setItem(COSName.MATRIX,new MATRIX().toCOSArray());
yesNStream.setItem(COSName.RESOURCES,new cosdirectionary());
os=yesNStream.createOutputStream();
write(yesNString.getBytes());
os.close();
apNDict.setItem(COSName.getPDFName(“是”),yesNStream);
COSDictionary apDDict=新的COSDictionary();
costream offDStream=新的costream();
offDStream.setItem(COSName.BBOX,新的PDRectangle(16,16));
offDStream.setItem(COSName.FORMTYPE,cointeger.ONE);
offDStream.setItem(COSName.TYPE,COSName.XOBJECT);
offDStream.setItem(COSName.SUBTYPE,COSName.FORM);
offDStream.setItem(COSName.MATRIX,new MATRIX().toCOSArray());
offDStream.setItem(COSName.RESOURCES,new COSDictionary());
o
public class CheckBoxWriter  {

    public static void main(String[] args) throws IOException {

        PDDocument document = new PDDocument();
        PDPage page = new PDPage();
        document.addPage(page);

        // just a rectangle to be able to locate the checkbox
        PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, false);
        contentStream.addRect(100, 100, 50, 50);
        contentStream.setLineWidth(1);
        contentStream.setNonStrokingColor(Color.WHITE);
        contentStream.setStrokingColor(Color.BLACK);
        contentStream.stroke();
        contentStream.close();

        PDAcroForm acroForm = new PDAcroForm(document);
        PDCheckBox checkBox = new PDCheckBox(acroForm);
        checkBox.setPartialName("xxx");

        PDAnnotationWidget widget = new PDAnnotationWidget();
        widget.setRectangle(new PDRectangle(100, 100, 10, 10));
        widget.setAnnotationFlags(4);
        widget.setPage(page);
        widget.setParent(checkBox);

        List<PDAnnotationWidget> widgets = new ArrayList<>();
        widgets.add(widget);
        page.getAnnotations().add(widget);
        checkBox.setWidgets(widgets);

        acroForm.getFields().add(checkBox);
        document.getDocumentCatalog().setAcroForm(acroForm);

        File file = new File("test.pdf");

        document.save(file);
        document.close();
    }
}
public static void main(String[] args) throws IOException
{
    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage(page);

    // just a rectangle to be able to locate the checkbox
    PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, false);
    contentStream.addRect(100, 100, 50, 50);
    contentStream.setLineWidth(1);
    contentStream.setNonStrokingColor(Color.WHITE);
    contentStream.setStrokingColor(Color.BLACK);
    contentStream.stroke();
    contentStream.close();

    PDAcroForm acroForm = new PDAcroForm(document);
    PDCheckBox checkBox = new PDCheckBox(acroForm);
    checkBox.setPartialName("xxx");


    //PDAnnotationWidget widget = new PDAnnotationWidget();
    PDAnnotationWidget widget = checkBox.getWidgets().get(0);
    widget.setRectangle(new PDRectangle(100, 100, 10, 10));
    widget.setAnnotationFlags(4);
    widget.setPage(page);
    //widget.setParent(checkBox);

    // inspired by annot 92 of file from PDFBOX-563
    // annot 172 has "checkmark" instead, but more more complex, needs ZaDb

    String offNString = "1 g\n"
            + "0 0 9.5 9.5 re\n"
            + "f\n"
            + "0.5 0.5 9.5 9.5 re\n"
            + "s";
    String offDString = "0.75 g\n"
            + "0 0 9.5 9.5 re\n"
            + "f\n"
            + "0.5 0.5 9.5 9.5 re\n"
            + "s";
    String yesNString = "1 g\n"
            + "0 0 9 9.5 re\n"
            + "f\n"
            + "0.5 0.5 9.5 9.5 re\n"
            + "s\n"
            + "q\n"
            + "  1 1 9 9 re\n"
            + "  W\n"
            + "  n\n"
            + "  2 8 m\n"
            + "  8 2 l\n"
            + "  8 8 m\n"
            + "  2 2 l\n"
            + "  s\n"
            + "Q";
    String yesDString = "0.75 g\n"
            + "0 0 9 9.5 re\n"
            + "f\n"
            + "0.5 0.5 9.5 9.5 re\n"
            + "s\n"
            + "q\n"
            + "  1 1 9 9 re\n"
            + "  W\n"
            + "  n\n"
            + "  2 8 m\n"
            + "  8 2 l\n"
            + "  8 8 m\n"
            + "  2 2 l\n"
            + "  s\n"
            + "Q";

    COSDictionary apNDict = new COSDictionary();
    COSStream offNStream = new COSStream();
    offNStream.setItem(COSName.BBOX, new PDRectangle(10, 10));
    offNStream.setItem(COSName.FORMTYPE, COSInteger.ONE);
    offNStream.setItem(COSName.TYPE, COSName.XOBJECT);
    offNStream.setItem(COSName.SUBTYPE, COSName.FORM);
    offNStream.setItem(COSName.MATRIX, new Matrix().toCOSArray());
    offNStream.setItem(COSName.RESOURCES, new COSDictionary());
    OutputStream os = offNStream.createOutputStream();
    os.write(offNString.getBytes());
    os.close();
    apNDict.setItem(COSName.Off, offNStream);

    COSStream yesNStream = new COSStream();
    yesNStream.setItem(COSName.BBOX, new PDRectangle(10, 10));
    yesNStream.setItem(COSName.FORMTYPE, COSInteger.ONE);
    yesNStream.setItem(COSName.TYPE, COSName.XOBJECT);
    yesNStream.setItem(COSName.SUBTYPE, COSName.FORM);
    yesNStream.setItem(COSName.MATRIX, new Matrix().toCOSArray());
    yesNStream.setItem(COSName.RESOURCES, new COSDictionary());
    os = yesNStream.createOutputStream();
    os.write(yesNString.getBytes());
    os.close();
    apNDict.setItem(COSName.getPDFName("Yes"), yesNStream);

    COSDictionary apDDict = new COSDictionary();
    COSStream offDStream = new COSStream();
    offDStream.setItem(COSName.BBOX, new PDRectangle(16, 16));
    offDStream.setItem(COSName.FORMTYPE, COSInteger.ONE);
    offDStream.setItem(COSName.TYPE, COSName.XOBJECT);
    offDStream.setItem(COSName.SUBTYPE, COSName.FORM);
    offDStream.setItem(COSName.MATRIX, new Matrix().toCOSArray());
    offDStream.setItem(COSName.RESOURCES, new COSDictionary());
    os = offDStream.createOutputStream();
    os.write(offDString.getBytes());
    os.close();
    apDDict.setItem(COSName.Off, offDStream);

    COSStream yesDStream = new COSStream();
    yesDStream.setItem(COSName.BBOX, new PDRectangle(16, 16));
    yesDStream.setItem(COSName.FORMTYPE, COSInteger.ONE);
    yesDStream.setItem(COSName.TYPE, COSName.XOBJECT);
    yesDStream.setItem(COSName.SUBTYPE, COSName.FORM);
    yesDStream.setItem(COSName.MATRIX, new Matrix().toCOSArray());
    yesDStream.setItem(COSName.RESOURCES, new COSDictionary());
    os = yesDStream.createOutputStream();
    os.write(yesDString.getBytes());
    os.close();
    apDDict.setItem(COSName.getPDFName("Yes"), yesDStream);

    PDAppearanceDictionary appearance = new PDAppearanceDictionary();
    PDAppearanceEntry appearanceNEntry = new PDAppearanceEntry(apNDict);
    appearance.setNormalAppearance(appearanceNEntry);

    // push appearance doesn't look nice enough, has weird effect when pushing
//        PDAppearanceEntry appearanceDEntry = new PDAppearanceEntry(apDDict);
//        appearance.setDownAppearance(appearanceDEntry);

    widget.setAppearance(appearance);

    // MK (appearance characteristics dictionary)
    COSDictionary acdDict = new COSDictionary();
    acdDict.setItem(COSName.CA, new COSString("8")); // 8 is X, 4 is checkmark
    COSArray bcArray = new COSArray();
    bcArray.add(COSInteger.ZERO);
    acdDict.setItem(COSName.BC, bcArray);
    COSArray bgArray = new COSArray();
    bgArray.add(COSInteger.ONE);
    acdDict.setItem(COSName.BG, bgArray);
    PDAppearanceCharacteristicsDictionary acd = new PDAppearanceCharacteristicsDictionary(acdDict);
    widget.setAppearanceCharacteristics(acd);

//        List<PDAnnotationWidget> widgets = new ArrayList<>();
//        widgets.add(widget);
    page.getAnnotations().add(widget);
//        checkBox.setWidgets(widgets);

    checkBox.setValue("Yes");

    acroForm.getFields().add(checkBox);
    document.getDocumentCatalog().setAcroForm(acroForm);

    File file = new File("checkbox.pdf");

    document.save(file);
    document.close();
}