PDFlib Cookbook

cookbook

text_output/text_as_clipping_path

Output text filled with an image.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Text as clipping path:
 * Output text filled with an image 
 *  
 * Use various text rendering modes to place an image clipped by some text
 * outlines.
 * Use the "textrendering=7" parameter to add text to the clipping path. Then
 * place an image clipped by that path.
 * Use "textrendering=5" to stroke text and add it to the clipping path. Then
 * place an image clipped by that path.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: image file
 */
package com.pdflib.cookbook.pdflib.text_output;

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

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

        pdflib p = null;

        String imagefile = "nesrin.jpg";
        int font, 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);

            /* Load the font. */
            font = p.load_font("Helvetica-Bold", "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());

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

            /* Save the current graphics state */
            p.save();

            /*
             * Output some text with the text rendering mode set to "add text
             * to the clipping path". The text outlines will not actually be
             * filled but be added to the clipping path instead.
             */
            p.fit_textline("Hello!", 30, 250,
                "font=" + font + " fontsize=250 textrendering=7");

            /* Place the image */
            p.fit_image(image, 0.0, 0.0, "boxsize={842 595} fitmethod=entire");

            /* Restore the current graphics state */
            p.restore();

            /* Save the current graphics state */
            p.save();

            /*
             * Output some text with the text rendering mode set to "stroke 
             * text and add it to the clipping path". The text outlines will
             * not actually be filled but be added to the clipping path
             * instead.
             */
            p.set_graphics_option("linewidth=8");
            p.fit_textline("Hello!", 100, 50,
                "font=" + font + " fontsize=200 " +
                    "strokecolor={rgb 0.1 0.1 0.1} textrendering=5");

            /* Place the image */
            p.fit_image(image, 0.0, 0.0, "boxsize={842 595} fitmethod=entire");

            p.close_image(image);

            /* Restore the current graphics state */
            p.restore();

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