PDFlib Cookbook

cookbook

images/image_mask

Place an image and apply a mask to it.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Image mask:
 * Place an image with a mask applied to it
 * 
 * Place an image and apply a grayscale mask on it. Depending on the values
 * contained in the mask, the image pixels will be displayed more or less 
 * transparent. Smaller (darker) mask values result in more transparent image 
 * pixels and larger (lighter) values in less transparency.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: image file, grayscale mask image file
 */
package com.pdflib.cookbook.pdflib.images;

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

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

        pdflib p = null;
        int exitcode = 0;
        String imagefile = "sky.jpg";
        String maskfile = "image_mask.jpg";
        int font, image, mask, masked_image;
      
        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);
            
            /* Load the original image */
            image = p.load_image("auto", imagefile, "");
            if (image == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /*
             * Load the grayscale image to be used as soft mask.
             * It must not have an ICC profile, as an image with an ICCBased
             * colorspace cannot be used as a mask.
             */
            mask = p.load_image("auto", maskfile, "honoriccprofile=false");
            if (mask == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* Load the image with the mask assigned to it */
            masked_image = p.load_image("auto", imagefile, "masked " + mask);
            if (masked_image == -1)
                throw new Exception("Error: " + p.get_errmsg());
            
            /* Load font */
            font = p.load_font("NotoSerif-Regular", "unicode", "");
            if (font == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* Start a page with a size similar to the image dimensions. */
            p.begin_page_ext(0, 0, "width=a4.height height=a4.width");
            
            /* Place the original image */
            p.fit_image(image, 50, 150, "boxsize {200 500} fitmethod=meet");
            p.fit_textline("Original image", 50, 130, "font=" + font +
                " fontsize=10");
            
            /* Place the image without the clipping path applied to it */
            p.fit_image(mask, 300, 150, "boxsize {200 500} fitmethod=meet");
            p.fit_textline("Soft mask", 300, 130, "font=" + font +
                " fontsize=10");
            
            /* Place the image with the clipping path applied to it */
            p.fit_image(masked_image, 550, 150, "boxsize {200 500} fitmethod=meet");
            p.fit_textline("Image with soft mask applied: darker mask", 550, 130,
                "font=" + font + " fontsize=10");
            p.fit_textline("values result in more transparent image " +
                "values.", 550, 110, "font=" + font + " fontsize=10");
                    
            /* Close the images */
            p.close_image(image);
            p.close_image(mask);
            p.close_image(masked_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);
        }
    }
}