PDFlib Cookbook

cookbook

images/clip_image

Clip image.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Clip image:
 *
 * Use the "matchbox" option with the "clipping" suboption to place a
 * rectangular part of the image.
 * - clip a rectangular area of the image and place it in its original scaling.
 * - clip a rectangular area of the image and force-fit into a box with
 *   specified size.
 *
 * 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 clip_image
{
    public static void main (String argv[])
    {
        /* This is where the data files are. Adjust as necessary. */
        String searchpath = "../input";
        String outfile = "clip_image.pdf";
        String title = "Clip image";

        pdflib p = null;
        int exitcode = 0;
        String imagefile = "nesrin.jpg";
        
        try {
            int image, gstate, font;
            String clipping_optlist, text;
            int llx = 50, lly = 550;

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

            /* Load the image */
            image = p.load_image("auto", imagefile, "");
            if (image == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            font = p.load_font("NotoSerif-Regular", "unicode", "");
            if (font == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
       
            // -------------------------------------------------------------
            // Output the original image with transparency as background
            // -------------------------------------------------------------
            p.begin_page_ext(0, 0, "width=a4.height height=a4.width");
            
            p.setfont(font, 10);
            
            /* Output some descriptive text */
            text = "Place the full image in its original size with transparency (for comparison)";
            p.fit_textline(text, llx, lly-=30, "");
            
            /* Create an extended graphics state with transparency */
            gstate = p.create_gstate("opacityfill=.4");
            
            /* Fit the image in its original size with the transparency gstate */
            p.fit_image(image, llx, lly-=500, "gstate=" + gstate);
            
            
            /* ----------------------------------------------
             * Place the clipped image in its original size
             * ----------------------------------------------
             */

            /* Define a rectangular part of the image with the "matchbox" option
             * and the "clipping" suboption to set the lower left and upper right
             * corners of the clipping rectangle.
             */
            int c_llx = 350, c_lly = 180, c_urx = 550, c_ury = 360;
            
            clipping_optlist = "matchbox={clipping={" + 
                c_llx + " " + c_lly + " " + c_urx + " " + c_ury + "}}";
            
            /* Output some descriptive text */
            text = "Place the clipped image in its original size:";
            p.fit_textline(text, llx + c_llx, 410, "");
            
            text = "p.fit_image(image, " + (llx + c_llx) + " , " + (lly + c_lly) +
                ", \"" + clipping_optlist + "\");";
            p.fit_textline(text, llx + c_llx, 395, "");
                      
            /* Display the clipped area at the same position where it would
             * appear when displaying the complete image.
             */
            p.fit_image(image, llx + c_llx, lly + c_lly, clipping_optlist);
            
            /* Output some descriptive text */
            
            text = "Place the clipped image in its original size";
            p.fit_textline(text, llx, 245, "");
            
            text = "at the lower left corner of the full image:";
            p.fit_textline(text, llx, 230, "");
            
            text = "p.fit_image(image, " + llx + " , " + lly + ", \"" + clipping_optlist + "\");";
            p.fit_textline(text, llx, 215, "");
            
            // Place the clipped image at the lower left corner of the complete image.
            p.fit_image(image, llx, lly, clipping_optlist);
            
            p.end_page_ext("");
            
                  
            /* -----------------------------------------------------
             * Display the clipped area in a box with specified size
             * -----------------------------------------------------
             */
            p.begin_page_ext(0, 0, "width=a4.height height=a4.width");
              
            p.setfont(font, 10);
      
            lly = 550;
                         
            /* Define a rectangular part of the image with the "matchbox" option
             * and the "clipping" suboption. The lower left and upper right corners
             * of the rectangle are specified as percentages of the original image size. 
             */
            clipping_optlist = "matchbox={clipping={35% 35% 75% 75%}}";
            
            text = "Place the clipped area in its original size:";
            p.fit_textline(text, llx, lly-=30, "");
            
            text = "p.fit_image(image, x, y, \"" + clipping_optlist + "\");";
            p.fit_textline(text, llx, lly-=15, "");
            
            /* Place the clipped image */
            p.fit_image(image, llx, lly-=220, clipping_optlist);
            
            /* Display the clipped image in a box of the specified size. Scale the
             * clipped image proportionally so that it fits into the specified box
             * with "fitmethod=meet"
             */
            clipping_optlist = "matchbox={clipping={35% 35% 75% 75%}} " +
                "boxsize={200 100} fitmethod=meet showborder";
            
            /* Output some descriptive text */
            text = "Place the clipped area in a specified box:";
            p.fit_textline(text, llx, lly-=50, "");
            
            text = "p.fit_image(image, x, y, \"" + clipping_optlist + "\");";
            p.fit_textline(text, llx, lly-=15, "");
            
            p.fit_image(image, llx, lly-=120, clipping_optlist);
            
            p.end_page_ext("");
            
            p.close_image(image);
            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);
        }
    }
}