Example usage for com.lowagie.text.pdf PdfAction javaScript

List of usage examples for com.lowagie.text.pdf PdfAction javaScript

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfAction javaScript.

Prototype

public static PdfAction javaScript(String code, PdfWriter writer) 

Source Link

Document

Creates a JavaScript action.

Usage

From source file:classroom.newspaper_b.Newspaper09.java

public static void main(String[] args) {
    try {/*from w  w w  .  j av  a2  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:classroom.newspaper_b.Newspaper10.java

public static void main(String[] args) {
    try {//from w ww.ja  va 2 s  .com
        PdfReader reader = new PdfReader(NEWSPAPER);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        PushbuttonField button;
        Rectangle rect;

        rect = new Rectangle(100, 980, 700, 1000);
        button = new PushbuttonField(stamper.getWriter(), rect, "click");
        button.setBackgroundColor(Color.ORANGE);
        button.setText("Click here to close window");
        button.setLayout(PushbuttonField.LAYOUT_LABEL_ONLY);
        button.setAlignment(Element.ALIGN_RIGHT);
        PdfFormField menubar = button.getField();
        String js = "var f1 = getField('click'); f1.display = display.hidden;"
                + "var f2 = getField('advertisement'); f2.display = display.hidden;";
        menubar.setAction(PdfAction.javaScript(js, stamper.getWriter()));
        stamper.addAnnotation(menubar, 1);

        rect = new Rectangle(100, 500, 700, 980);
        button = new PushbuttonField(stamper.getWriter(), rect, "advertisement");
        button.setBackgroundColor(Color.WHITE);
        button.setBorderColor(Color.ORANGE);
        button.setImage(Image.getInstance(IMG));
        button.setText("Buy the book iText in Action");
        button.setLayout(PushbuttonField.LAYOUT_LABEL_TOP_ICON_BOTTOM);
        PdfFormField advertisement = button.getField();
        advertisement.setAction(new PdfAction("http://www.1t3xt.com/docs/book.php"));
        stamper.addAnnotation(advertisement, 1);
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

From source file:com.dlya.facturews.DlyaPdfExporter2.java

License:Open Source License

/**
 *
 *//*from  w  w  w  . j a  v a2s  . co m*/
protected void setHyperlinkInfo(Chunk chunk, JRPrintHyperlink link) {
    if (link != null) {
        switch (link.getHyperlinkTypeValue()) {
        case REFERENCE: {
            if (link.getHyperlinkReference() != null) {
                switch (link.getHyperlinkTargetValue()) {
                case BLANK: {
                    chunk.setAction(PdfAction.javaScript("if (app.viewerVersion < 7)" + "{this.getURL(\""
                            + link.getHyperlinkReference() + "\");}" + "else {app.launchURL(\""
                            + link.getHyperlinkReference() + "\", true);};", pdfWriter));
                    break;
                }
                case SELF:
                default: {
                    chunk.setAnchor(link.getHyperlinkReference());
                    break;
                }
                }
            }
            break;
        }
        case LOCAL_ANCHOR: {
            if (link.getHyperlinkAnchor() != null) {
                chunk.setLocalGoto(link.getHyperlinkAnchor());
            }
            break;
        }
        case LOCAL_PAGE: {
            if (link.getHyperlinkPage() != null) {
                chunk.setLocalGoto(
                        JR_PAGE_ANCHOR_PREFIX + reportIndex + "_" + link.getHyperlinkPage().toString());
            }
            break;
        }
        case REMOTE_ANCHOR: {
            if (link.getHyperlinkReference() != null && link.getHyperlinkAnchor() != null) {
                chunk.setRemoteGoto(link.getHyperlinkReference(), link.getHyperlinkAnchor());
            }
            break;
        }
        case REMOTE_PAGE: {
            if (link.getHyperlinkReference() != null && link.getHyperlinkPage() != null) {
                chunk.setRemoteGoto(link.getHyperlinkReference(), link.getHyperlinkPage().intValue());
            }
            break;
        }
        case CUSTOM: {
            if (hyperlinkProducerFactory != null) {
                String hyperlink = hyperlinkProducerFactory.produceHyperlink(link);
                if (hyperlink != null) {
                    switch (link.getHyperlinkTargetValue()) {
                    case BLANK: {
                        chunk.setAction(
                                PdfAction.javaScript(
                                        "if (app.viewerVersion < 7)" + "{this.getURL(\"" + hyperlink + "\");}"
                                                + "else {app.launchURL(\"" + hyperlink + "\", true);};",
                                        pdfWriter));
                        break;
                    }
                    case SELF:
                    default: {
                        chunk.setAnchor(hyperlink);
                        break;
                    }
                    }
                }
            }
        }
        case NONE:
        default: {
            break;
        }
        }
    }
}

From source file:com.geek.tutorial.itext.acroform.ListFieldForm.java

License:Open Source License

public ListFieldForm() throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("ListFieldForm.pdf"));
    document.open();// w w  w .  ja va  2s.  c o m

    // Code 1
    String options[] = { "PS3", "XBOX 360", "Wii", "PSP", "NDS", "GBA" };

    // Code 2 create drop-down list
    PdfFormField dropDown = PdfFormField.createCombo(writer, true, options, 0);
    dropDown.setWidget(new Rectangle(50, 785, 120, 800), PdfAnnotation.HIGHLIGHT_INVERT);
    dropDown.setFieldName("dropDownList");
    dropDown.setValueAsString("PS3");
    dropDown.setMKBorderColor(Color.BLACK);
    writer.addAnnotation(dropDown);

    // Code 3 create scrollable list
    TextField scrollableList = new TextField(writer, new Rectangle(150, 740, 250, 800), "scrollableList");
    scrollableList.setBackgroundColor(Color.WHITE);
    scrollableList.setBorderColor(Color.BLUE);
    scrollableList.setBorderWidth(2);
    scrollableList.setBorderStyle(PdfBorderDictionary.STYLE_SOLID);
    scrollableList.setFontSize(10);
    scrollableList.setChoices(options);
    scrollableList.setChoiceSelection(0);
    writer.addAnnotation(scrollableList.getListField());

    // Code 4 add function and button for showing state 
    writer.addJavaScript(
            "function showState(){" + "app.alert('DropDown:'+ this.getField('dropDownList').value +'\\n'+"
                    + "'Scrollable List:'+this.getField('scrollableList').value);" + "}");
    PushbuttonField push = new PushbuttonField(writer, new Rectangle(70, 710, 140, 730), "pushAction");
    push.setBackgroundColor(Color.LIGHT_GRAY);
    push.setBorderColor(Color.GRAY);
    push.setText("Show State");
    push.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED);
    push.setTextColor(Color.BLACK);
    PdfFormField pushbutton = push.getField();
    pushbutton.setAction(PdfAction.javaScript("showState()", writer));
    writer.addAnnotation(pushbutton);

    document.close();
}

