graphics/layers_and_bookmarks
Define two layers and hide or show them via bookmarks.
Download Java Code Switch to PHP Code Show Output
/*
* Layers and bookmarks:
* Define two layers and hide or show them via bookmarks
*
* Define two layers for English or German text. Create two bookmarks which
* show the English or the German layer, respectively.
*
* Required software: PDFlib/PDFlib+PDI/PPS 10
* Required data: none
*/
package com.pdflib.cookbook.pdflib.graphics;
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;
public class layers_and_bookmarks
{
public static void main (String argv[])
{
/* This is where the data files are. Adjust as necessary. */
String searchpath = "../input";
String outfile = "layers_and_bookmarks.pdf";
String title = "Layers and Bookmarks";
pdflib p = null;
int action, layerEN, layerDE;
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");
/* Open the document with the "Bookmarks" navigation tab visible */
if (p.begin_document(outfile, "openmode=bookmarks") == -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");
/* Define the layer "English" and place some English text on it */
layerEN = p.define_layer("English", "");
p.begin_layer(layerEN);
p.fit_textline("Our paper planes are the ideal way of passing the " +
"time.", 30, 600,
"fontname=NotoSerif-Bold fontsize=20");
/* Define the layer "German" which is hidden when opening the document
* or printing it. Place a German image caption on that layer.
*/
layerDE = p.define_layer("German", "initialviewstate=false " +
"initialprintstate=false");
p.begin_layer(layerDE);
p.fit_textline("Unsere Papierflieger sind ein idealer Zeitvertreib.",
30, 600,
"fontname=NotoSerif-Bold fontsize=20");
/* At most one of the "English" and "German" layers should be visible */
p.set_layer_dependency("Radiobtn", "group={" + layerEN + " " +
layerDE + "}");
/* Create a "SetOCGState" action which shows the English layer. Since
* at most one layer may be visible we don't need to explicitly hide
* the German layer.
*/
action = p.create_action("SetOCGState", "layerstate={on " +
layerEN + "}");
/* Create a bookmark which activate the action above to shows the layer
* with the English text
*/
p.create_bookmark("Show English", " action={activate=" + action + "}");
/* Create a "SetOCGState" action which shows the German layer. Since
* at most one layer may be visible we don't need to explicitly hide
* the English layer. */
action = p.create_action("SetOCGState", "layerstate={on " +
layerDE + "}");
/* Create a bookmark which activate the action above to shows the layer
* with the German text
*/
p.create_bookmark("Show German", " action={activate=" + action + "}");
/* Complete all layers */
p.end_layer();
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);
}
}
}