import java.io.IOException;

import com.gnostice.pdfone.PDFOne;
import com.gnostice.pdfone.PdfDocument;
import com.gnostice.pdfone.PdfException;
import com.gnostice.pdfone.PdfImage;
import com.gnostice.pdfone.PdfMeasurement;
import com.gnostice.pdfone.PdfPage;
import com.gnostice.pdfone.PdfWriter;

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

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

    // This code segment creates a page 600 points wide and 800
    // points long. However, it uses a method that specifies page
    // dimensions in inches. Hence, the convertToMeasurementUnit
    // method is used to convert point values to inches.
    public void convertToMeasurementUnit_Example()
        throws IOException, PdfException
    {
        PdfWriter writer = PdfWriter.fileWriter(
            "PdfMeasurement_convertToMeasurementUnit_Example.pdf");
        PdfDocument document = new PdfDocument(writer);

        // Converts page dimensions from points to inches
        double b = PdfMeasurement.convertToMeasurementUnit(
                                      PdfMeasurement.MU_INCHES, 
                                      600);
        double h = PdfMeasurement.convertToMeasurementUnit(
                                      PdfMeasurement.MU_INCHES, 
                                      800);

        // Creates a page with above dimensions
        PdfPage page = new PdfPage(b, h, PdfMeasurement.MU_INCHES);

        // Writes text identifying the page
        page.writeText(
                "This page is 600 points wide and 800 points long!",
                2, 1);
        page.writeText(
                "It was created with method PdfPage(b, h, "
                + "PdfMeasurement.MU_INCHES)",
                2, 1.2);
        page.writeText(
                "where b = " + b 
                + " inches and h = " + h
                + " inches.", 
                2, 1.4);

        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 draws an image using a method that requires
    // the image dimensions to be specified in points. However, the
    // image dimensions are known only in pixels. Hence, the
    // convertToPdfUnit method is used to convert pixel values to
    // points.
    public void convertToPdfUnit_Example() throws IOException,
        PdfException
    {
        PdfWriter writer = PdfWriter.fileWriter(
            "PdfMeasurement_convertToPdfUnit_Example.pdf");

        PdfDocument document = new PdfDocument(writer);

        // Creates a PdfImage object. Assumes that an image file
        // banner_img_468_60.jpg exists inside a "InputDocs"
        // directory in the current directory. Also assumes that
        // the image is assumed 468 pixels wide and 60 pixels tall.
        PdfImage image = PdfImage.create("banner_img_468_60.jpg");

        // Converts image dimensions from pixels to points
        double b = PdfMeasurement.convertToPdfUnit(
                                      PdfMeasurement.MU_PIXELS,
                                      468);
        double h = PdfMeasurement.convertToPdfUnit(
                                      PdfMeasurement.MU_PIXELS,
                                      60);

        // Draws the image with its height and width specified in
        // points. Requires width and height arguments to be
        // specified in points.
        document.drawImage(image, 200, 100, b, h);

        // Writes text identifying the image
        document.writeText("b = " + b + " points", 200, 60);
        document.writeText("h = " + h + " points", 200, 70);
        document.writeText("drawImage(image, 200, 100, b, h)",
                           200,
                           80);

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