/*
* Image dimensions:
* Get the dimensions of an image for various purposes
*
* Place an image in its original size, retrieve its dimensions using the
* "matchbox" option and place text directly to the right of the image. Fit an
* image into a box and use the box dimensions to center text directly below
* the image. Create a page with the dimensions of an image and place the image
* so that it covers the page completely.
*
* Required software: PDFlib/PDFlib+PDI/PPS 10
* Required data: image file
*/
package com.pdflib.cookbook.pdflib.images;
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;
public class image_dimensions
{
public static void main (String argv[])
{
/* This is where the data files are. Adjust as necessary. */
String searchpath = "../input";
String outfile = "image_dimensions.pdf";
String title = "Image Dimensions";
pdflib p = null;
int exitcode = 0;
String imagefile = "websurfer.jpg";
String imagefile2 = "nesrin.jpg";
int image;
double x2 = 0.0, y2 = 0.0, y3 = 0.0;
double imagewidth, imageheight;
try {
p = new pdflib();
p.set_option("searchpath={" + searchpath + "}");
/* This means we must check return values of load_font() etc. */
p.set_option("errorpolicy=return");
if (p.begin_document(outfile, "") == -1)
throw new Exception("Error: " + p.get_errmsg());
p.set_info("Creator", "PDFlib Cookbook");
p.set_info("Title", title);
/* On page 1, place an image in its original size, retrieve its
* dimensions using the "matchbox" option and place text directly
* to the right of the image
*/
p.begin_page_ext(0, 0, "width=100 height=100");
/* Load the image */
image = p.load_image("auto", imagefile, "");
if (image == -1)
throw new Exception("Error: " + p.get_errmsg());
/* Fit the image with a matchbox called "web" respresenting
* the image dimensions */
p.fit_image(image, 20, 20, "matchbox={name=web}");
p.close_image(image);
/* Retrieve the coordinates of the lower right (x2, y2) and upper right
* (x3, Y3) image corners via the matchbox "web" specified above
*/
if ((int) p.info_matchbox("web", 1, "exists") == 1)
{
x2 = p.info_matchbox("web", 1, "x2");
y2 = p.info_matchbox("web", 1, "y2");
y3 = p.info_matchbox("web", 1, "y3");
}
/* Start the text line in x direction at the lower right corner of the
* image and in y direction 1/4 of the image height up
*/
p.fit_textline("www.kraxi.com", x2, y2 + (y3 - y2)/4,
"fontname=NotoSerif-Regular fontsize=8");
p.end_page_ext("");
/* On page 2, fit an image into a box and use the box dimensions to
* center text directly below the image
*/
p.begin_page_ext(0, 0, "width=100 height=100");
/* Load the image */
image = p.load_image("auto", imagefile2, "");
if (image == -1)
throw new Exception("Error: " + p.get_errmsg());
/* Fit the image with a matchbox called "web" respresenting
* the image dimension
*/
p.fit_image(image, 10, 20, "boxsize={80 50} position={center bottom} " +
"fitmethod=meet");
p.close_image(image);
/* Start the text line in x direction at the lower right corner of the
* image and in y direction 1/4 of the image height up
*/
p.fit_textline("My holiday photo", 10, 10,
"boxsize={80 50} position={center bottom} "
+ "fontname=NotoSerif-Regular fontsize=8");
p.end_page_ext("");
/* Create page 3 with the original dimensions of the image and place
* the image so that it covers the page completely
*
* Load the image
*/
image = p.load_image("auto", imagefile2, "");
if (image == -1)
throw new Exception("Error: " + p.get_errmsg());
/* Get the width and height of the image */
imagewidth = p.info_image(image, "imagewidth", "");
imageheight = p.info_image(image, "imageheight", "");
/* Start page 3 with the dimensions of the image to be placed */
p.begin_page_ext(imagewidth, imageheight, "");
/* Fit the image */
p.fit_image(image, 0, 0, "");
p.close_image(image);
p.end_page_ext("");
p.end_document("");
} catch (PDFlibException e) {
System.err.println("PDFlib exception occurred:");
System.err.println("[" + e.get_errnum() + "] " + e.get_apiname() +
": " + e.get_errmsg());
exitcode = 1;
} catch (Exception e) {
System.err.println(e);
exitcode = 1;
} finally {
if (p != null) {
p.delete();
}
System.exit(exitcode);
}
}
}