import java.awt.Color;
import java.awt.Rectangle;
import java.io.IOException;

import com.gnostice.pdfone.PDFOne;
import com.gnostice.pdfone.PdfAnnot;
import com.gnostice.pdfone.PdfDocument;
import com.gnostice.pdfone.PdfException;
import com.gnostice.pdfone.PdfMeasurement;
import com.gnostice.pdfone.PdfPage;
import com.gnostice.pdfone.PdfRect;
import com.gnostice.pdfone.PdfTextAnnot;
import com.gnostice.pdfone.PdfWriter;

public class PdfTextAnnot_Examples
{
    // Activates the component PDFOne.jar
    static
    {
        PDFOne.activate("T95VZE:W8HBPVA:74VQ8QV:LO4V8",
            "9B1HRZAP:X5853ERNE:5EREMEGRQ:TX1R10");
    }

    public static void main(String[] args) throws IOException,
        PdfException
    {
        PdfTextAnnot_Examples obj = new PdfTextAnnot_Examples();

        // To try other examples, add the obj.<example_method>
        // accordingly. For example, try:
        // obj.setRect_Example();
        obj.PdfTextAnnot_CONSTRUCTORS_Example();
    }

    // This code segment creates several text annotations using
    // overloaded constructors
    public void PdfTextAnnot_CONSTRUCTORS_Example()
        throws IOException, PdfException
    {
        PdfWriter writer = PdfWriter.fileWriter(
            "PdfTextAnnot_CONSTRUCTORS_Example.pdf");
        PdfDocument document = new PdfDocument(writer);
        PdfPage page = new PdfPage();
        // Constructs a text annotation with pink-colored popup
        // window. Specifies that the top-left corner of the
        // annotation rectangle is positioned at (200, 100).
        PdfTextAnnot tAnnot1 = new PdfTextAnnot(200, 75, Color.PINK);
        // Constructs a text annotation that can be printed but is
        // read-only
        PdfTextAnnot tAnnot2 = new PdfTextAnnot(
            200, 150,
            PdfTextAnnot.FLAG_PRINT | PdfTextAnnot.FLAG_READONLY);
        // Constructs a text annotation with a pink-colored popup
        // window. Specifies that the annotation can be printed but
        // is read-only.
        PdfTextAnnot tAnnot3 = new PdfTextAnnot(
            200, 225,
            PdfTextAnnot.FLAG_PRINT | PdfTextAnnot.FLAG_READONLY,
            Color.PINK);
        // Constructs a text annotation with comment-icon appearance
        // that is automatically displayed when the document is
        // displayed
        PdfTextAnnot tAnnot4 = new PdfTextAnnot(
                                       200, 300,
                                       PdfTextAnnot.ICON_COMMENT,
                                       true);

        // Specifies title, subject and contents settings for the
        // above annotations
        tAnnot1.setTitle("PdfTextAnnot");
        tAnnot2.setTitle("PdfTextAnnot");
        tAnnot3.setTitle("PdfTextAnnot");
        tAnnot4.setTitle("PdfTextAnnot");
        tAnnot1.setSubject("Constructor");
        tAnnot2.setSubject("Constructor");
        tAnnot3.setSubject("Constructor");
        tAnnot4.setSubject("Constructor");
        tAnnot1.setContents(
            "PdfTextAnnot(double x, double y, Color c)");
        tAnnot2.setContents(
            "PdfTextAnnot(double x, double y, int flags)");
        tAnnot3.setContents(
            "PdfTextAnnot(double x, double y, int flags, Color c)");
        tAnnot4.setContents(
            "PdfTextAnnot(double x, double y, int iconName, "
            + "boolean open)");

        // Creates strings to be used as contents for the annotations
        // created below
        String str5 =
            "PdfTextAnnot(double x, double y, String subject, "
            + "String contents, String title)";
        String str6 =
            "PdfTextAnnot(double x, double y, String subject, "
            + "String contents, String title, Color c)";
        String str7 =
            "PdfTextAnnot(double x, double y, String subject, "
            + "String contents, String title, int flags)";
        String str8 =
            "PdfTextAnnot(double x, double y, String subject, "
            + "String contents, String title, int flags, Color c)";
        String str9 =
            "PdfTextAnnot(double x, double y, String subject, "
            + "String contents, String title, int flags, Color c,"
            + "int iconName, boolean open)";
        // Constructs a text annotation with specified subject,
        // contents, and title
        PdfTextAnnot tAnnot5 = new PdfTextAnnot(250, 375,
                                                "Constructor",
                                                str5,
                                                "PdfTextAnnot");
        // Constructs a text annotation with pink-colored popup
        // window. Also specifies subject, contents, and title.
        PdfTextAnnot tAnnot6 = new PdfTextAnnot(250, 450,
                                                "Constructor",
                                                str6,
                                                "PdfTextAnnot",
                                                Color.PINK);
        // Constructs a text annotation with specified subject,
        // contents, title, and flags. Also specifies that the
        // annotation is read-only.
        PdfTextAnnot tAnnot7 = new PdfTextAnnot(
                                250, 525,
                                "Constructor",
                                str7,
                                "PdfTextAnnot",
                                PdfTextAnnot.FLAG_READONLY);
        // Constructs a text annotation with specified subject,
        // contents, title, flags, and color of popup window
        PdfTextAnnot tAnnot8 = new PdfTextAnnot(
                               250, 600,
                               "Constructor",
                               str8,
                               "PdfTextAnnot",
                               PdfTextAnnot.FLAG_READONLY,
                               Color.PINK);
        // Constructs a text annotation with specified subject,
        // constructor, title, flags, color of popup window, icon
        // appearance, and display setting
        PdfTextAnnot tAnnot9 = new PdfTextAnnot(
                                   250, 675,
                                   "Constructor", 
                                   str9,
                                   "PdfTextAnnot",
                                   PdfTextAnnot.FLAG_READONLY,
                                   Color.PINK,
                                   PdfTextAnnot.ICON_PARAGRAPH,
                                   true);

        // Sets the annotation windows to be displayed by default
        tAnnot1.setOpen(true);
        tAnnot2.setOpen(true);
        tAnnot3.setOpen(true);
        tAnnot5.setOpen(true);
        tAnnot6.setOpen(true);
        tAnnot7.setOpen(true);
        tAnnot8.setOpen(true);

        page.addAnnotation(tAnnot1);
        page.addAnnotation(tAnnot2);
        page.addAnnotation(tAnnot3);
        page.addAnnotation(tAnnot4);
        page.addAnnotation(tAnnot5);
        page.addAnnotation(tAnnot6);
        page.addAnnotation(tAnnot7);
        page.addAnnotation(tAnnot8);
        page.addAnnotation(tAnnot9);

        document.add(page);

        // Sets the file to be opened after it is written to
        document.setOpenAfterSave(true);

        // Writes the document object to file
        document.write();

        // Closes all I/O streams associated with this writer object
        writer.dispose();
    }

