PDFlib Cookbook

cookbook

path_objects/import_path_from_image

Import a path object from an image file and use it for filling, stroking or clipping.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Import a path object from an image file and use it for filling, stroking
 * or clipping
 * 
 * Use a TIFF or JPEG image containing a clipping path. Import the path
 * (while ignoring the pixel data) and use the resulting path object
 * for filling, stroking or clipping.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: TIFF or JPEG image containing a clipping path
 */
package com.pdflib.cookbook.pdflib.path_objects;

import com.pdflib.pdflib;
import com.pdflib.PDFlibException;

public class import_path_from_image
{
    public static void main (String argv[])
    {
        /* This is where the data files are. Adjust as necessary. */
        String searchpath = "../input";
        String outfile = "import_path_from_image.pdf";
        String title = "Import path from image";

        pdflib p = null;
        String imagefile = "child_clipped.jpg";
        int image;
        int exitcode = 0;

        double target_x = 50, target_y = 50;      // target location
      
        try {
            int path, bbox;
            
            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);

            /* Start a page */
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

            /* Load the image file with its integrated clipping path
             * Add the following option to retrieve a particular path by
             * name: "clippingpathname={Path 47}".
             * "infomode" means that the pixel data is ignored and
             * not loaded into the PDF to reduce output file size.
             */
            image = p.load_image("auto", imagefile, "infomode");
            if (image == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /*
             * You can supply formatting options here so that the
             * path will be formatted the same way as an image.
             */
            String formatting_options = "boxsize={300 300}";

            /* Supply the formatting options when retrieving
             * the clipping path to ensure correct scaling and placement.
             */
            path = (int) p.info_image(image, "clippingpath", formatting_options);
            
            if (path == -1)
                throw new Exception("Error: image '" + imagefile + "' doesn't contain clipping path");
            
            /* Mark the target location with a red circle for illustration */
            p.set_graphics_option("fillcolor=red");
            p.circle(target_x, target_y, 5);
            p.fill();

            p.save();

            /* Optional: retrieve the bounding box of the placed image and
             * clip the imported path to this box. This may be useful if the
             * image contains irrelevant additional paths outside the image
             * area. However, if you need the additional paths you should
             * skip the bbox clipping.
             */
            bbox = (int) p.info_image(image, "boundingbox", formatting_options);
            p.draw_path(bbox, target_x, target_y, "close clip");
            
            /* Draw the path at the target location */
            String pathoptlist =
                "close stroke strokecolor=red fill fillcolor=blue " +
                "linewidth=2 linejoin=round linecap=round ";
            p.draw_path(path, target_x, target_y, pathoptlist + formatting_options);
            
            p.restore();

            // Delete the path objects
            p.delete_path(path);
            p.delete_path(bbox);

            /* Close the image */
            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);
        }
    }
}