Example usage for com.lowagie.text.pdf PdfWriter addAnnotation

List of usage examples for com.lowagie.text.pdf PdfWriter addAnnotation

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfWriter addAnnotation.

Prototype

public void addAnnotation(PdfAnnotation annot) 

Source Link

Usage

From source file:at.reppeitsolutions.formbuilder.components.pdf.itext.ITextCheckbox.java

License:Open Source License

@Override
public void cellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) {
    PdfWriter writer = canvases[0].getPdfWriter();
    PdfAppearance[] onOff = new PdfAppearance[2];
    onOff[0] = canvases[0].createAppearance(20, 20);
    onOff[0].rectangle(1, 1, 18, 18);//from   w w w .j av  a 2s. c o  m
    onOff[0].stroke();
    onOff[1] = canvases[0].createAppearance(20, 20);
    onOff[1].setRGBColorFill(255, 128, 128);
    onOff[1].rectangle(1, 1, 18, 18);
    onOff[1].fillStroke();
    onOff[1].moveTo(1, 1);
    onOff[1].lineTo(19, 19);
    onOff[1].moveTo(1, 19);
    onOff[1].lineTo(19, 1);
    onOff[1].stroke();
    RadioCheckField checkbox;
    Font f = new Font();
    f.setSize(ITextInputText.FONTSIZE);
    for (int i = 0; i < values.length; i++) {
        try {
            Rectangle rect = ITextRadio.getBoxRectangle(rectangle, i);
            checkbox = new RadioCheckField(writer, rect, UUID.randomUUID().toString(), "Yes");
            boolean checked = false;
            if (selectedValues != null) {
                for (int i2 = 0; i2 < selectedValues.length; i2++) {
                    if (values[i].equals(selectedValues[i2])) {
                        checked = true;
                        break;
                    }
                }
            }
            checkbox.setChecked(checked);
            PdfFormField field = checkbox.getCheckField();
            field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", onOff[0]);
            field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Yes", onOff[1]);
            if (locked) {
                field.setFieldFlags(BaseField.READ_ONLY);
            }
            writer.addAnnotation(field);
            ITextRadio.addBoxDescription(rectangle, i, values, canvases);
        } catch (IOException ex) {
            Logger.getLogger(ITextCheckbox.class.getName()).log(Level.SEVERE, null, ex);
        } catch (DocumentException ex) {
            Logger.getLogger(ITextCheckbox.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:at.reppeitsolutions.formbuilder.components.pdf.itext.ITextInputText.java

License:Open Source License

@Override
public void cellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) {
    PdfWriter writer = canvases[0].getPdfWriter();
    TextField text = new TextField(writer, rectangle, String.format("text_" + UUID.randomUUID().toString()));
    text.setBorderStyle(PdfBorderDictionary.STYLE_INSET);
    text.setText(value);/*from   w  ww. j  av  a  2s. com*/
    text.setFontSize(FONTSIZE);
    text.setAlignment(Element.ALIGN_LEFT);
    try {
        PdfFormField field = text.getTextField();
        if (locked) {
            field.setFieldFlags(BaseField.READ_ONLY);
        }
        writer.addAnnotation(field);
    } catch (IOException ioe) {
        throw new ExceptionConverter(ioe);
    } catch (DocumentException de) {
        throw new ExceptionConverter(de);
    }
}

From source file:at.reppeitsolutions.formbuilder.components.pdf.itext.ITextListbox.java

License:Open Source License

@Override
public void cellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) {
    PdfWriter writer = canvases[0].getPdfWriter();
    TextField text = new TextField(writer, rectangle, String.format("choice_" + UUID.randomUUID().toString()));
    text.setBorderStyle(PdfBorderDictionary.STYLE_INSET);
    text.setFontSize(ITextInputText.FONTSIZE);
    text.setOptions(TextField.MULTISELECT);
    text.setChoices(values);//from  ww w . j a v a 2s  . c o  m
    ArrayList<Integer> choiceSelections = new ArrayList<>();
    if (selectedValues != null) {
        for (int i = 0; i < values.length; i++) {
            for (int i2 = 0; i2 < selectedValues.length; i2++) {
                if (values[i].equals(selectedValues[i2])) {
                    choiceSelections.add(i);
                    break;
                }
            }
        }
    }
    text.setChoiceSelections(choiceSelections);
    try {
        PdfFormField listField = text.getListField();
        if (locked) {
            listField.setFieldFlags(BaseField.READ_ONLY);
        }
        writer.addAnnotation(listField);
    } catch (IOException ex) {
        Logger.getLogger(ITextListbox.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(ITextListbox.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:at.reppeitsolutions.formbuilder.components.pdf.itext.ITextRadio.java

License:Open Source License

@Override
public void cellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) {
    PdfWriter writer = canvases[0].getPdfWriter();
    PdfFormField radiogroup = PdfFormField.createRadioButton(writer, true);
    radiogroup.setFieldName(UUID.randomUUID().toString());
    if (locked) {
        radiogroup.setFieldFlags(BaseField.READ_ONLY);
    }// ww w  .ja va  2s. com
    RadioCheckField radio;
    for (int i = 0; i < values.length; i++) {
        try {
            Rectangle rect = getBoxRectangle(rectangle, i);
            radio = new RadioCheckField(writer, rect, null, UUID.randomUUID().toString());
            radio.setBorderColor(GrayColor.GRAYBLACK);
            radio.setBackgroundColor(GrayColor.GRAYWHITE);
            radio.setCheckType(RadioCheckField.TYPE_CIRCLE);
            if (value != null && values[i].equals(value)) {
                radio.setChecked(true);
            }
            PdfFormField field = radio.getRadioField();
            radiogroup.addKid(field);
            addBoxDescription(rectangle, i, values, canvases);
        } catch (IOException ex) {
            Logger.getLogger(ITextRadio.class.getName()).log(Level.SEVERE, null, ex);
        } catch (DocumentException ex) {
            Logger.getLogger(ITextRadio.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    writer.addAnnotation(radiogroup);
}

From source file:at.reppeitsolutions.formbuilder.components.pdf.itext.ITextSelect.java

License:Open Source License

@Override
public void cellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) {
    PdfWriter writer = canvases[0].getPdfWriter();
    TextField text = new TextField(writer, rectangle, String.format("choice_" + UUID.randomUUID().toString()));
    text.setBorderStyle(PdfBorderDictionary.STYLE_INSET);
    text.setChoices(values);/*  w  w w .  j  a  va2  s . c  om*/
    text.setFontSize(ITextInputText.FONTSIZE);
    text.setChoiceExports(text.getChoices());
    for (int i = 0; value != null && i < values.length; i++) {
        if (values[i].equals(value)) {
            text.setChoiceSelection(i);
            break;
        }
    }
    try {
        PdfFormField comboField = text.getComboField();
        if (locked) {
            comboField.setFieldFlags(BaseField.READ_ONLY);
        }
        writer.addAnnotation(comboField);
    } catch (IOException ex) {
        Logger.getLogger(ITextSelect.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(ITextSelect.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:buckley.compile.Compiler.java

License:Apache License

public void compile(InputStream pdfTemplate, OutputStream output, final buckley.Document doc) {
    final Pdf pdf = new Pdf(pdfTemplate, output);
    try {/* www  .  ja  v  a 2 s  .  c  o m*/
        pdf.eachPage(new Pdf.PageHandler() {
            public void handlePage(int number, PdfReader reader, PdfWriter writer, Document document,
                    PdfAcroForm form) {
                PdfContentByte content = writer.getDirectContent();
                PdfImportedPage importedPage = writer.getImportedPage(reader, number);
                content.addTemplate(importedPage, 0.0f, 0.0f);
                if (doc.hasFields(number)) {
                    Page page = doc.getPage(number);
                    for (Field field : page.getFields()) {
                        Rectangle fieldSize = fieldSizeFactory.build(field);
                        ITextFieldFactory factory = fieldFactories.get(field.getClass());
                        BaseField iTextField = factory.build(writer, fieldSize, field.getName());
                        for (FieldAttributeModifier modifier : modifiers) {
                            if (modifier.canModify(field)) {
                                modifier.modify(iTextField, field, doc);
                            }
                        }
                        try {
                            writer.addAnnotation(factory.buildFormField(iTextField));
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
                document.newPage();
            }
        });
    } finally {
        pdf.close();
    }
}

From source file:classroom.filmfestival_c.Movies22.java

public static void main(String[] args) {
    // step 1//from  www  .  j a va  2  s. c  o  m
    Document document = new Document(new Rectangle(WIDTH, HEIGHT));
    try {
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(FORM));
        writer.setViewerPreferences(PdfWriter.PageLayoutSinglePage);
        // step 3
        document.open();
        // step 4
        PdfContentByte cb = writer.getDirectContent();

        // top part of the card
        cb.setColorFill(RED);
        cb.rectangle(0, mm2pt(57f), WIDTH, HEIGHT - mm2pt(57f));
        cb.fill();
        cb.setColorFill(BLACK);
        cb.rectangle(0, mm2pt(61), WIDTH, mm2pt(21f));
        cb.fill();
        cb.setColorFill(WHITE);

        Image kubrick = Image.getInstance(KUBRICK);
        kubrick.scaleToFit(WIDTH, mm2pt(21));
        kubrick.setAbsolutePosition(0, mm2pt(61));
        cb.addImage(kubrick);

        Image logo = Image.getInstance(LOGO);
        logo.scaleToFit(mm2pt(14), mm2pt(14));
        logo.setAbsolutePosition(mm2pt(37), mm2pt(65));
        cb.addImage(logo);

        cb.beginText();
        cb.setFontAndSize(FONT, 7);
        cb.showTextAligned(Element.ALIGN_CENTER, "Flanders International Film Festival-Ghent", WIDTH / 2,
                mm2pt(58.5f), 0);
        cb.endText();

        // bottom part of the card

        TextField filmfestival = new TextField(writer, new Rectangle(0, 0, WIDTH, mm2pt(9)), "filmfestival");
        filmfestival.setBackgroundColor(GRAY);
        filmfestival.setTextColor(WHITE);
        filmfestival.setText("www.filmfestival.be");
        filmfestival.setFontSize(14);
        filmfestival.setOptions(TextField.READ_ONLY);
        filmfestival.setAlignment(Element.ALIGN_CENTER);
        writer.addAnnotation(filmfestival.getTextField());

        cb.beginText();
        cb.moveText(mm2pt(1.5f), mm2pt(12));
        cb.setFontAndSize(FONT, 21);
        cb.setColorFill(RED);
        cb.showText("10");
        cb.setColorFill(BLUE);
        cb.showText("/");
        cb.setColorFill(RED);
        cb.showText("21");
        cb.setColorFill(BLUE);
        cb.showText("OCT");
        cb.setColorFill(GREEN);
        cb.showText("2006");
        cb.endText();
        cb.setColorStroke(BLUE);
        cb.moveTo(mm2pt(24.5f), mm2pt(29));
        cb.lineTo(mm2pt(24.5f), mm2pt(36));
        cb.moveTo(mm2pt(24.5f), mm2pt(48));
        cb.lineTo(mm2pt(24.5f), mm2pt(54));
        cb.stroke();

        // central part of the card
        PushbuttonField photo = new PushbuttonField(writer,
                new Rectangle(mm2pt(3), mm2pt(29), mm2pt(24), mm2pt(54)), "photo");
        PdfTemplate t1 = cb.createTemplate(mm2pt(21), mm2pt(25));
        t1.setColorFill(GRAY);
        t1.rectangle(0, 0, mm2pt(21), mm2pt(25));
        t1.fill();
        photo.setTemplate(t1);
        photo.setLayout(PushbuttonField.LAYOUT_ICON_ONLY);
        writer.addAnnotation(photo.getField());

        TextField type = new TextField(writer,
                new Rectangle(mm2pt(26), mm2pt(46), WIDTH - mm2pt(1.5f), mm2pt(54)), "type");
        type.setTextColor(GRAY);
        type.setText("TYPE");
        type.setFontSize(0);
        writer.addAnnotation(type.getTextField());

        TextField number = new TextField(writer,
                new Rectangle(mm2pt(26), mm2pt(44), WIDTH - mm2pt(1.5f), mm2pt(48)), "number");
        number.setText("N\u00b0 0000000");
        number.setFontSize(8);
        writer.addAnnotation(number.getTextField());

        TextField name = new TextField(writer,
                new Rectangle(mm2pt(26), mm2pt(28), WIDTH - mm2pt(1.5f), mm2pt(40)), "name");
        name.setText("Name");
        name.setFontSize(8);
        name.setOptions(TextField.MULTILINE);
        writer.addAnnotation(name.getTextField());

        PushbuttonField barcode = new PushbuttonField(writer,
                new Rectangle(mm2pt(3), mm2pt(23), WIDTH - mm2pt(3), mm2pt(28)), "barcode");
        PdfTemplate t2 = cb.createTemplate(WIDTH - mm2pt(6), mm2pt(5));
        t2.setColorFill(GRAY);
        t2.rectangle(0, 0, WIDTH - mm2pt(6), mm2pt(5));
        t2.fill();
        barcode.setTemplate(t2);
        barcode.setLayout(PushbuttonField.LAYOUT_ICON_ONLY);
        writer.addAnnotation(barcode.getField());

    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // step 4
    document.close();
}

From source file:classroom.filmfestival_c.Movies25.java

public static void createTemplate() {
    // our template will be 35 x 50 mm
    Document document = new Document(
            new Rectangle(Utilities.millimetersToPoints(35), Utilities.millimetersToPoints(50)));
    try {/*from   ww  w  . j  a  va2  s. co  m*/
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(TEMPLATE));
        writer.setViewerPreferences(PdfWriter.PageLayoutSinglePage);
        // step 3
        document.open();
        // step 4

        // space reserved for the film poster
        PushbuttonField foto = new PushbuttonField(writer,
                new Rectangle(Utilities.millimetersToPoints(0), Utilities.millimetersToPoints(25),
                        Utilities.millimetersToPoints(35), Utilities.millimetersToPoints(50)),
                POSTER);
        foto.setBackgroundColor(Color.BLUE);
        writer.addAnnotation(foto.getField());

        // space reserved for the text
        TextField tekst = new TextField(writer,
                new Rectangle(Utilities.millimetersToPoints(0), Utilities.millimetersToPoints(7),
                        Utilities.millimetersToPoints(35), Utilities.millimetersToPoints(25)),
                TEXT);
        tekst.setOptions(TextField.MULTILINE);
        writer.addAnnotation(tekst.getTextField());

        // space reserved for the year
        TextField prijs = new TextField(writer,
                new Rectangle(Utilities.millimetersToPoints(0), Utilities.millimetersToPoints(0),
                        Utilities.millimetersToPoints(35), Utilities.millimetersToPoints(7)),
                YEAR);
        prijs.setAlignment(Element.ALIGN_CENTER);
        prijs.setBackgroundColor(Color.BLUE);
        prijs.setTextColor(Color.WHITE);
        writer.addAnnotation(prijs.getTextField());
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
    // step 5
    document.close();
}

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();//from  w  w  w  . jav  a  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();/*from w w w  . ja  va 2s  . co  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();
}