    // This code segment creates a text annotation, set its
    // properties, and adds it to a page
    public void SETTERS_Example() throws IOException,
        PdfException
    {
        PdfWriter writer = PdfWriter.fileWriter(
            "PdfTextAnnot_SETTERS_Example.pdf");
        PdfDocument document = new PdfDocument(writer);
        // Creates a text annotation object
        PdfTextAnnot tAnnot1 = new PdfTextAnnot();
        // Specifies a unique name in the document for the annotation
        tAnnot1.setAnnotName("First_Text_Annotation");
        // Specifies color of the annotation popup
        tAnnot1.setColor(Color.PINK);
        // Specifies icon appearance
        tAnnot1.setIconName(PdfTextAnnot.ICON_KEY);
        // Specifies text to be used in title bar
        tAnnot1.setTitle("Attn: All");
        // Specifies text to be used as subject of the annotation
        tAnnot1.setSubject("EOD Deliverables");
        // Specifies contents of the annotation
        tAnnot1.setContents(
            "Please check this document. It has some errors.");
        // Specifies coordinates of top-left corner of the annotation
        // rectangle at position (200, 100)
        tAnnot1.setLocation(200, 100);
        // Specifies that the annotation is to be included when the
        // documented is printed
        tAnnot1.setFlags(PdfAnnot.FLAG_PRINT);
        // Specifies the annotation to be displayed by default
        tAnnot1.setOpen(true);

        // Creates a page
        PdfPage page = new PdfPage();

        // Adds the annotation to the above page
        page.addAnnotation(tAnnot1);

        document.add(page);

        // Sets the file to be opened after it is written to
        document.setOpenAfterSave(true);

        // Writes the document object to file
        document.write();

        // Closes all I/O streams associated with this writer object
        writer.dispose();
    }

