改进的答案,如OP自己的评论中所述,并且还包括后续问题的答案。
PDdocument doc = new PDdocument();PDPage page = new PDPage();doc.addPage(page);COSDictionary acroFormDict = new COSDictionary();PDAcroForm acroForm = new PDAcroForm(doc, acroFormDict);doc.getdocumentCatalog().setAcroForm(acroForm);acroForm.setFields(new ArrayList<>());PDPushButton button = new PDPushButton(acroForm);button.setPartialName("Btn1");PDActionJavascript actionJavascript = new PDActionJavascript("this.closeDoc();");PDAnnotationAdditionalActions additionalActions = new PDAnnotationAdditionalActions();additionalActions.setU(actionJavascript);// widgetPDAnnotationWidget widget = button.getWidgets().get(0);widget.setActions(additionalActions);widget.setRectangle(new PDRectangle(100, 700, 100, 50));PDColor colourBlack = new PDColor(new float[] { 0, 0, 0 }, PDDeviceRGB.INSTANCE);PDAppearanceCharacteristicsDictionary fieldAppearance = new PDAppearanceCharacteristicsDictionary(new COSDictionary());fieldAppearance.setBorderColour(colourBlack);widget.setAppearanceCharacteristics(fieldAppearance);// Create appearancePDAppearanceDictionary appearanceDictionary = new PDAppearanceDictionary();PDAppearanceStream appearanceStream = new PDAppearanceStream(doc);appearanceStream.setResources(new PDResources());try (PDPageContentStream cs = new PDPageContentStream(doc, appearanceStream)){ PDRectangle bbox = new PDRectangle(widget.getRectangle().getWidth(), widget.getRectangle().getHeight()); appearanceStream.setBBox(bbox); cs.setNonStrokingColor(0, 0, 0); // black cs.addRect(bbox.getLowerLeftX() + 0.5f, bbox.getLowerLeftY() + 0.5f, bbox.getWidth() - 1, bbox.getHeight() - 1); cs.stroke(); // put some useful text cs.setFont(PDType1Font.HELVETICA, 20); cs.beginText(); cs.newlineAtOffset(20, 20); cs.showText("Close"); cs.endText();}appearanceDictionary.setNormalAppearance(appearanceStream);widget.setAppearance(appearanceDictionary);page.getAnnotations().add(widget);acroForm.getFields().add(button);doc.save("..../Button.pdf");doc.close();


