import java.io.ByteArrayOutputStream;
import java.io.File;
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.PdfWriter;

public class PdfWriter_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
    {
        PdfWriter_Examples obj = new PdfWriter_Examples();

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


    // This code segment creates a new PdfWriter object with a
    // ByteArrayOutputStream object. It then writes a line of text 
    // on the PdfWriter object and saves it to a file.    
    public void memoryWriter_ByteArrayOutputStream_Example()
        throws PdfException, IOException
    {
        // Creates a ByteArrayOutputStream object
        ByteArrayOutputStream boas = new ByteArrayOutputStream();

        // Creates a new PdfWriter object with the
        // ByteArrayOutputStream object
        PdfWriter writer = PdfWriter.memoryWriter(boas);

        // Creates a PdfDocument object with the new PdfWriter object
        PdfDocument document = new PdfDocument(writer);

        // Writes a line of text to the document
        document.writeText("A new file was created using PdfWriter,"
                           + " ByteArrayOutputStream, and String!");

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

        // Sets the output file to be used by the
        // ByteArrayOutputStream object
        OutputStream os = new FileOutputStream(
                              ".\\pdfwriter_memoryWriter_"
                              + "ByteArrayOutputStream_"
                              + "example.pdf");

        // Writes to the output file
        boas.writeTo(os);

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

    // This code segment creates a new PdfWriter object with a
    // String object. It then writes a line of text on the PdfWriter
    // object and saves it to a file.
    public void fileWriter_String_Example() throws IOException,
        PdfException
    {
         // Creates a new PdfWriter object based on a File object
        PdfWriter writer = PdfWriter.fileWriter(
                               ".\\pdfwriter_fileWriter_"
                               + "String_example.pdf");

        // Creates a PdfDocument object from the new PdfWriter object
        PdfDocument document = new PdfDocument(writer);

        // Writes a line of text to the document
        document.writeText("A new file was created using PdfWriter,"
                           + " fileWriter, and String!");

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

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

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

    // This code segment creates a PdfWriter object with a file
    // output stream. It then writes a line of text on the object and
    // saves it to a file.
    public void streamWriter_OutputStream_Example()
        throws PdfException, IOException
    {

        // Creates a new OutputStream object from a file output
        // stream
        OutputStream os = new FileOutputStream(
                              ".\\pdfwriter_streamWriter_"
                              + "OutputStream_example.pdf");

        // Creates a new PdfWriter object from the OutputStream
        // object
        PdfWriter writer = PdfWriter.streamWriter(os);

        // Creates a PdfDocument object from the new PdfWriter object
        PdfDocument document = new PdfDocument(writer);

        // Writes a line of text to the document
        document.writeText("A new file was created using output"
                           + " stream os!");

        // Sets the PDF document to be opened after the save operation
        document.setOpenAfterSave(true);

        // Writes the PdfDocument object to a file
        document.write();
        // Closes I/O streams associated with this writer object
                writer.dispose();
    }

    // This code segment creates a new PdfWriter object with a
    // File object. It then writes a line of text on the PdfWriter
    // object and saves it to a file.
    public void fileWriter_File_Example() throws IOException,
        PdfException
    {
        // Creates a new PdfWriter object based on a File object
        PdfWriter writer = PdfWriter.fileWriter(
                               new File(
                                   ".\\pdfwriter_fileWriter_File_"
                                   + "example.pdf"));

        // Creates a PdfDocument object from the new PdfWriter object        
        PdfDocument document = new PdfDocument(writer);

        // Writes a line of text to the document
        document.writeText("A new file was created using PdfWriter,"
                           + " fileWriter, and File!");

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

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

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

    }

    // This code segment creates a PdfWriter object with a file
    // output stream. It then writes a line of text on the object and
    // saves it to a file.
    public void fileStreamWriter_FileOutputStream_Example()
        throws PdfException, IOException
    {

        // Creates a new PdfWriter object from a new FileOutPutStream
        // object
        PdfWriter writer = PdfWriter.fileStreamWriter(
                               new FileOutputStream(
                                  ".\\pdfwriter_fileStreamWriter_"
                                  + "FileOutputStream_example.pdf"));

        // Creates a PdfDocument object from the new PdfWriter object
        PdfDocument document = new PdfDocument(writer);

        // Writes a line of text to the document
        document.writeText("A new file was created using PdfWriter, "
                           + "fileStreamWriter, and "
                           + "FileOutputStream!");

        // Sets the PDF document to be opened after the save operation
        document.setOpenAfterSave(true);

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

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