PDFlib Cookbook

cookbook

text_output/transparent_text

Create some transparent text.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Transparent text:
 * Create some transparent text
 *
 * Display a background image and show transparent text in the foreground.
 * Create transparency by applying an extended graphics state with the
 * "opacityfill" option set to a value less than 1.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: image file
 */
package com.pdflib.cookbook.pdflib.text_output;

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

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

        pdflib p = null;
        
        String imagefile = "nesrin.jpg";
        int image, gstate;
        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 image */
            image = p.load_image("auto", imagefile, "");
            if (image == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* Start page */
            p.begin_page_ext(0, 0, "width=a4.height height=a4.width");
            
            
            /* Draw a yellow background rectangle */
            p.setcolor("fill", "rgb", 1, 0.9, 0.58, 0);
            p.rect(0, 0, 842, 595);
            p.fill(); 
            
            /* Output a background image */
            p.fit_image(image, 50, 50, "");
            
            /* Save the current graphics state. The save/restore of the current
             * state is not necessarily required, but it will help you get back to
             * a graphics state without any transparency.
             */
            p.save();
            
            /* Create an extended graphics state with transparency set to 50%, using
             * create_gstate() with the "opacityfill" option set to 0.5.
             */
            gstate = p.create_gstate("opacityfill=.5");
            
            /* Apply the extended graphics state */
            p.set_gstate(gstate);
            
            /* Display some red text which will be transparent according to the
             * graphics state set
             */
            p.setcolor("fill", "rgb", 1, 0.9, 0.58, 0);
            p.fit_textline("My favorite holiday photo", 100, 100, 
                "fontname=NotoSerif-Bold fontsize=50");
            
            /* 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);
        }
    }
}