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

import com.gnostice.pdfone.PDFOne;
import com.gnostice.pdfone.PdfAction;
import com.gnostice.pdfone.PdfAnnot;
import com.gnostice.pdfone.PdfBookmark;
import com.gnostice.pdfone.PdfDocument;
import com.gnostice.pdfone.PdfException;
import com.gnostice.pdfone.PdfLinkAnnot;
import com.gnostice.pdfone.PdfPage;
import com.gnostice.pdfone.PdfRect;
import com.gnostice.pdfone.PdfWriter;
import com.gnostice.pdfone.encodings.PdfEncodings;
import com.gnostice.pdfone.fonts.PdfFont;


public class PdfLinkAnnot_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
    {
        PdfLinkAnnot_Examples obj = new PdfLinkAnnot_Examples();
        obj.PdfLinkAnnot_CONSTRUCTORS();
    }

    // This code segment creates several link annotation using
    // overloaded methods. It also adds several actions to the
    // annotations with methods of the PdfLinkAnnot class.
    public void PdfLinkAnnot_CONSTRUCTORS() throws IOException,
        PdfException
    {
        PdfWriter writer = PdfWriter.fileWriter(
            "PdfLinkAnnot_CONSTRUCTORS.pdf");
        PdfDocument document = new PdfDocument(writer);
        // Creates two pages
        PdfPage page1 = new PdfPage();
        PdfPage page2 = new PdfPage();
        // Creates a PdfFont object and sets it to a hyperlink color
        PdfFont fontCourier = PdfFont.create(
            "COURIER", 10, PdfEncodings.WINANSI);
        fontCourier.setColor(Color.BLUE);
        // Creates a rectangle
        PdfRect rectangle = new PdfRect(150, 50, 300, 20);
        // Creates a link annotation using the default constructor
        PdfLinkAnnot lAnnot1 = new PdfLinkAnnot();
        // Specifies the above rectangle as the annotation rectangle
        lAnnot1.setRect(rectangle);
        // Adds an action to the annotation linking it to page 2
        lAnnot1.addActionGoTo(2);
        // Makes the annotation look like it has been pushed from
        // below the surface of page
        lAnnot1.setHighlightMode(PdfLinkAnnot.HIGHLIGHT_MODE_PUSH);
        // Writes text inside the annotation rectangle
        page1.writeText("Go to page 2", fontCourier, rectangle);
        // Adds the link annotation to the page
        page1.addAnnotation(lAnnot1);
        rectangle = new PdfRect(150, 100, 300, 20);
        // Creates a link annotation with specified annotation
        // rectangle. Also specifies the color of the annotation
        // rectangle.
        PdfLinkAnnot lAnnot2 = new PdfLinkAnnot(rectangle,
                                                Color.RED);
        // Enables the display of the annotation rectangle
        lAnnot2.setShowRect(true);
        // Adds action to the annotation linking it to a specified
        // rectangular area on page 2
        lAnnot2.addActionGoTo(
                 2,                   // page number
                 100, 400, 500, 100); // left, bottom, right, and top
        // Makes the contents of the annotation inverted when
        // highlighted
        lAnnot2.setHighlightMode(PdfLinkAnnot.HIGHLIGHT_MODE_INVERT);
        page1.writeText(
            "Go to specified rectangular area on page 2.",
            fontCourier, rectangle);
        page1.addAnnotation(lAnnot2);
        page2.drawRect(100, 100, 400, 300);
        rectangle = new PdfRect(150, 150, 300, 40);
        // Creates a link annotation with specified annotation
        // rectangle and flag
        PdfLinkAnnot lAnnot3 = new PdfLinkAnnot(
                                   rectangle,
                                   PdfAnnot.FLAG_LOCKED);
        // Adds action to annotation linking it to position
        // (200, 100) on page 2. Also specifies that page 2 is to
        // be zoomed to 200%.
        lAnnot3.addActionGoTo(2,          // page
                              200, 100,   // position
                              200);       // zoom
        // Makes the annotation remain unchanged when highlighted
        lAnnot3.setHighlightMode(PdfLinkAnnot.HIGHLIGHT_MODE_NONE);
        page1.writeText(
            "Go to position (200, 100) on page 2 and zoom 200%.",
            fontCourier, rectangle);
        page1.addAnnotation(lAnnot3);
        // Marks position (200, 100) on page 2
        page2.writeText(". (200, 100)", 200, 100);
        rectangle = new PdfRect(150, 200, 300, 40);
        // Creates a link annotation with specified annotation
        // rectangle, flag, and annotation rectangle color
        PdfLinkAnnot lAnnot4 = new PdfLinkAnnot(
                                   rectangle,
                                   PdfAnnot.FLAG_LOCKED,
                                   Color.RED);
        lAnnot4.setShowRect(true);
        // Adds action to annotation linking it to position 50 points
        // down from the top of page 2
        lAnnot4.addActionGoTo(2, 50, PdfLinkAnnot.FITH);
        // Makes the annotation to display an outline when
        // highlighted
        lAnnot4.setHighlightMode(
            PdfLinkAnnot.HIGHLIGHT_MODE_OUTLINE);
        page1.writeText(
            "Scroll down 50 points on page2 and zoom the page to "
            + "tightly fit its entire width inside the window",
            fontCourier, rectangle);
        page1.addAnnotation(lAnnot4);
        // Marks position 50 points down from the top of page 2
        page2.writeText(". (200, 50)", 200, 50);

        rectangle = new PdfRect(150, 250, 300, 20);
        // Creates a link annotation with specified annotation
        // rectangle, subject, contents and title
        PdfLinkAnnot lAnnot5 = new PdfLinkAnnot(
            rectangle,  // annotation rectangle
            "Demo",     // subject
            "Execute Javascript statement",    // contents
            "PdfLinkAnnot.addActionJavascript" // title
            + "(String script)");
        // Adds action to the annotation making it display a
        // Javascript alert message
        lAnnot5.addActionJavaScript("app.alert('Hello, world!')");
        lAnnot5.setHighlightMode(PdfLinkAnnot.HIGHLIGHT_MODE_PUSH);
        page1.writeText(
            "Select this link for a Javascript message",
            fontCourier, rectangle);
        page1.addAnnotation(lAnnot5);



        // Creates a test PDF file for use it with code segments
        // below
        char pathSeparator = File.separatorChar;
        String testFile = "."
                          + pathSeparator
                          + "InputDocs"
                          + pathSeparator
                          + "test_pdf_file.pdf";
        PdfWriter writer2 = PdfWriter.fileWriter(testFile);
        PdfDocument document2 = new PdfDocument(writer2);
        for (int i =1; i <=5; i++) {
            PdfPage page = new PdfPage();
            if (i == 2) {
                page.writeText(". (200, 100)", 200, 100);
                page.writeText(". (200, 50)", 200, 50);
            }
            page.writeText(
                "This is page #" + i + " of " + testFile);
            document2.add(page);
        }        
        document2.write();
        writer2.dispose();
        
        rectangle = new PdfRect(150, 300, 300, 40);
        // Creates a link annotation with specified annotation
        // rectangle, subject, contents, title, and annotation
        // rectangle color
        PdfLinkAnnot lAnnot6 = new PdfLinkAnnot(
            rectangle,           // annotation rectangle  
            "Demo",              // subject
            "Launch an application or document with an optional print"
            + "command setting", // contents
            "addActionLaunch(String applicationToLaunch, "
            + "boolean print)",  // title  
            Color.RED);          // annotation rectangle color
        lAnnot6.setShowRect(true);
        // Adds action to the annotation to have the test PDF file
        // printed
        lAnnot6.addActionLaunch(
                    testFile,    // applicationToLaunch
                    true);       // print
        lAnnot6.setHighlightMode(PdfLinkAnnot.HIGHLIGHT_MODE_PUSH);
        page1.writeText("Select this link to print "
                        + testFile,
                        fontCourier,
                        rectangle);
        page1.addAnnotation(lAnnot6);

        rectangle = new PdfRect(150, 350, 300, 20);
        // Creates a link annotation with specified annotation
        // rectangle, subject, contents, title, and flag
        PdfLinkAnnot lAnnot7 = new PdfLinkAnnot(
            rectangle,                        // annotation rectangle
            "Demo",                           // subject
            "Execute a named action",         // contents
            "addActionNamed(int actionType)", // title 
            PdfAnnot.FLAG_LOCKED);            // flag
        // Adds a named action to the annotation linking it to the
        // last page
        lAnnot7.addActionNamed(PdfAction.NAMED_LASTPAGE);
        lAnnot7.setHighlightMode(PdfLinkAnnot.HIGHLIGHT_MODE_PUSH);
        page1.writeText("Go to last page in document",
                        fontCourier, rectangle);
        page1.addAnnotation(lAnnot7);

        rectangle = new PdfRect(150, 400, 300, 20);
        // Creates a link annotation with specified, subject,
        // contents, title, flag, and annotation rectangle color
        PdfLinkAnnot lAnnot8 = new PdfLinkAnnot(
            rectangle,                   // annotation rectangle
            "Demo",                      // subject
            "Execute an URI",            // contents
            "addActionURI(String uri)",  // title 
            PdfAnnot.FLAG_LOCKED,        // flag
            Color.RED);                  // color
        lAnnot8.setShowRect(true);
        // Adds action to the annotation making it link to Gnostice
        // website
        lAnnot8.addActionURI("http://www.gnostice.com/");
        lAnnot8.setHighlightMode(PdfLinkAnnot.HIGHLIGHT_MODE_PUSH);
        // Sets border to be displayed
        lAnnot8.setShowRect(true);
        // Sets border style to dashed
        lAnnot8.setBorderStyle(PdfAnnot.BORDERSTYLE_DASHED);
        // Sets dash pattern to have 3-point dashes and 2-point gaps
        lAnnot8.setDashPattern(new int [] {3, 2});
        page1.writeText("Go to Gnostice website",
                        fontCourier, rectangle);
        page1.addAnnotation(lAnnot8);

        rectangle = new PdfRect(150, 450, 300, 40);
        // Creates a link annotation with specified annotation
        // rectangle, subject, contents, title, flag, annotation
        // rectangle color, and highlight mode
        PdfLinkAnnot lAnnot9 = new PdfLinkAnnot(
            rectangle,                 // annotation rectangle 
            "Demo",                    // subject
            "Go to a bookmark in an external file",    // contents
            "addActionRemoteGoTo(String pdfFilePath, " // title
            + "PdfBookmark.RemoteGoTo rGoTo, boolean newWindow)",
            PdfAnnot.FLAG_LOCKED,      // flag
            Color.RED,                 // annotation rectangle color
            PdfLinkAnnot.HIGHLIGHT_MODE_PUSH); // highlight mode
        lAnnot9.setShowRect(true);
        // Creates a remote Go-To action for navigating to page 2
        // of the test PDF file
        PdfBookmark.RemoteGoTo remoteBookmark =
            PdfLinkAnnot.getRemoteGoToInstance(2);
        // Sets the the annotation to performs the remote Go-To action
        // on the test PDF file in a new window
        lAnnot9.addActionRemoteGoTo(
            testFile,
            remoteBookmark,
            true);
        page1.writeText("Display page 2 of "
                        + testFile
                        + "a new Window",
                        fontCourier, rectangle);
        page1.addAnnotation(lAnnot9);

        rectangle = new PdfRect(150, 500, 300, 40);
        PdfLinkAnnot lAnnot10 = new PdfLinkAnnot();
        lAnnot10.setRect(rectangle);
        // Creates a remote Go-To action for navigating to a
        // rectangular area specified with reference to the
        // bottom-left corner of the test PDF file
        remoteBookmark = PdfLinkAnnot.getRemoteGoToInstance(
            2,                     // page
            100, 100, 600, 600);   // left, bottom, right, and top
        lAnnot10.addActionRemoteGoTo(
                        testFile,
                        remoteBookmark,
                        true);;
        lAnnot10.setHighlightMode(PdfLinkAnnot.HIGHLIGHT_MODE_PUSH);
        page1.writeText(
            "Display specified rectangular area on page 2 of "
            + testFile
            + "in a new Window",
            fontCourier, rectangle);
        page1.addAnnotation(lAnnot10);


        rectangle = new PdfRect(150, 550, 300, 40);
        PdfLinkAnnot lAnnot11 = new PdfLinkAnnot();
        lAnnot11.setRect(rectangle);
        // Creates a remote Go-To action for navigating to 50 points
        // up from the bottom of page 2 of another PDF file
        remoteBookmark = PdfLinkAnnot.getRemoteGoToInstance(
                                2,
                                50,
                                PdfLinkAnnot.FITH);
        lAnnot11.addActionRemoteGoTo(
            testFile,
            remoteBookmark,
            true);
        page1.writeText(
            "Scroll up 50 points from bottom of page2 of "
            + ".\\InputDocs\\test_pdf_file.pdf and zoom the page "
            + "to tightly fit its entire width inside a new window",
            fontCourier, rectangle);
        page1.addAnnotation(lAnnot11);


        rectangle = new PdfRect(150, 600, 300, 40);
        PdfLinkAnnot lAnnot12 = new PdfLinkAnnot();
        lAnnot12.setRect(rectangle);
        // Creates a remote Go-To action for navigating to page 2 of
        // the test PDF. Specifies that the page is to be displayed
        // with its entire height and width accommodated inside a
        // window.
        remoteBookmark =
            PdfLinkAnnot.getRemoteGoToInstance(2, PdfLinkAnnot.FITB);
        lAnnot12.addActionRemoteGoTo(
            testFile,
            remoteBookmark,
            true);
        page1.writeText(
            "Fit entire width and height of page 2 of "
            + testFile
            + " in a new Window",
            fontCourier, rectangle);
        page1.addAnnotation(lAnnot12);


        rectangle = new PdfRect(150, 650, 300, 40);
        PdfLinkAnnot lAnnot13 = new PdfLinkAnnot();
        lAnnot13.setRect(rectangle);
        // Creates a remote Go-To action for navigating to a
        // rectangle that has specified with reference to the
        // bottom-left corner of page 2 of another PDF file
        remoteBookmark = PdfLinkAnnot.getRemoteGoToInstance(
                            2,
                            new PdfRect(200, 200, 100, 100));
        lAnnot13.addActionRemoteGoTo(
            testFile,
            remoteBookmark,
            true);
        page1.writeText(
            "Display rectangle (200, 200, 100, 100) on page 2 of "
            + testFile
            + " in a new Window",
            fontCourier, rectangle);
        page1.addAnnotation(lAnnot13);



        document.add(page1);
        document.add(page2);

        // 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();
    }
}