BASKET
Search
PDFlib

images/align_text_at_image

Download Java Code     Switch to PHP Code    Show Output PDF

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

 * Align text at image:

 * Align text at an image

 *

 * Align text orientated to the west at the lower right corner of an image by

 * retrieving the coordinates of the image matchbox.

 * Align text orientated to the west at the lower right corner of an image

 * orientated to the west.

 *

 * 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 align_text_at_image

{

    public static void main (String argv[])

    {

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

        String searchpath = "../input";

        String outfile = "align_text_at_image.pdf";

        String title = "Align Text at Image";


        pdflib p = null;

        int exitcode = 0;

        String imagefile = "kraxi_logo.tif";

        String optlist;

        int font, image;

        double x1 = 0, x2 = 0, y1 = 0, y2 = 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.4 $");


            /* Start page */

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

            

            font = p.load_font("Helvetica", "unicode", "");

            if (font == -1)

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


            /* Load the image */

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

            if (image == -1)

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

            


            /* -----------------------------------------------------------------

             * Align text orientated to the west at the lower right corner of an

             * 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

             * small rectangle around the image with the "strokecolor" suboption

             * determining the border color.

             */

            

            /* Fit the image */

            optlist = "boxsize={300 200} position={center} " +

                "fitmethod=meet matchbox={name=giantwing borderwidth=3 " +

                "strokecolor={rgb 0.85 0.83 0.85}}";

            

            p.fit_image(image, 100, 500, optlist);

            

            /* Retrieve the coordinates of the second (lower right) matchbox corner.

             * The parameter "1" indicates the first instance of the "giantwing"

             * matchbox.

             */

            if ((int) p.info_matchbox("giantwing", 1, "exists") == 1) {

                    x2 = p.info_matchbox("giantwing", 1, "x2");

                y2 = p.info_matchbox("giantwing", 1, "y2");

            }

            

            /* Start the text line orientated to the west at the corner coordinates

             * retrieved (x2, y2) with a small offset of 3 or 2, respectively.

             */

            optlist = "font=" + font + " fontsize=12 orientate=west";

            

            p.fit_textline("Foto: Kraxi", x2+3, y2+2, optlist);

            

            

            /* -----------------------------------------------------------------

             * Align text orientated to the west at the lower right corner of an

             * image orientated to the west as well.

             * -----------------------------------------------------------------

             */

            

            /* Place the image in the center of a box using the "boxsize" and

             * "position" options. Maintain its proportions using "fitmethod=meet".

             * Using the "orientate" option orientate the image to the west. Use the

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

             * rectangle around the image with the "strokecolor" suboption

             * determining the border color.

             */

            optlist = "boxsize={200 300} position={center} fitmethod=meet " +

                "orientate=west matchbox={name=giantwing borderwidth=3 " +

                "strokecolor={rgb 0.85 0.83 0.85}}";

            

            p.fit_image(image, 100, 100, optlist);

            

            /* Retrieve the coordinates of the first matchbox corner; usually this

             * will be the lower left corner but with being orientated to the west

             * it will be moved to the bottom right. The parameter "2" indicates the

             * second instance of the "giantwing" matchbox.

             */

            if ((int) p.info_matchbox("giantwing", 2, "exists") == 1) {

                    x1 = p.info_matchbox("giantwing", 2, "x1");

                y1 = p.info_matchbox("giantwing", 2, "y1");

            }

            

            /* Start the text line orientated to the west at the corner coordinates

             * retrieved (x1, y1) with a small offset of 3 or 2, respectively.

             */

            optlist = "font=" + font + " fontsize=12 orientate=west";

            

            p.fit_textline("Foto: Kraxi", x1+3, y1+2, optlist);

            

            p.close_image(image);


            p.end_page_ext("");


            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);

        }

    }

}