color/color_gradient updated
Fill some area or text with a smooth transition from one color to another.
Download Java Code Switch to PHP Code Show Output
/*
* Color gradient:
* Fill some area or text with a smooth transition from one color to another
* Required software: PDFlib/PDFlib+PDI/PPS 9.1
* Required data: none
*/
package com.pdflib.cookbook.pdflib.color;
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;
public class color_gradient {
public static void main(String argv[]) {
/* This is where the data files are. Adjust as necessary. */
String searchpath = "../input";
String outfile = "color_gradient.pdf";
String title = "Color Gradient";
pdflib p = null;
int font, sh, pattern;
int fontsize = 36;
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, "destination={type=fitwindow}") == -1)
throw new Exception("Error: " + p.get_errmsg());
p.set_info("Creator", "PDFlib Cookbook");
p.set_info("Title", title);
/* Start Page */
p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
/*
* Fill the page completely with a gradient from green to black:
*
* Set the first color for the gradient to green with option
* "startcolor" and the second color to black with "endcolor".
*
* The parameters c1/c2/c3/c4 are ignored in this case.
*/
sh = p.shading("axial", 0, 0, 595, 842, 0, 0, 0, 0,
"startcolor={rgb 0.0 0.5 0.5} endcolor={rgb 0 0 0}");
/* Draw the gradient over the whole page */
p.shfill(sh);
/*
* Fill a rectangle with a gradient:
*
* Set the first color for the gradient to orange.
* Set the second color for the gradient to light orange.
* Define an axial gradient with a size similar to the size of the
* rectangle to be filled.
* Create a shading pattern.
*/
sh = p.shading("axial", 200, 200, 450, 450, 0, 0, 0, 0,
"startcolor={rgb 1.0 0.5 0.1} endcolor={rgb 0.9 0.8 0.8}");
pattern = p.shading_pattern(sh, "");
/*
* Draw a rectangle using the shading pattern as fill color.
*/
p.set_graphics_option("fillcolor={pattern " + pattern + "}");
p.rect(200, 200, 250, 250);
p.fill();
/*
* Fill a circle with a gradient:
*
* Set the first color for the gradient to white.
* Set the second color for the gradient to orange.
* Define a radial gradient with a size similar to the size
* of the circle to be filled.
* Create a shading pattern.
*/
sh = p.shading("radial", 400, 600, 400, 600, 0, 0, 0, 0,
"r0=0 r1=60 startcolor={rgb 1.0 1.0 1.0} endcolor={rgb 1.0 0.5 0.1}");
pattern = p.shading_pattern(sh, "");
/*
* Draw a circle using the shading pattern as fill color.
*/
p.set_graphics_option("fillcolor={pattern " + pattern + "}");
p.circle(400, 600, 50);
p.fill();
/*
* Fill a text with a gradient: Load the font.
*/
font = p.load_font("NotoSerif-Bold", "unicode", "");
if (font == -1)
throw new Exception("Error: " + p.get_errmsg());
/*
* Set the first color for the gradient to white.
* Set the second color for the gradient to orange.
* Define an axial gradient with a size similar to the size of
* the text to be filled.
* Create a shading pattern.
*/
sh = p.shading("axial", 50, 50, 50, 200, 0, 0, 0, 0,
"startcolor={rgb 1.0 1.0 1.0} endcolor={rgb 1.0 0.5 0.1}");
pattern = p.shading_pattern(sh, "");
/*
* Option list to use the shading pattern as the fill color
* for the text. Also specify font and font size.
*/
String optlist = "fillcolor={pattern " + pattern + "} "
+ "font=" + font + " fontsize=" + fontsize;
/*
* Output the text with the shading pattern as current fill color.
*/
p.fit_textline("Hello World!", 50, 100, optlist);
p.fit_textline("(says PDFlib GmbH)", 50, 100 - fontsize, optlist);
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);
}
}
}