import java.io.File;
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 PdfImage_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
{
PdfImage_Examples obj = new PdfImage_Examples();
// To try other examples, add the obj.<example_method>
// accordingly. For example, try:
// obj.FileDimensions_Example();
obj.Rotation_Example();
}
// This code segment creates a PdfImage object and draws it
// twice - one with measurement unit in inches and the other
// with pixels.
public void intro_Example() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
new File(
"pdfimage_intro_example.pdf"));
PdfDocument document = new PdfDocument(writer);
// Creates a PdfImage object for a 468x60 pixels image file
// located in the InputDocs directory of the current
// directory.
PdfImage image = PdfImage.create("banner_img_468_60.jpg");
document.setMeasurementUnit(PdfMeasurement.MU_INCHES);
image.setWidth(5);
image.setHeight(1);
document.drawImage(image, 1, 1);
document.setMeasurementUnit(PdfMeasurement.MU_PIXELS);
image.setWidth(5 * 96);
image.setHeight(1 * 96);
document.drawImage(image, 100, 300);
document.setOpenAfterSave(true);
document.write();
writer.dispose();
}
// This code segment creates a PdfImage object, obtains number of
// bits per component (pixel) and writes the value to page.
public void bitsPerComponent_Example() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
new File(
"pdfimage_bitspercomponent_"
+ "example.pdf"));
PdfDocument document = new PdfDocument(writer);
PdfPage page = new PdfPage();
page.setMeasurementUnit(PdfMeasurement.MU_INCHES);
// Creates a PdfImage object for banner_img_468_grey.jpg
// located in the InputDocs directory of the current
// directory.
PdfImage image = PdfImage.create(
"banner_img_468_60_8bpp.bmp");
// Obtains the number of bits per component and writes it to
// page.
page.writeText(
"Number of components in the colorspace : "
+ image.bitsPerComponent(),
2,
0.8);
// Draws the image for reference
page.drawImage(image, 1, 1);
document.add(page);
document.setOpenAfterSave(true);
document.write();
writer.dispose();
}
// This code segment creates a PdfImage object and draws it on the
// screen. It then obtains the object's height and width values
// and sets them to twice their original values.
public void ObjectDimensions_Example() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
new File(
"pdfimage_objectdimensions_example.pdf"));
PdfDocument document = new PdfDocument(writer);
PdfPage page = new PdfPage();
page.setMeasurementUnit(PdfMeasurement.MU_INCHES);
// Creates a PdfImage object for banner_img_468_60.jpg
// located in the InputDocs directory of the current
// directory.
PdfImage image = PdfImage.create(
"banner_img_160_50.jpg");
// Draws the object using its default height and width
page.writeText(
"Image drawn using default height and width",
2,
0.8);
page.drawImage(image, 2, 1);
// Obtains default height and weight values of the object
// into variables
float l = image.getHeight();
float b = image.getWidth();
// Sets height and width variables to twice the original
// values
l = l * 2;
b = b * 2;
// Sets new height and width values to the object
image.setHeight(l);
image.setWidth(b);
// Draws the object using new object dimensions
page.writeText(
"Image drawn with twice the height and width",
2,
3.8);
page.drawImage(image, 2, 4);
document.add(page);
document.setOpenAfterSave(true);
document.write();
writer.dispose();
}
// This code segment creates a PdfImage object and obtains the
// height and width of the image. It then writes these values to
// the page.
public void FileDimensions_Example() throws IOException,
PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
new File(
"pdfimage_height_example.pdf"));
PdfDocument document = new PdfDocument(writer);
document.setVersion(PdfDocument.VERSION_1_4);
PdfPage page = new PdfPage();
page.setMeasurementUnit(PdfMeasurement.MU_INCHES);
// Creates a PdfImage object for banner_img_468_60.jpg
// located in the InputDocs directory of the current
// directory.
PdfImage image = PdfImage.create(
"banner_img_468_60.jpg");
// Obtains the height of the image and writes the value to
// page
page.writeText("Banner_img_468_60.jpg has a height of: "
+ image.height()
+ " pixels",
2,
0.8);
// Obtains the width of the image and writes the value to
// page
page.writeText("Banner_img_468_60.jpg has a width of: "
+ image.width()
+ " pixels",
2,
1.2);
// Draws the image for reference
page.drawImage(image, 1, 1.6);
document.add(page);
document.setOpenAfterSave(true);
document.write();
writer.dispose();
}
// This code segment creates a PdfImage object and draws it on a
// page. It then obtains the object's rotation angle, adds a
// randomly chosen value (5 degrees) to it, and sets that as the
// new rotation angle. When the object is drawn again, the image
// is slanted.
public void Rotation_Example() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
new File(
"pdfimage_getrotation_example.pdf"));
PdfDocument document = new PdfDocument(writer);
PdfPage page = new PdfPage();
page.setMeasurementUnit(PdfMeasurement.MU_INCHES);
// Creates a PdfImage object for banner_img_468_60.jpg
// located in the InputDocs directory of the current
// directory.
PdfImage image = PdfImage.create(
"banner_img_468_60.jpg");
page.writeText(
"Image drawn with its scaled width set to 400 pixels",
200,
80);
page.drawImage(image, 1, 1);
// Obtains current rotation angle
float n = image.getRotation();
// Sets rotation angle to 5 degrees so that the image is
// slightly slanted
n = n + 5;
image.setRotation(n);
page.writeText("Image redrawn but slanted", 1, 2.8);
page.drawImage(image, 1, 3);
document.add(page);
document.setOpenAfterSave(true);
document.write();
writer.dispose();
}
// This code segment creates a PdfImage object for a given image
// file located in a specified path and then draws it on a
// document page.
public void create_Example() throws IOException, PdfException
{
PdfWriter writer = PdfWriter.fileWriter(
new File(
"pdfimage_create_example.pdf"));
PdfDocument document = new PdfDocument(writer);
PdfPage page = new PdfPage();
page.setMeasurementUnit(PdfMeasurement.MU_INCHES);
// Creates a PdfImage object using an image file
PdfImage image = PdfImage.create(
"banner_img_468_60.jpg");
page.writeText(
"Image (banner_img_468_60.jpg) created and drawn",
2,
1.8);
page.drawImage(image, 2, 2);
document.add(page);
document.setOpenAfterSave(true);
document.write();
writer.dispose();
}
}