import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import com.gnostice.pdfone.PDFOne;
import com.gnostice.pdfone.PdfDocument;
import com.gnostice.pdfone.PdfException;
import com.gnostice.pdfone.PdfReader;
import com.gnostice.pdfone.PdfWriter;

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

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

        // To try other examples, add the obj.<example_method>
        // accordingly. For example, try:
        // obj.fileReader_File_example();
        obj.fileStreamReader_FileInputStream_String_example();
     }
    // This code segment demonstrates how to create a
    // PDF reader object by specifying a FileInputStream object
    // for the input file and pathname string for the output file.
    public void fileStreamReader_FileInputStream_String_example()
        throws IOException, PdfException
    {
        // Creates a file for reading - the input file
        PdfWriter writer = PdfWriter.fileWriter(
            "PdfReader_fileStreamReader_FileInputStream_"
            + "String_Example_INPUT_FILE.pdf");
        PdfDocument document1 = new PdfDocument(writer); 
        document1.writeText(
            "This text is from "
            + "PdfReader_fileStreamReader_FileInputStream_"
            + "String_Example_INPUT_FILE.pdf");       
        document1.write();
        writer.dispose();         
    	
    	// Creates a FileInputStream object for the above file
        FileInputStream fis = new FileInputStream(
            "PdfReader_fileStreamReader_FileInputStream_"
            + "String_Example_INPUT_FILE.pdf");

        // Creates a String object for the output file
        String outputPathname = 
            "PdfReader_fileStreamReader_FileInputStream_"
            + "String_Example_OUTPUT_FILE.pdf";

        // Creates a PdfReader object with the FileInputStream
        // object and the String object
        PdfReader reader = PdfReader.fileStreamReader(
                                        fis,
                                        outputPathname);

        // Creates a PdfDocument object with the PdfReader object
        PdfDocument document = new PdfDocument(reader);

        // Writes an extra line of text 
        document.writeText(
                    "This text goes to - "
                    + "PdfReader_fileStreamReader_FileInputStream_"
                    + "String_Example_OUTPUT_FILE.pdf",
                    200, 100);

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

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

    // This code segment reads from an existing file, using a
    // FileInputStream object. It then writes an extra line of text.
    // The file contents along with the changes are saved to an output
    // file using an OutputStream object.
    public void fileStreamReader_FileInputStream_OutputStream_example()
        throws IOException, PdfException
    {
        // Creates a file for reading - the input file
        PdfWriter writer = PdfWriter.fileWriter(
            "PdfReader_fileStreamReader_FileInputStream_"
            + "OutputStream_Example_INPUT_FILE.pdf");
        PdfDocument document1 = new PdfDocument(writer); 
        document1.writeText(
            "This text is from "
            + "PdfReader_fileStreamReader_FileInputStream_"
            + "OutputStream_Example_INPUT_FILE.pdf");       
        document1.write();
        writer.dispose();        
        
        // Creates a FileInputStream object for the above file
        FileInputStream fis = new FileInputStream(
            "PdfReader_fileStreamReader_FileInputStream_"
            + "OutputStream_Example_INPUT_FILE.pdf");

        // Creates an OutputStream object for the output file
        OutputStream os = new FileOutputStream(
            "PdfReader_fileStreamReader_FileInputStream_"
            + "OutputStream_Example_OUTPUT_FILE.pdf");

        // Creates a PdfReader object with the FileInputStream object
        // and OutputStream object
        PdfReader reader = PdfReader.fileStreamReader(fis, os);

        // Creates a PdfDocument object with the new PdfReader object
        PdfDocument document = new PdfDocument(reader);

        // Writes a line of text for the output file
        document.writeText(
            "This text goes to - " +
            "PdfReader_fileStreamReader_FileInputStream_"
            + "OutputStream_Example_OUTPUT_FILE.pdf",
            200, 100);

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

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

    // This code segment reads from an existing file using a
    // FileInputStream object, counts the pages, and outputs the
    // number on the system console.
    public void fileStreamReader_FileInputStream_example()
        throws IOException, PdfException
    {
        // Creates a file for reading
        PdfWriter writer = PdfWriter.fileWriter(
            "PdfReader_fileStreamReader_FileInputStream_Example.pdf");
        PdfDocument document1 = new PdfDocument(writer); 
        document1.writeText("This is a sample file.");
        document1.write();
        writer.dispose();        
        
        // Creates a FileInputStream object for the above file 
        FileInputStream fis = new FileInputStream(
            "PdfReader_fileStreamReader_FileInputStream_Example.pdf");
        
        // Creates a new PdfReader object with the FileInputStream
        // object and makes the file contents available for reading
        PdfReader reader = PdfReader.fileStreamReader(fis);

        // Creates a PdfDocument object from the new PdfReader object
        PdfDocument document2 = new PdfDocument(reader);

        // Prints the number of pages in the document
        System.out.println(document2.getPageCount());

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

    // This code segment reads from an existing file, specified by a
    // String object. It then writes an extra line of text. The file
    // contents along with the changes are saved to an output file
    // represented by another String object.
    public void fileReader_String_String_example()
        throws IOException, PdfException
    {
        // Creates a String object containing the pathname of the
        // file to be read - the input file
        String inputPathname =
            "PdfReader_fileReader_String_String_"
            + "Example_INPUT_FILE.pdf";

        // Creates the input file
        PdfWriter writer = PdfWriter.fileWriter(inputPathname);
        PdfDocument document1 = new PdfDocument(writer);
        document1.writeText("This text is from - " + inputPathname);
        document1.write();
        writer.dispose();

        // Creates a String object for the output file
        String outputPathname =
            "PdfReader_fileReader_String_String_"
            + "Example_OUTPUT_FILE.pdf";

        // Creates a PdfReader object with the String objects
        PdfReader reader = PdfReader.fileReader(
                                        inputPathname,
                                        outputPathname);

        // Creates a PdfDocument object with the PdfReader object
        PdfDocument document = new PdfDocument(reader);

        // Writes an extra line of text
        document.writeText("This text goes to - " + outputPathname,
                           200, 100);

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

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

    // This code segment reads from an existing file, specified by a
    // String object. It then writes an extra line of text. The file
    // contents along with the changes are saved to an output file
    // represented by an OutputStream object.
    public void fileReader_String_OutputStream_example()
        throws IOException, PdfException
    {
        // Creates a String object containing the pathname of the
        // file to be read - the input file
        String inputPathname = "PdfReader_fileReader_String_"
                               + "OutputStream_example_"
                               + "INPUT_FILE.pdf";

        // Creates the input file
        PdfWriter writer = PdfWriter.fileWriter(inputPathname);
        PdfDocument document1 = new PdfDocument(writer);
        document1.writeText("This text is from - " + inputPathname);
        document1.write();
        writer.dispose();

        // Creates an OutputStream object with the String object
        OutputStream os = new FileOutputStream(
                              "PdfReader_fileReader_"
                              + "String_OutputStream_example_"
                              + "OUTPUT_FILE.pdf");

        // Creates a PdfReader object with the String object and the
        // OutputStream object
        PdfReader reader = PdfReader.fileReader(inputPathname, os);

        // Creates a PdfDocument object with the PdfReader object
        PdfDocument document = new PdfDocument(reader);

        // Writes an extra line of text
        document.writeText("This text goes to - "
                           + "PdfReader_fileReader_"
                           + "String_OutputStream_example_"
                           + "OUTPUT_FILE.pdf",
                           200, 100);

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

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

    // This code segment reads from an existing file using a
    // String object, counts pages, and outputs the number on the
    // system console
    public void fileReader_String_example() throws IOException,
        PdfException
    {
        // Creates a String object containing the pathname of the
        // file to be read - the input file
        String inputPathname =
                   "PdfReader_fileReader_String_Example.pdf";

        // Creates the input file
        PdfWriter writer = PdfWriter.fileWriter(inputPathname);
        PdfDocument document1 = new PdfDocument(writer);
        document1.writeText("This text is from - " + inputPathname);
        document1.write();
        writer.dispose();

        // Creates a PdfReader object with the String object
        PdfReader reader = PdfReader.fileReader(inputPathname);

        // Creates a PdfDocument object with the PdfReader object
        // and make the file contents available for reading
        PdfDocument document = new PdfDocument(reader);

        // Prints the number of pages in the document
        System.out.println(document.getPageCount());

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

    // This code segment reads from an existing file, specified by a
    // File object. It then writes an extra line of text. The file
    // contents along with the changes are saved to an output file
    // represented by a String object.
    public void fileReader_File_String_example() throws IOException,
        PdfException
    {
        // Creates a file for reading
        PdfWriter writer = PdfWriter.fileWriter(
            "PdfReader_fileReader_File_String_"
            + "Example_INPUT_FILE.pdf");
        PdfDocument document1 = new PdfDocument(writer);
        document1.writeText(
            "This text is from PdfReader_fileReader_File_"
            + "String_Example_INPUT_FILE.pdf");
        document1.write();
        writer.dispose();

        // Creates a File object for the input file
        File f = new File("PdfReader_fileReader_File_"
                          + "String_Example_INPUT_FILE.pdf");

        // Creates a String object for the output file
        String outputPathname = "PdfReader_fileReader_File_"
                                + "String_Example_OUTPUT_FILE.pdf";

        // Creates a PdfReader object with the File object and the
        // String object
        PdfReader reader = PdfReader.fileReader(f, outputPathname);

        // Creates a PdfDocument object with the PdfReader object
        PdfDocument document2 = new PdfDocument(reader);

        // Writes an extra line of text
        document2.writeText("This text goes to - "
                            + "PdfReader_fileReader_File_String_"
                            + "Example_OUTPUT_FILE.pdf", 200, 100);

        // Writes the PdfDocument object to the output file
        document2.write();

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

    // This code segment reads from an existing file, specified by a
    // File object. It then writes an extra line of text. The file
    // contents along with the changes are saved to an output file
    // represented by an OutputStream object.
    public void fileReader_File_OutputStream_example()
        throws IOException, PdfException
    {
    	// Creates a file for reading
    	PdfWriter writer = PdfWriter.fileWriter(
    		"PdfReader_fileReader_File_OutputStream_"
    	        + "Example_INPUT_FILE.pdf");
    	PdfDocument document1 = new PdfDocument(writer);
    	document1.writeText(
    		"This text is from PdfReader_fileReader_File_"
    		+ "OutputStream_Example_INPUT_FILE.pdf");
    	document1.write();
    	writer.dispose();

        // Creates a File object for the above file
        File file = new File(
        		"PdfReader_fileReader_File_OutputStream_"
        	        + "Example_INPUT_FILE.pdf");

        // Creates OutputStream object for the output file
        OutputStream os = new FileOutputStream(
        		"PdfReader_fileReader_File_OutputStream_"
        	        + "Example_OUTPUT_FILE.pdf");

        // Creates a PdfReader object with the File object and the
        // OutputStream object
        PdfReader reader = PdfReader.fileReader(file, os);

        // Creates a PdfDocument object with the PdfReader object
        PdfDocument document2 = new PdfDocument(reader);

        // Writes an extra line of text
        document2.writeText(
                    "This text goes to - "
           	    + "PdfReader_fileReader_File_OutputStream_"
  	    	    + "Example_OUTPUT_FILE.pdf",
        	    200, 100);

        // Writes the PdfDocument object to the output file
        document2.write();

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


    // This code segment reads from an existing file using a File
    // object, counts the pages, and outputs the number on the system
    // console.
    public void fileReader_File_example() throws IOException,
        PdfException
    {

    	// Creates a file for reading
    	PdfWriter writer = PdfWriter.fileWriter(
    		"PdfReader_fileReader_File_Example.pdf");
    	PdfDocument document1 = new PdfDocument(writer); 
    	document1.writeText("This is a sample file.");
    	document1.write();
    	writer.dispose();
    	
        // Creates a File object for the above file
    	File f = new File("PdfReader_fileReader_File_Example.pdf");
    	
    	// Creates a PdfReader object with above File object
        PdfReader reader = PdfReader.fileReader(f);

        // Creates a PdfDocument object with the above PdfReader
        // object and makes the file contents available for reading
        PdfDocument document2 = new PdfDocument(reader);

        // Prints the number of pages in the document
        System.out.println(document2.getPageCount());

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