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:org.xhtmlrenderer.pdf.TextFormField.java

License:Open Source License

public void paint(RenderingContext c, ITextOutputDevice outputDevice, BlockBox box) {

    PdfWriter writer = outputDevice.getWriter();

    Element elem = box.getElement();

    Rectangle targetArea = outputDevice.createLocalTargetArea(c, box);
    TextField field = new TextField(writer, targetArea, getFieldName(outputDevice, elem));

    String value = getValue(elem);
    field.setText(value);/* w  ww. jav  a2  s  .c  o m*/

    try {
        PdfFormField formField = field.getTextField();
        createAppearance(c, outputDevice, box, formField, value);
        //TODO add max length back in
        if (isReadOnly(elem)) {
            formField.setFieldFlags(PdfFormField.FF_READ_ONLY);
        }
        writer.addAnnotation(formField);
    } catch (IOException ioe) {
        System.out.println(ioe);
    } catch (DocumentException de) {
        System.out.println(de);
    }

}

From source file:questions.compression.CompressionLevelsEmbeddedFiles.java

public static void createPdf(int compressionLevel) {
    try {//from  www .  j a va 2s. c o  m
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT[compressionLevel + 1]));
        document.open();
        document.add(new Paragraph("Hello World"));
        PdfFileSpecification fs1 = PdfFileSpecification.fileEmbedded(writer, null, "some.txt",
                "some text".getBytes(), compressionLevel);
        writer.addAnnotation(PdfAnnotation.createFileAttachment(writer, new Rectangle(100f, 750f, 120f, 770f),
                "This is some text", fs1));
        PdfFileSpecification fs2 = PdfFileSpecification.fileEmbedded(writer, RESOURCE, "caesar.txt", null,
                compressionLevel);
        writer.addAnnotation(PdfAnnotation.createFileAttachment(writer, new Rectangle(100f, 780f, 120f, 800f),
                "Caesar", fs2));
        document.close();
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }
}

From source file:questions.encryption.HelloWorldFullyEncrypted.java

public static void main(String[] args) {
    // step 1/*  www  .ja  v a2  s .  c  o  m*/
    Document document = new Document();
    try {
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        writer.setEncryption("hello".getBytes(), "world".getBytes(), 0, PdfWriter.ENCRYPTION_AES_128);
        writer.createXmpMetadata();
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph("Hello World"));
        writer.addAnnotation(PdfAnnotation.createFileAttachment(writer, new Rectangle(100f, 650f, 150f, 700f),
                "This is some text", "some text".getBytes(), null, "some.txt"));
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    // step 5
    document.close();
}

From source file:questions.encryption.HelloWorldMetadataNotEncrypted.java