From source file:com.geek.tutorial.itext.acroform.RadioCheckBoxForm.java

License:Open Source License

public RadioCheckBoxForm() throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("RadioCheckBoxForm.pdf"));
    document.open();/* w  w w  . java2  s .  c  o  m*/
    PdfContentByte cb = writer.getDirectContent();

    BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    Rectangle rect;

    // Code 1 create radio button
    String[] radios = { "Radio1", "Radio2", "Radio3" };
    PdfFormField radioField = PdfFormField.createRadioButton(writer, true);
    radioField.setFieldName("radioField");
    radioField.setValueAsName(radios[0]);
    for (int i = 0; i < radios.length; i++) {
        rect = new Rectangle(40, 815 - i * 30, 60, 797 - i * 30);
        addRadioButton(writer, rect, radioField, radios[i], i == 0);
        cb.beginText();
        cb.setFontAndSize(bf, 12);
        cb.showTextAligned(Element.ALIGN_LEFT, radios[i], 70, 802 - i * 30, 0);
        cb.endText();
    }
    writer.addAnnotation(radioField);

    // Code 2 create checkbox button
    String[] options = { "Check1", "Check2", "Check3" };
    for (int i = 0; i < options.length; i++) {
        rect = new Rectangle(160, 815 - i * 30, 180, 797 - i * 30);
        addCheckbox(writer, rect, options[i]);
        cb.beginText();
        cb.setFontAndSize(bf, 12);
        cb.showTextAligned(Element.ALIGN_LEFT, options[i], 190, 802 - i * 30, 0);
        cb.endText();
    }

    // Code 3 add function and button for showing state 
    writer.addJavaScript(
            "function showState(){" + "app.alert('Radio:'+ this.getField('radioField').value +'\\n\\n'+"
                    + "'Check1:'+this.getField('Check1').value +'\\n'+"
                    + "'Check2:'+this.getField('Check2').value +'\\n'+"
                    + "'Check3:'+this.getField('Check3').value);" + "}");
    PushbuttonField push = new PushbuttonField(writer, new Rectangle(80, 710, 150, 730), "pushAction");
    push.setBackgroundColor(Color.LIGHT_GRAY);
    push.setBorderColor(Color.GRAY);
    push.setText("Show State");
    push.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED);
    push.setTextColor(Color.BLACK);
    PdfFormField pushbutton = push.getField();
    pushbutton.setAction(PdfAction.javaScript("showState()", writer));
    writer.addAnnotation(pushbutton);

    document.close();
}

