List of usage examples for com.lowagie.text.pdf PdfName AA
PdfName AA
To view the source code for com.lowagie.text.pdf PdfName AA.
Click Source Link
From source file:classroom.newspaper_b.Newspaper09.java
public static void main(String[] args) { try {/* w w w. java 2 s .c o m*/ PdfReader reader = new PdfReader(NEWSPAPER); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT)); PdfAnnotation annotation1 = PdfAnnotation.createSquareCircle(stamper.getWriter(), new Rectangle(LLX1, LLY1, URX1, URY1), MESSAGE, true); annotation1.put(PdfName.T, new PdfString("Advertisement 1")); annotation1.put(PdfName.C, new PdfArray(new float[] { 1, 0, 0 })); stamper.addAnnotation(annotation1, 1); PdfAnnotation annotation2 = PdfAnnotation.createText(stamper.getWriter(), new Rectangle(LLX2, LLY2, URX2, URY2), "Advertisement 2", MESSAGE, false, null); annotation2.put(PdfName.NM, new PdfString("ad2")); // the text must be read only, and the annotation set to NOVIEW annotation2.put(PdfName.F, new PdfNumber(PdfAnnotation.FLAGS_READONLY | PdfAnnotation.FLAGS_NOVIEW)); // we create a popup annotation that will define where the rectangle will appear PdfAnnotation popup = PdfAnnotation.createPopup(stamper.getWriter(), new Rectangle(LLX2 + 50, LLY2 + 120, URX2 - 80, URY2 - 120), null, false); // we add a reference to the text annotation to the popup annotation popup.put(PdfName.PARENT, annotation2.getIndirectReference()); // we add a reference to the popup annotation to the text annotation annotation2.put(PdfName.POPUP, popup.getIndirectReference()); // we add both annotations to the writer stamper.addAnnotation(annotation2, 1); stamper.addAnnotation(popup, 1); // the text annotation can't be viewed (it's invisible) // we create a widget annotation named mywidget (it's a button field) PushbuttonField field = new PushbuttonField(stamper.getWriter(), new Rectangle(LLX2, LLY2, URX2, URY2), "button"); PdfAnnotation widget = field.getField(); PdfDictionary dict = new PdfDictionary(); // we write some javascript that makes the popup of the text annotation visible/invisible on mouse enter/exit String js1 = "var t = this.getAnnot(this.pageNum, 'ad2'); t.popupOpen = true; var w = this.getField('button'); w.setFocus();"; PdfAction enter = PdfAction.javaScript(js1, stamper.getWriter()); dict.put(PdfName.E, enter); String js2 = "var t = this.getAnnot(this.pageNum, 'ad2'); t.popupOpen = false;"; PdfAction exit = PdfAction.javaScript(js2, stamper.getWriter()); dict.put(PdfName.X, exit); // we add the javascript as additional action widget.put(PdfName.AA, dict); // we add the button field stamper.addAnnotation(widget, 1); stamper.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } }
From source file:questions.forms.AddActionToField.java
public static void main(String[] args) { try {//from w w w. ja v a2s . co m PdfReader reader = new PdfReader(RESOURCE); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT)); AcroFields form = stamper.getAcroFields(); Item fd = form.getFieldItem("Who"); PdfDictionary dict = (PdfDictionary) PdfReader.getPdfObject((PdfObject) fd.getWidgetRef(0)); PdfDictionary aa = dict.getAsDict(PdfName.AA); if (aa == null) aa = new PdfDictionary(); aa.put(new PdfName("Fo"), PdfAction.javaScript("app.alert('Who has got the focus!?');", stamper.getWriter())); dict.put(PdfName.AA, aa); stamper.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } }
From source file:questions.javascript.AddJavaScriptToForm.java
public static void addJavaScript(String input, String output) throws IOException, DocumentException { PdfReader reader = new PdfReader(input); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(output)); stamper.getWriter()/*from w w w. j a v a2 s .c o m*/ .addJavaScript("function setReadOnly(readonly) {" + "var partner = this.getField('partner');" + "if(readonly) {" + "partner.value = '';" + "}" + "partner.readonly = readonly;" + " }" + "function validate() {" + "var married = this.getField('married');" + "var partner = this.getField('partner');" + "if (married.value == 'yes' && partner.value == '') {" + "app.alert('please enter the name of your partner');" + "}" + "else {" + "this.submitForm({" + " cURL:\"http://1t3xt.info/examples/request.php\"," + " cSubmitAs: \"HTML\"" + "});" + "}" + " }"); AcroFields form = stamper.getAcroFields(); Item fd = form.getFieldItem("married"); PdfDictionary dictYes = (PdfDictionary) PdfReader.getPdfObject((PdfObject) fd.getWidgetRef(0)); PdfDictionary yesAction = dictYes.getAsDict(PdfName.AA); if (yesAction == null) yesAction = new PdfDictionary(); yesAction.put(new PdfName("Fo"), PdfAction.javaScript("setReadOnly(false);", stamper.getWriter())); dictYes.put(PdfName.AA, yesAction); PdfDictionary dictNo = (PdfDictionary) PdfReader.getPdfObject((PdfObject) fd.getWidgetRef(1)); PdfDictionary noAction = dictNo.getAsDict(PdfName.AA); if (noAction == null) noAction = new PdfDictionary(); noAction.put(new PdfName("Fo"), PdfAction.javaScript("setReadOnly(true);", stamper.getWriter())); dictNo.put(PdfName.AA, noAction); PdfWriter writer = stamper.getWriter(); PushbuttonField button = new PushbuttonField(writer, new Rectangle(40, 690, 200, 710), "submit"); button.setText("validate and submit"); button.setOptions(PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT); PdfFormField validateAndSubmit = button.getField(); validateAndSubmit.setAction(PdfAction.javaScript("validate();", stamper.getWriter())); stamper.addAnnotation(validateAndSubmit, 1); stamper.close(); }