text_output/shadowed_text
Create a shadowed text line with the 'shadow' option of fit_textline.
Download Java Code Switch to PHP Code Show Output
/*
* Shadowed text:
* Create a shadowed text line
*
* To create a shadowed text line use the "shadow" option of fit_textline() and
* the suboptions "offset" for setting the position, "fillcolor" for defining
* the color, and "gstate" for applying advanced graphics settings for the
* shadow.
*
* 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 shadowed_text {
public static void main(String argv[]) {
/* This is where the data files are. Adjust as necessary. */
String searchpath = "../input";
String outfile = "shadowed_text.pdf";
String title = "Shadowed Text";
pdflib p = null;
int exitcode = 0;
final String imagefile = "cambodia_bayon3.jpg";
final String text = "the faces of Bayon";
final double x1 = 20, x2 = 360, yoff = 60;
double y = 550;
String base_shadow_opts, shadow_opts, smallfont_opts, bigfont_opts, color_opts;
int image, gstate;
int normalfont, boldfont;
double bigfontsize = 35, smallfontsize = 13;
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);
normalfont = p.load_font("NotoSerif-Regular", "unicode", "");
if (normalfont == -1)
throw new Exception("Error: " + p.get_errmsg());
boldfont = p.load_font("NotoSerif-Bold", "unicode", "");
if (boldfont == -1)
throw new Exception("Error: " + p.get_errmsg());
/* 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");
/* Define general options for small and big fonts */
smallfont_opts = "font=" + normalfont + " fontsize="
+ smallfontsize + " ";
bigfont_opts = "font=" + boldfont + " fontsize=" + bigfontsize
+ " ";
/* Ouput a heading */
p.fit_textline("Output of fit_textline()", x1, y, smallfont_opts);
p.fit_textline("Options of fit_textline()", x2, y, smallfont_opts);
/*
* ------------------------------------------------------
* Output shadowed text with default shadow option values
* ------------------------------------------------------
*/
shadow_opts = "shadow={} ";
p.fit_textline(text, x1, y -= yoff, bigfont_opts + shadow_opts);
/* Output a description of the shadow options */
p.fit_textline(shadow_opts, x2, y + bigfontsize / 4,
smallfont_opts);
/*
* -------------------------------------------------------------
* Output shadowed text with varied offset (Default is {5% -5%})
* -------------------------------------------------------------
*/
shadow_opts = "shadow={offset={10% 8%}} ";
p.fit_textline(text, x1, y -= yoff, bigfont_opts + shadow_opts);
/* Output a description of the shadow options */
p.fit_textline(shadow_opts, x2, y + bigfontsize / 4,
smallfont_opts);
/* Output shadowed text with default shadow option values */
shadow_opts = "shadow={offset={-10% 8%}} ";
p.fit_textline(text, x1, y -= yoff, bigfont_opts + shadow_opts);
/* Output a description of the shadow options */
p.fit_textline(shadow_opts, x2, y + bigfontsize / 4,
smallfont_opts);
/* Output shadowed text with default shadow option values */
shadow_opts = "shadow={offset={-10% -8%}} ";
p.fit_textline(text, x1, y -= yoff, bigfont_opts + shadow_opts);
/* Output a description of the shadow options */
p.fit_textline(shadow_opts, x2, y + bigfontsize / 4,
smallfont_opts);
/*
* -------------------------------------------
* Output shadowed text with varied fill color
* -------------------------------------------
*/
shadow_opts = "shadow={fillcolor={rgb 0.28 0.81 0.8} offset={10% -10%}} ";
color_opts = "fillcolor={rgb 0 0.5 0.52}";
p.fit_textline(text, x1, y -= yoff, bigfont_opts + shadow_opts
+ color_opts);
/* Output the shadow options */
p.fit_textline(shadow_opts, x2, y + bigfontsize / 4,
smallfont_opts);
/*
* 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();
/* Output a background image */
p.fit_image(image, x1, 50, "boxsize={170 170} fitmethod=meet");
/*
* 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");
/*
* Display some white shadowed text which will be transparent
* according to the graphics state supplied
*/
base_shadow_opts = "shadow={fillcolor={rgb 0.87 0.72 0.52} "
+ "offset={8% -8%}} gstate=";
shadow_opts = base_shadow_opts + gstate;
color_opts = " fillcolor={rgb 1 1 1}";
p.fit_textline("the", x1, 185, bigfont_opts + shadow_opts
+ color_opts);
p.fit_textline("faces", x1, 145, bigfont_opts + shadow_opts
+ color_opts);
p.fit_textline("of", x1, 105, bigfont_opts + shadow_opts
+ color_opts);
p.fit_textline("Bayon", x1, 65, bigfont_opts + shadow_opts
+ color_opts);
/* Restore the current graphics state */
p.restore();
/* Output the shadow options */
p.fit_textline(base_shadow_opts + "<handle>", x2, 145, smallfont_opts);
p.fit_textline("(gstate is created with \"opacityfill=.5\")", x2,
115, smallfont_opts);
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.toString());
exitcode = 1;
}
finally {
if (p != null) {
p.delete();
}
System.exit(exitcode);
}
}
}