    // This code segment creates a text annotation and then clones it
    // several times. The location of the cloned annotation is changed
    // using overloaded setRect methods.
    public void setRect_Example() throws IOException, PdfException
    {
        PdfWriter writer = PdfWriter.fileWriter(
            "PdfTextAnnot_setRect_Example.pdf");
        PdfDocument document = new PdfDocument(writer);

        // Creates a page and writes some text
        PdfPage page = new PdfPage();
        page.writeText("One, to, buckle my shoe.", 200, 100);
        page.writeText("Three, for, shut the door.", 200, 150);
        page.writeText("Five, socks, pick up sticks.", 200, 200);
        page.writeText("Seven, height, lay them straight. ", 200, 250);
        page.writeText("Nine, ton, a big fat hen. ", 200, 300);
        page.writeText("Eleven, toll, men must delve. ", 200, 350);

        // Creates a text annotation object and specifies its
        // properties
        PdfTextAnnot tAnnot1 = new PdfTextAnnot();
        tAnnot1.setAnnotName("Annotation1");
        tAnnot1.setColor(Color.PINK);
        tAnnot1.setIconName(PdfTextAnnot.ICON_COMMENT);
        tAnnot1.setTitle("Mistakes in this document");
        tAnnot1.setSubject("Mistake #1");
        tAnnot1.setContents("\"Two\" is spelled as \"to\"");
        tAnnot1.setFlags(PdfAnnot.FLAG_PRINT);
        tAnnot1.setOpen(false);
        tAnnot1.setLocation(200, 80);

        // Creates cloned annotations with the above annotation
        PdfTextAnnot tAnnot2 = (PdfTextAnnot) tAnnot1.clone();
        PdfTextAnnot tAnnot3 = (PdfTextAnnot) tAnnot1.clone();
        PdfTextAnnot tAnnot4 = (PdfTextAnnot) tAnnot1.clone();
        PdfTextAnnot tAnnot5 = (PdfTextAnnot) tAnnot1.clone();
        PdfTextAnnot tAnnot6 = (PdfTextAnnot) tAnnot1.clone();

        // Specifies annotation rectangle with its top corner at
        // position (200, 130) and its width and height set at 50
        tAnnot2.setRect(200, 130, 50, 50);
        tAnnot2.setAnnotName("Annotation2");
        tAnnot2.setSubject("Mistake #2");
        tAnnot3.setContents("\"Six\" is spelled as \"socks\"");
        // Specifies annotation rectangle in explicitly specified
        // measurement units
        tAnnot3.setRect(200, 180, 50, 150, PdfMeasurement.MU_POINTS);
        tAnnot3.setAnnotName("Annotation3");
        tAnnot3.setSubject("Mistake #3");
        tAnnot4.setContents("\"Eight\" is spelled as \"height\"");
        // Specifies annotation rectangle with a PdfRect object
        PdfRect pRectangle1 = new PdfRect(200, 230, 50, 50);
        tAnnot4.setRect(pRectangle1);
        tAnnot4.setAnnotName("Annotation4");
        tAnnot4.setSubject("Mistake #4");
        tAnnot4.setContents("\"Eight\" is spelled as \"height\"");
        // Specifies annotation rectangle with a PdfRect object.
        // Explicitly specifies that the PdfRect object is to applied
        // in points.
        PdfRect pRectangle2 = new PdfRect(200, 280, 50, 50);
        tAnnot5.setRect(pRectangle2, PdfMeasurement.MU_POINTS);
        tAnnot5.setAnnotName("Annotation5");
        tAnnot5.setSubject("Mistake #5");
        tAnnot5.setContents("\"Ten\" is spelled as \"ton\"");
        // Specifies annotation rectangle with a Rectangle object
        Rectangle jRectangle = new Rectangle(200, 330, 50, 50);
        tAnnot6.setRect(jRectangle);
        tAnnot6.setAnnotName("Annotation6");
        tAnnot6.setSubject("Mistake #6");
        tAnnot6.setContents("\"Twelve\" is spelled as \"toll\"");

        page.addAnnotation(tAnnot1);
        page.addAnnotation(tAnnot2);
        page.addAnnotation(tAnnot3);
        page.addAnnotation(tAnnot4);
        page.addAnnotation(tAnnot5);
        page.addAnnotation(tAnnot6);

        document.add(page);

        // Sets the file to be opened after it is written to
        document.setOpenAfterSave(true);

        // Writes the document object to file
        document.write();

        // Closes all I/O streams associated with this writer object
        writer.dispose();
    }
}