From source file:net.sf.jasperreports.engine.export.JRPdfExporter.java

License:LGPL

/**
 *
 */// w  ww  .j  av a  2s  .  c  o  m
protected void setHyperlinkInfo(Chunk chunk, JRPrintHyperlink link) {
    switch (link.getHyperlinkType()) {
    case JRHyperlink.HYPERLINK_TYPE_REFERENCE: {
        if (link.getHyperlinkReference() != null) {
            switch (link.getHyperlinkTarget()) {
            case JRHyperlink.HYPERLINK_TARGET_BLANK: {
                chunk.setAction(PdfAction.javaScript("if (app.viewerVersion < 7)" + "{this.getURL(\""
                        + link.getHyperlinkReference() + "\");}" + "else {app.launchURL(\""
                        + link.getHyperlinkReference() + "\", true);};", pdfWriter));
                break;
            }
            case JRHyperlink.HYPERLINK_TARGET_SELF:
            default: {
                chunk.setAnchor(link.getHyperlinkReference());
                break;
            }
            }
        }
        break;
    }
    case JRHyperlink.HYPERLINK_TYPE_LOCAL_ANCHOR: {
        if (link.getHyperlinkAnchor() != null) {
            chunk.setLocalGoto(link.getHyperlinkAnchor());
        }
        break;
    }
    case JRHyperlink.HYPERLINK_TYPE_LOCAL_PAGE: {
        if (link.getHyperlinkPage() != null) {
            chunk.setLocalGoto(JR_PAGE_ANCHOR_PREFIX + reportIndex + "_" + link.getHyperlinkPage().toString());
        }
        break;
    }
    case JRHyperlink.HYPERLINK_TYPE_REMOTE_ANCHOR: {
        if (link.getHyperlinkReference() != null && link.getHyperlinkAnchor() != null) {
            chunk.setRemoteGoto(link.getHyperlinkReference(), link.getHyperlinkAnchor());
        }
        break;
    }
    case JRHyperlink.HYPERLINK_TYPE_REMOTE_PAGE: {
        if (link.getHyperlinkReference() != null && link.getHyperlinkPage() != null) {
            chunk.setRemoteGoto(link.getHyperlinkReference(), link.getHyperlinkPage().intValue());
        }
        break;
    }
    case JRHyperlink.HYPERLINK_TYPE_CUSTOM: {
        if (hyperlinkProducerFactory != null) {
            String hyperlink = hyperlinkProducerFactory.produceHyperlink(link);
            if (hyperlink != null) {
                switch (link.getHyperlinkTarget()) {
                case JRHyperlink.HYPERLINK_TARGET_BLANK: {
                    chunk.setAction(
                            PdfAction.javaScript(
                                    "if (app.viewerVersion < 7)" + "{this.getURL(\"" + hyperlink + "\");}"
                                            + "else {app.launchURL(\"" + hyperlink + "\", true);};",
                                    pdfWriter));
                    break;
                }
                case JRHyperlink.HYPERLINK_TARGET_SELF:
                default: {
                    chunk.setAnchor(hyperlink);
                    break;
                }
                }
            }
        }
    }
    case JRHyperlink.HYPERLINK_TYPE_NONE:
    default: {
        break;
    }
    }
}

