color/blendmode updated
Demonstrate all blend modes available in PDF.
Download Java Code Switch to PHP Code Show Output
/*
* Blend modes:
* Demonstrate all blend modes which are available in PDF
*
* The following layers are used for demonstration:
* - an RGB image
* - SVG logo (with white background) and a black and white gradient
*
* Each blend mode is demonstrated twice, once with the image as background
* and once with the image as foreground.
*
* Required software: PDFlib/PDFlib+PDI/PPS 10
*
* Required data: image, SVG graphics
*/
package com.pdflib.cookbook.pdflib.color;
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;
public class blendmode {
static final double WIDTH = 170, HEIGHT = 170;
static void place_image(pdflib p, int image) throws PDFlibException {
/* Place image */
p.fit_image(image, 0, 0, "boxsize={" + WIDTH + " " + HEIGHT + "} "
+ "fitmethod=slice position={0 0}");
}
static void paint(pdflib p, int shp, int graphics) throws PDFlibException {
/* Place SVG graphics over top half of the image */
p.fit_graphics(graphics, 0.2 * WIDTH, 0.6 * HEIGHT,
"boxsize={ " + 0.6 * WIDTH + " " + 0.3 * HEIGHT
+ "} position={center} fitmethod=meet");
/* Paint shading pattern over lower half of image */
p.set_graphics_option("fillcolor={pattern " + shp + "}");
p.rect(0, 0, WIDTH, HEIGHT / 2);
p.fill();
}
/*
* Create a template which demonstrates a particular blend mode
*/
static int create_blendmode_sample(pdflib p, String blendmode,
boolean image_in_background, int shp, int image, int graphics)
throws PDFlibException {
int gs = p.create_gstate("blendmode=" + blendmode);
int templ = p.begin_template_ext(WIDTH, HEIGHT, "");
if (image_in_background) {
place_image(p, image);
p.set_gstate(gs); /* Set gstate with selected blend mode */
paint(p, shp, graphics);
}
else {
paint(p, shp, graphics);
p.set_gstate(gs); /* Set gstate with selected blend mode */
place_image(p, image);
}
p.end_template_ext(0, 0);
return templ;
}
public static void main(String argv[]) {
/* This is where the data files are. Adjust as necessary. */
String searchpath = "../input";
String graphicsFileName = "PDFlib-blue-logo-white-background.svg";
String imageFileName = "Lucy.jpg";
String outFileName = "blendmode.pdf";
String title = "Blend mode";
int exitcode = 0;
/* Borders for the table fitbox */
final double border = 30;
class blendmoderecord {
String name;
String notes;
blendmoderecord(String name, String notes) {
this.name = name;
this.notes = notes;
}
}
/* Records with all blend modes */
final blendmoderecord blendmodes[] = {
new blendmoderecord("Normal",
"no blending effect: foreground obscures background"),
new blendmoderecord("Darken", "symmetric mode"),
new blendmoderecord("Multiply", "symmetric mode"),
new blendmoderecord("ColorBurn", ""),
new blendmoderecord("Lighten", "symmetric mode"),
new blendmoderecord("Screen", "symmetric mode"),
new blendmoderecord("ColorDodge", ""),
new blendmoderecord("HardLight", ""),
new blendmoderecord("SoftLight", ""),
new blendmoderecord("Overlay", ""),
new blendmoderecord("Difference", "symmetric mode"),
new blendmoderecord("Exclusion", "symmetric mode"),
new blendmoderecord("Hue", ""),
new blendmoderecord("Saturation", ""),
new blendmoderecord("Color", ""), new blendmoderecord("Luminosity",
"Like \"Color\", but with foreground and background exchanged") };
pdflib p = null;
try {
p = new pdflib();
int i, row;
int tf = -1, tbl = -1;
String cellopts, result;
double pagewidth = 595, pageheight = 842;
final double margin = 6, rowheight = 180;
final double fontsize = 11;
p.set_option("searchpath={" + searchpath + "}");
/* This means that errors throw an exception */
p.set_option("errorpolicy=exception");
if (p.begin_document(outFileName, "") == -1)
throw new Exception("Error: " + p.get_errmsg());
p.set_info("Creator", "PDFlib Cookbook");
p.set_info("Title", title);
String tf_optlist = "fontname=NotoSerif-Regular fontsize=" + fontsize;
/* Set some general text options shared among all cells */
String textoptlist = "fittextline={" + tf_optlist + " position={left center}} " + " margin=" + margin
+ " ";
/* Load image */
int image = p.load_image("auto", imageFileName, "");
/* Load the SVG graphics */
int graphics = p.load_graphics("auto", graphicsFileName, "");
/* Create axial shading */
int sh = p.shading("axial", 0, HEIGHT / 2, WIDTH, HEIGHT / 2, 0, 0,
0, 0, "startcolor=black endcolor=white");
/* shading pattern handle */
int shp = p.shading_pattern(sh, "");
/*
* ----------------------------------
* Create table header for all cells
* ----------------------------------
*/
row = 1;
cellopts = textoptlist;
tbl = p.add_table_cell(tbl, 1, row, "Blend mode", cellopts);
cellopts = textoptlist;
tbl = p.add_table_cell(tbl, 2, row, "Notes", cellopts);
tf = p.add_textflow(-1,
"Image in background,\nlogo and shading in foreground",
tf_optlist + " lastalignment=center alignment=center");
cellopts = textoptlist + " textflow=" + tf;
tbl = p.add_table_cell(tbl, 3, row, "", cellopts);
tf = p.add_textflow(-1,
"Logo and shading in background,\nimage in foreground",
tf_optlist + " lastalignment=center alignment=center");
cellopts = textoptlist + " textflow=" + tf;
tbl = p.add_table_cell(tbl, 4, row, "", cellopts);
/*
* ------------------------------------------------
* Add a row for each blend mode
* ------------------------------------------------
*/
for (i = 0, row = 2; i < blendmodes.length; i++, row++) {
/*
* -------------------------------------------------------------
* Column 1: Blend mode name
* -------------------------------------------------------------
*/
cellopts = "colwidth=15% " + textoptlist;
tbl = p.add_table_cell(tbl, 1, row, blendmodes[i].name,
cellopts);
/*
* ---------------------------------------------
* Column 2: Notes
* ---------------------------------------------
*/
tf = p.add_textflow(-1, blendmodes[i].notes, tf_optlist);
cellopts = "colwidth=20% " + textoptlist + " textflow=" + tf;
tbl = p.add_table_cell(tbl, 2, row, "", cellopts);
/*
* -------------------------------------------------------------
* Column 3: image in background
* -------------------------------------------------------------
*/
int templ = create_blendmode_sample(p, blendmodes[i].name, true,
shp, image, graphics);
cellopts = "rowheight=" + rowheight + " margin=3 image="
+ templ;
tbl = p.add_table_cell(tbl, 3, row, "", cellopts);
/*
* -------------------------------------------------------------
* Column 4: image in foreground
* -------------------------------------------------------------
*/
templ = create_blendmode_sample(p, blendmodes[i].name, false,
shp, image, graphics);
cellopts = "rowheight=" + rowheight + " margin=3 image="
+ templ;
tbl = p.add_table_cell(tbl, 4, row, "", cellopts);
} /* for */
/*
* -----------------------------------------------------------------
* Fit the table. Using "header=1" the table header will include the
* first line. Using "line=horother linewidth=0.3" the ruling is
* specified with a line width of 0.3 for all horizontal lines.
* -----------------------------------------------------------------
*/
String tableoptlist =
"showgrid header=1 stroke={ {line=horother linewidth=0.3}}";
do {
p.begin_page_ext(pagewidth, pageheight, "");
/* Place the table instance */
result = p.fit_table(tbl, border, border, pagewidth - border,
pageheight - border, tableoptlist);
if (result.equals("_error"))
throw new Exception(
"Couldn't place table : " + p.get_errmsg());
p.end_page_ext("");
}
while (result.equals("_boxfull"));
/* Check the result; "_stop" means all is ok. */
if (!result.equals("_stop")) {
if (result.equals("_error")) {
throw new Exception(
"Error when placing table: " + p.get_errmsg());
}
}
/* This will also delete Textflow handles used in the table */
p.delete_table(tbl, "");
p.close_image(image);
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);
}
}
}