BASKET
Search
PDFlib

images/frame_around_image

Download Java Code      Switch to PHP Code      Show Output PDF

/* $Id: frame_around_image.java,v 1.6 2014/01/30 14:05:25 rjs Exp $

 * Frame around image:

 * Draw a frame around an image

 *

 * Place an image and draw a thick border around it using the "matchbox" option

 * and its "borderwidth" and "offset" suboptions.

 * On the second page, retrieve the matchbox of the placed image, create a

 * path object from it and draw the path with the desired line properties.

 *

 * Required software: PDFlib/PDFlib+PDI/PPS 9

 * Required data: image file

 */

package com.pdflib.cookbook.pdflib.images;


import com.pdflib.pdflib;

import com.pdflib.PDFlibException;


public class frame_around_image {

    public static void main(String argv[]) {

        /* This is where the data files are. Adjust as necessary. */

        String searchpath = "../input";

        String outfile = "frame_around_image.pdf";

        String title = "Frame around Image";


        pdflib p = null;

        String imagefile = "nesrin.jpg";

        int image;

        int exitcode = 0;


        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 + " $Revision: 1.6 $");


            /* Load the image */

            image = p.load_image("auto", imagefile, "");

            if (image == -1)

                throw new Exception("Error: " + p.get_errmsg());


            /* Start page */

            p.begin_page_ext(0, 0, "width=a4.height height=a4.width");


            /*

             * Define the option list for fit_image(): Place the image in the

             * center of a box using the "boxsize" and "position" options.

             * Maintain its proportions using "fitmethod=meet". Use the

             * "matchbox" option with the "borderwidth" suboption to draw a

             * thick rectangle around the image. The "strokecolor" suboption

             * determines the border color, and the "linecap" and "linejoin"

             * suboptions are used to round the corners. The matchbox is always

             * drawn before the image which means it would be hidden by the

             * image. To avoid this use the "offset" suboptions with 50 percent

             * of the border width to enlarge the frame beyond the area covered

             * by the image.

             */


            String optlist =

                "boxsize={400 300} position={center} fitmethod=meet "

                + "matchbox={borderwidth=10 offsetleft=-5 offsetright=5 "

                + "offsetbottom=-5 offsettop=5 linecap=round linejoin=round "

                + "strokecolor {rgb 0.0 0.3 0.3}}";


            /* Fit the image using the option list defined above */

            p.fit_image(image, 200, 150, optlist);


            p.end_page_ext("");

           

            p.begin_page_ext(0, 0, "width=a4.height height=a4.width");

           

            /*

             * Second variant: Retrieve matchbox and create a path from it.

             * Draw the path with the desired line properties. This method

             * works also with other fitmethods than "meet".

             */

           

            optlist =

                "boxsize={400 300} position={center} fitmethod=clip " +

                    "matchbox={name=border}";

           

            p.fit_image(image, 200, 150, optlist);

           

            int path = (int) p.info_matchbox("border", 1, "boundingbox");

            p.draw_path(path, 0, 0,

                "stroke close linewidth=10 linecap=round linejoin=round " +

                "strokecolor {rgb 0.0 0.3 0.3}");

            p.delete_path(path);

           

            p.end_page_ext("");


            p.close_image(image);


            p.end_document("");

        }

        catch (PDFlibException e) {

            System.err.print("PDFlib exception occurred:\n");

            System.err.print("[" + e.get_errnum() + "] " + e.get_apiname()

                + ": " + e.get_errmsg() + "\n");

            exitcode = 1;

        }

        catch (Exception e) {

            System.err.println(e.getMessage());

            exitcode = 1;

        }

        finally {

            if (p != null) {

                p.delete();

            }

            System.exit(exitcode);

        }

    }

}