RISEBY
PDF本身允许您嵌入(脚本的子集)。此嵌入式代码可以链接到文档事件(例如打开文档)或特定的表单元素(例如,单击按钮,更改文本输入字段中的文本)。这是他们网站上一个名为“制作PDF交互式”的页面,专注于添加表单元素。布鲁诺·洛瓦吉(iText的创始人)的书(iText在行动)也非常详细。它甚至展示了如何在PDF文档中编程计算器,第232页。我只是要在这里复制粘贴相关部分。清单 7.29 计算器public void addTextField(PdfWriter writer, Rectangle rect, String name) { PdfFormField field = PdfFormField.createTextField(writer, false, false, 0); field.setFieldName(name); field.setWidget(rect, PdfAnnotation.HIGHLIGHT_NONE); field.setQuadding(PdfFormField.Q_RIGHT); field.setFieldFlags(PdfFormField.FF_READ_ONLY); writer.addAnnotation(field);} public void addPushButton(PdfWriter writer, Rectangle rect, String btn, String script) { float w = rect.getWidth(); float h = rect.getHeight(); PdfFormField pushbutton = PdfFormField.createPushButton(writer); pushbutton.setFieldName("btn_" + btn); pushbutton.setWidget(rect, PdfAnnotation.HIGHLIGHT_PUSH); PdfContentByte cb = writer.getDirectContent(); pushbutton.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, createAppearance(cb, btn, BaseColor.GRAY, w, h)); pushbutton.setAppearance(PdfAnnotation.APPEARANCE_ROLLOVER, createAppearance(cb, btn, BaseColor.RED, w, h)); pushbutton.setAppearance(PdfAnnotation.APPEARANCE_DOWN, createAppearance(cb, btn, BaseColor.BLUE, w, h)); pushbutton.setAdditionalActions(PdfName.U, PdfAction.javaScript(script, writer)); pushbutton.setAdditionalActions(PdfName.E, PdfAction.javaScript( "this.showMove('" + btn + "');", writer)); pushbutton.setAdditionalActions(PdfName.X, PdfAction.javaScript( "this.showMove(' ');", writer)); writer.addAnnotation(pushbutton);}public PdfAppearance createAppearance(PdfContentByte cb, String btn, BaseColor color, float w, float h) { PdfAppearance app = cb.createAppearance(w, h); app.setColorFill(color); app.rectangle(2, 2, w - 4, h - 4); app.fill(); app.beginText(); app.setColorFill(BaseColor.BLACK); app.setFontAndSize(bf, h / 2); app.showTextAligned(Element.ALIGN_CENTER, btn, w / 2, h / 4, 0); app.endText(); return app;}