栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

PDFBox按钮执行JavaScript以关闭文档

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PDFBox按钮执行JavaScript以关闭文档

改进的答案,如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();


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/570841.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号