public static void main(String[] args) {
    // step 1//from   w  w  w  . j  ava 2s.  c o m
    Document document = new Document();
    try {
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        writer.setEncryption("hello".getBytes(), "world".getBytes(), 0,
                PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
        writer.createXmpMetadata();
        writer.setPdfVersion(PdfWriter.VERSION_1_5);
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph("Hello World"));
        writer.addAnnotation(PdfAnnotation.createFileAttachment(writer, new Rectangle(100f, 650f, 150f, 700f),
                "This is some text", "some text".getBytes(), null, "some.txt"));
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    // step 5
    document.close();
}

From source file:questions.forms.FormWithTooltips.java

public static void main(String[] args) {
    Document document = new Document();
    try {/* ww  w  .  java 2 s .  c  o  m*/
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();
        PdfFormField person = PdfFormField.createEmpty(writer);
        person.setFieldName("person");
        document.add(createTable(writer, person));
        writer.addAnnotation(person);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    // step 5
    document.close();
}

From source file:questions.forms.KidsOnDifferentPages.java

public static void createPdf() {
    Document document = new Document();
    try {/*w w  w . j  av  a2 s. c  om*/
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(FORM));
        document.open();

        // create the parent field and its kids
        PdfFormField person = PdfFormField.createEmpty(writer);
        person.setFieldName("person");
        // one kid on page 1
        TextField field1 = new TextField(writer, new Rectangle(0, 0), "name1");
        PdfFormField kid1 = field1.getTextField();
        kid1.setPlaceInPage(1);
        person.addKid(kid1);
        // another kid on page 2
        TextField field2 = new TextField(writer, new Rectangle(0, 0), "name2");
        PdfFormField kid2 = field2.getTextField();
        kid2.setPlaceInPage(2);
        person.addKid(kid2);
        writer.addAnnotation(person);

        // now add the page content
        document.add(createTable(kid1));
        document.newPage();
        document.add(createTable(kid2));
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    document.close();
}

From source file:questions.forms.MultipleChoice.java

public static void createPdf() throws IOException, DocumentException {
    // step 1//from  w ww . j a va2 s . c om
    Document document = new Document();
    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
    // step 3
    document.open();
    // step 4
    TextField field = new TextField(writer, new Rectangle(36, 750, 144, 806), "iText");
    field.setFontSize(9);
    String[] list_options = { "JAVA", "C", "CS", "VB", "PHP" };
    field.setChoiceExports(list_options);
    String[] list_values = { "Java", "C/C++", "C#", "VB", "PHP" };
    field.setChoices(list_values);
    PdfFormField f = field.getListField();
    f.setFieldFlags(PdfFormField.FF_MULTISELECT);
    f.put(PdfName.I, new PdfArray(new int[] { 0, 2 }));
    writer.addAnnotation(f);
    // step 5
    document.close();
}

From source file:questions.forms.RadioButtonsOnDifferentPages.java

public static void main(String[] args) {
    Document document = new Document();
    try {/*from   w  w  w.  j ava  2 s  .  c o  m*/
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();

        PdfContentByte cb = writer.getDirectContent();
        BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
        String[] languages = { "English", "French", "Dutch" };
        Rectangle rect;

        // create radio button field and its kids
        PdfFormField language = PdfFormField.createRadioButton(writer, true);
        language.setFieldName("language");
        language.setValueAsName(languages[0]);
        for (int i = 0; i < languages.length; i++) {
            rect = new Rectangle(40, 806 - i * 40, 60, 788 - i * 40);
            addRadioButton(writer, rect, language, languages[i], i == 0, writer.getPageNumber() + i);
        }
        writer.addAnnotation(language);

        // add the page content
        for (int i = 0; i < languages.length; i++) {
            cb.beginText();
            cb.setFontAndSize(bf, 18);
            cb.showTextAligned(Element.ALIGN_LEFT, languages[i], 70, 790 - i * 40, 0);
            cb.endText();
            document.newPage();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    // step 5: we close the document
    document.close();

}

From source file:questions.javascript.AddJavaScriptToForm.java

public static void createPdf(String filename) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
    document.open();/*w w w  . j  a v a 2  s.  c om*/

    BaseFont bf = BaseFont.createFont();
    PdfContentByte directcontent = writer.getDirectContent();
    directcontent.beginText();
    directcontent.setFontAndSize(bf, 12);
    directcontent.showTextAligned(Element.ALIGN_LEFT, "Married?", 36, 770, 0);
    directcontent.showTextAligned(Element.ALIGN_LEFT, "YES", 58, 750, 0);
    directcontent.showTextAligned(Element.ALIGN_LEFT, "NO", 102, 750, 0);
    directcontent.showTextAligned(Element.ALIGN_LEFT, "Name partner?", 36, 730, 0);
    directcontent.endText();

    PdfFormField married = PdfFormField.createRadioButton(writer, true);
    married.setFieldName("married");
    married.setValueAsName("yes");
    Rectangle rectYes = new Rectangle(40, 766, 56, 744);
    RadioCheckField yes = new RadioCheckField(writer, rectYes, null, "yes");
    yes.setChecked(true);
    married.addKid(yes.getRadioField());
    Rectangle rectNo = new Rectangle(84, 766, 100, 744);
    RadioCheckField no = new RadioCheckField(writer, rectNo, null, "no");
    no.setChecked(false);
    married.addKid(no.getRadioField());
    writer.addAnnotation(married);

    Rectangle rect = new Rectangle(40, 710, 200, 726);
    TextField partner = new TextField(writer, rect, "partner");
    partner.setBorderColor(Color.BLACK);
    partner.setBorderWidth(0.5f);
    writer.addAnnotation(partner.getTextField());

    document.close();
}

From source file:questions.javascript.TriggerMenuButtons.java

public static final void main(String[] args) {
    Document document = new Document();
    try {/* w ww. jav a2 s.  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();
}