From source file:org.xhtmlrenderer.pdf.ITextOutputDevice.java

License:Open Source License

private void processLink(RenderingContext c, Box box) {
    Element elem = box.getElement();
    if (elem != null) {
        NamespaceHandler handler = _sharedContext.getNamespaceHandler();
        String uri = handler.getLinkUri(elem);
        if (uri != null) {
            if (uri.length() > 1 && uri.charAt(0) == '#') {
                String anchor = uri.substring(1);
                Box target = _sharedContext.getBoxById(anchor);
                if (target != null) {
                    PdfDestination dest = createDestination(c, target);

                    if (dest != null) {
                        PdfAction action = new PdfAction();
                        if (!"".equals(handler.getAttributeValue(elem, "onclick"))) {
                            action = PdfAction.javaScript(handler.getAttributeValue(elem, "onclick"), _writer);
                        } else {
                            action.put(PdfName.S, PdfName.GOTO);
                            action.put(PdfName.D, dest);
                        }/* w ww.j a v a  2s.  c o m*/

                        com.lowagie.text.Rectangle targetArea = checkLinkArea(c, box);
                        if (targetArea == null) {
                            return;
                        }

                        targetArea.setBorder(0);
                        targetArea.setBorderWidth(0);

                        PdfAnnotation annot = new PdfAnnotation(_writer, targetArea.getLeft(),
                                targetArea.getBottom(), targetArea.getRight(), targetArea.getTop(), action);
                        annot.put(PdfName.SUBTYPE, PdfName.LINK);
                        annot.setBorderStyle(new PdfBorderDictionary(0.0f, 0));
                        annot.setBorder(new PdfBorderArray(0.0f, 0.0f, 0));
                        _writer.addAnnotation(annot);
                    }
                }
            } else if (uri.indexOf("://") != -1) {
                PdfAction action = new PdfAction(uri);

                com.lowagie.text.Rectangle targetArea = checkLinkArea(c, box);
                if (targetArea == null) {
                    return;
                }
                PdfAnnotation annot = new PdfAnnotation(_writer, targetArea.getLeft(), targetArea.getBottom(),
                        targetArea.getRight(), targetArea.getTop(), action);
                annot.put(PdfName.SUBTYPE, PdfName.LINK);

                annot.setBorderStyle(new PdfBorderDictionary(0.0f, 0));
                annot.setBorder(new PdfBorderArray(0.0f, 0.0f, 0));
                _writer.addAnnotation(annot);
            }
        }
    }
}

From source file:questions.forms.AddActionToField.java

public static void main(String[] args) {
    try {//from   w  ww  . j  a v a  2s. c o 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.  java2  s .  co 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();
}

From source file:questions.javascript.TriggerMenuButtons.java

public static final void main(String[] args) {
    Document document = new Document();
    try {/*from  www.  ja  v a  2s .c  o m*/
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();
        document.add(new Paragraph("Triggering Menu Items"));
        PushbuttonField saveAs = new PushbuttonField(writer, new Rectangle(40, 760, 100, 780), "Save");
        saveAs.setBorderColor(Color.BLACK);
        saveAs.setText("Save");
        saveAs.setTextColor(Color.RED);
        saveAs.setLayout(PushbuttonField.LAYOUT_LABEL_ONLY);
        PdfFormField saveAsButton = saveAs.getField();
        saveAsButton.setAction(PdfAction.javaScript("app.execMenuItem('SaveAs')", writer));
        writer.addAnnotation(saveAsButton);
        PushbuttonField mail = new PushbuttonField(writer, new Rectangle(120, 760, 180, 780), "Mail");
        mail.setBorderColor(Color.BLACK);
        mail.setText("Mail");
        mail.setTextColor(Color.RED);
        mail.setLayout(PushbuttonField.LAYOUT_LABEL_ONLY);
        PdfFormField mailButton = mail.getField();
        mailButton.setAction(PdfAction.javaScript("app.execMenuItem('AcroSendMail:SendMail')", writer));
        writer.addAnnotation(mailButton);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
    document.close();
}