import java.io.IOException;

import com.gnostice.pdfone.PDFOne;
import com.gnostice.pdfone.PdfCustomPlaceholderHandler;
import com.gnostice.pdfone.PdfDocument;
import com.gnostice.pdfone.PdfException;
import com.gnostice.pdfone.PdfReader;
import com.gnostice.pdfone.PdfSaveAsImageHandler;

public class PdfProDocument_Examples implements
    PdfCustomPlaceholderHandler, PdfSaveAsImageHandler
{
    	static {
		PDFOne.activate("T95VZE:W8HBPVA:74VQ8QV:LO4V8",
				"9B1HRZAP:X5853ERNE:5EREMEGRQ:TX1R10");
	}

	public static void main(String[] args) throws IOException, PdfException {
		// Create an object of this class
		PdfProDocument_Examples obj = new PdfProDocument_Examples();
		// Execute example method of the above instance
		obj.saveAsImage_Example(args[0]);
	}


    
    // This code snippet illustrates how to add a list of form fields
    // to a page in a document.
    public void addFormField_List_int_Example()
        throws IOException, PdfException
    {
        // Create a blank PDF document with a single page
        PdfWriter wrtr = 
            PdfWriter.fileWriter(
                "addFormField_List_int_example.pdf");
        PdfDocument doc = new PdfDocument(wrtr);
        PdfPage pg = new PdfPage(PdfPageSize.A4);
        doc.add(pg);

        // Create a new form field 
        PdfFormTextField ftf1 = 
            new PdfFormTextField(
                new PdfRect(250,100, 200, 18),
                "ftf_Name");
        ftf1.setBorderColor(Color.BLACK);
        ftf1.setName("ftf_Name");
        ftf1.setNameAsUnicode(false);
        ftf1.setValue("John Doe");
        
        // Create another form field
        PdfFormTextField ftf2 = (PdfFormTextField) ftf1.clone();
        ftf2.setRect(250, 125, 200, 18);
        ftf2.setName("ftf_Occupation");
        ftf2.setValue("");
        
        // Add the form fields to a list
        ArrayList lst = new ArrayList();
        lst.add(ftf1);
        lst.add(ftf2);
        
        // Add the list of form fields to page 1
        doc.addFormFieldList(lst, 1);

        doc.writeText("Enter name:", 150, 100);        
        doc.writeText("Enter occupation:", 150, 125);
        doc.setOpenAfterSave(true);

        // Write document to file
        doc.write();
        wrtr.dispose();        
    }

    
    // This code snippet illustrates how to add a form field to a
    // document
    public void addFormField_PdfFormField_int_Example()
        throws IOException, PdfException
    {
        // Create a blank PDF document with 5 pages
        PdfWriter wrtr =
            PdfWriter.fileWriter(
                "addFormField_PdfFormField_int_example.pdf");
        PdfDocument doc = new PdfDocument(wrtr);
        PdfPage pg = new PdfPage(PdfPageSize.A4);
        doc.add(pg);


        // Create a new form field
        PdfFormTextField ftf =
            new PdfFormTextField(
                new PdfRect(250,100, 200, 18),
                "ftf_Name");
        ftf.setBorderColor(Color.BLACK);
        ftf.setName("ftf_Name");
        ftf.setNameAsUnicode(false);
        ftf.setValue("John Doe");

        // Adds the form field to page 1 of the document
        doc.addFormField(ftf, 1);

        doc.writeText("Enter name:", 150, 100);
        doc.setOpenAfterSave(true);

        // Write document to file
        doc.write();
        wrtr.dispose();
    }


    
    // This code snippet illustrates how to add a list of annotations
    // to a document and also remove existing annotations in the
    // document. The annotation location are specified in inches.
    public void addAnnotationList_List_String_boolean_int_Example()
        throws IOException, PdfException
    {
        // Create a document with several annotations on page 1
        addAnnotationList_List_int_Example(false);

        // Read the above document
        PdfReader rdr = PdfReader.fileReader(
            "addAnnotationList_List_int_example.pdf",
            "addAnnotationList_List_String_boolean_int_Example1.pdf");
        PdfDocument doc = new PdfDocument(rdr);

        // Add some extra pages to the document
        PdfPage pg2 = new PdfPage(PdfPageSize.A4);
        PdfPage pg3 = new PdfPage(PdfPageSize.A4);
        PdfPage pg4 = new PdfPage(PdfPageSize.A4);
        doc.add(pg2);
        doc.add(pg3);
        doc.add(pg4);

        // Create several new annotations
        PdfTextAnnot ta1 = new PdfTextAnnot(2, 1,
            "addAnnotationList Example",
            "This annotation was part of a list.", "New annotation 1");
        PdfTextAnnot ta2 = new PdfTextAnnot(3, 2,
            "addAnnotationList Example",
            "This annotation was part of a list.", "New annotation 2");
        PdfTextAnnot ta3 = new PdfTextAnnot(4, 3,
            "addAnnotationList Example",
            "This annotation was part of a list.", "New annotation 3");

        // Add the annotations to a list
        ArrayList lst = new ArrayList();
        lst.add(ta1);
        lst.add(ta2);
        lst.add(ta3);

        String[] pr = new String[1];
        pr[0] = "1,3,4";

        // Add the list of annotations to pages 1, 3, and 4 and remove
        // existing annotations (in page 1)
        doc.addAnnotationList(
             lst, 
             pr, 
             true,
             PdfMeasurement.MU_INCHES);

        doc.setOpenAfterSave(true);

        // Write document to file
        doc.write();
        rdr.dispose();
    }    


    
    // This code snippet illustrates how to add a list of annotations
    // to a page and at the same time remove or keep existing
    // annotations
    public void addAnnotationList_List_int_boolean_Example()
        throws IOException, PdfException
    {
        // Create a document with several annotations
        addAnnotationList_List_int_Example(false);   
        
        // Read the above document
        PdfReader rdr = 
            PdfReader.fileReader(
                "addAnnotationList_List_int_example.pdf", 
                "addAnnotationList_List_int_boolean_Example1.pdf");        
        PdfDocument doc = new PdfDocument(rdr);
        
        // Create several new annotations
        PdfTextAnnot ta1 = 
            new PdfTextAnnot(200, 100,
                "addAnnotationList Example",
                "This annotation was part of a list.",
                "New annotation 1");
        PdfTextAnnot ta2 = 
            new PdfTextAnnot(200, 200,
                "addAnnotationList Example",
                "This annotation was part of a list.",
                "New annotation 2");
        PdfTextAnnot ta3 = 
            new PdfTextAnnot(300, 300,
                "addAnnotationList Example",
                "This annotation was part of a list.",
                "New annotation 3");
        
        // Add the annotations to a list
        ArrayList lst = new ArrayList();
        lst.add(ta1);
        lst.add(ta2);
        lst.add(ta3);
        
        // Add the list of annotations to the document and remove
        // existing annotations
        doc.addAnnotationList(lst, 1, true);

        doc.setOpenAfterSave(true);

        // Write document to file
        doc.write();
        rdr.dispose();
        
        // Read the original document with annotations
        rdr = PdfReader.fileReader(
                "addAnnotationList_List_int_example.pdf", 
                "addAnnotationList_List_int_boolean_Example2.pdf");
        doc = new PdfDocument(rdr);
        
        // Add the list of annotations to the document but do not
        // remove existing annotations
        doc.addAnnotationList(lst, 1, false);
        
        doc.setOpenAfterSave(true);

        // Write document to file
        doc.write();        
        rdr.dispose();        
    }

    
    // This code snippet illustrates how to add a list of annotations
    // to a document
    public void addAnnotationList_List_int_Example(boolean openAfterCreate)
        throws IOException, PdfException
    {

        // Create a blank PDF document
        PdfWriter wrtr = PdfWriter
            .fileWriter("addAnnotationList_List_int_example.pdf");
        PdfDocument doc = new PdfDocument(wrtr);

        // Add a new page to the document
        PdfPage pg1 = new PdfPage(PdfPageSize.A4);
        doc.add(pg1);

        // Create several new annotations
        PdfTextAnnot ta1 = 
            new PdfTextAnnot(100, 100,
                "addAnnotationList Example",
                "This annotation was part of a list.",
                "Annotation 1");
        PdfTextAnnot ta2 = 
            new PdfTextAnnot(100, 200,
                "addAnnotationList Example",
                "This annotation was part of a list.",
                "Aannotation 2");
        PdfTextAnnot ta3 = 
            new PdfTextAnnot(200, 300,
                "addAnnotationList Example",
                "This annotation was part of a list.",
                "Annotation 3");
        
        // Add the annotations to a list
        ArrayList lst = new ArrayList();
        lst.add(ta1);
        lst.add(ta2);
        lst.add(ta3);

        // Add list of annotations to page 1
        doc.addAnnotationList(lst, 1);

        doc.setOpenAfterSave(openAfterCreate);

        // Write document to file
        doc.write();
        wrtr.dispose();
    }

    
    // This code snippet illustrates how to add an annotation to a
    // page
    public void addAnnotation_Example() throws IOException,
        PdfException
    {

	// Create a blank PDF document
        PdfWriter wrtr =
        	PdfWriter.fileWriter("addAnnotation_example.pdf");
        PdfDocument doc = new PdfDocument(wrtr);

        // Add a new page to the document
        PdfPage pg1 = new PdfPage(PdfPageSize.A4);
        doc.add(pg1);

        // Create a new annotation
        PdfTextAnnot ta1 =
        	new PdfTextAnnot(
        			100,
        			100,
        			"This is an example",
        			"This annotation was added to the document",
        			"addAnnotation Example");

        // Add the annotation to page 1
        doc.addAnnotation(ta1, 1);

        doc.setOpenAfterSave(true);

        // Write document to file
        doc.write();
        wrtr.dispose();
    }

	public void saveAsImage_Example(String inputDoc) throws IOException,
			PdfException {
		PdfReader r = PdfReader.fileReader(inputDoc);
		PdfDocument d = new PdfDocument(r);

		// Specify instance that implements
		// PdfCustomPlaceholderHandler
		d.setCph(this);
		// Specify instance that implements
		// PdfSaveAsImageHandler
		d.setSaveAsImageHandler(this);

		// Save pages 1 and 2 as images
		d.saveAsImage(
				// format
				"jpg",
				// pages
				"1,2",
				// file name of image
				// - contains a custom placeholder
				"Image_<% myvar1 %>",
				// path of image
				// - contains a pre-defined placeholder
				// that refers to the input file path
				"<% InputFilePath %>");
		r.dispose();
	}

	public void onSaveAsImage(PdfDocument pdfDocument, int pageNum,
			StringBuffer imageName, StringBuffer outputFilepath) {
		if ((imageName != null)
			&& !imageName.toString().trim().equals("")
			&& (outputFilepath != null)
			&& !outputFilepath.toString().trim().equals("")) {
			// Save image to a folder named "images" in
			// the original output folder
			outputFilepath.append("\\images\\");
			// Write console information about extraction of image
			System.out.println(imageName + " extracted from" + " page #"
					+ pageNum + " saved to " + outputFilepath);

		}
	}

	public String onCustomPlaceHolder(String variable, PdfDocument d,
			int pagenumber) {
		// Provide value for custom placeholder "myVar1"
		if (variable.equalsIgnoreCase("myVar1")) {
			return "from_page" + pagenumber;
		}
		return null;
	}
	
        public void onSaveAsImage(PdfDocument pdfDocument, int pageNum,
               Integer imageWidth, Integer imageHeight,
               Float compressionLevel, StringBuffer imageName,
	       StringBuffer outputPath)
       {
        // Remains to be implemented
       }


}