1#!/usr/bin/python
2# Id: starter_layer.pl,v 1.1.2.3 2007/12/21 14:04:32 rjs Exp
3# Starter layer:
4# Define several layers, output images and text to them and define
5# particular layers to be visible when opening the document
6#
7# Define two layers for RGB or Grayscale images and two layers for English or
8# German image captions. Output images and text on the various layers and
9# open the document with the RGB images and English captions visible.
10#
11# Required software: PDFlib/PDFlib+PDI/PPS 7
12# Required data: grayscale and RGB images
13#
14
15from sys import *
16from pdflib_py import *
17
18# This is where the data files are. Adjust as necessary.
19searchpath = "../data"
20outfile = "starter_layer.pdf"
21title = "Starter Layer"
22
23rgb = "nesrin.jpg"
24gray = "nesrin_gray.jpg"
25
26# create a new PDFlib object
27p = PDF_new()
28
29try:
30  try:
31    PDF_set_parameter(p, "SearchPath", searchpath)
32
33    # This means we must check return values of load_font() etc.
34    PDF_set_parameter(p, "errorpolicy", "return")
35
36
37    # Open the document with the "Layers" navigation tab visible
38    if PDF_begin_document(p, outfile, "openmode=layers") == -1:
39        raise PDFlibException("Error: " + PDF_get_errmsg(p))
40
41    PDF_set_info(p, "Creator", "PDFlib Cookbook")
42    buf = title + ' Revision: 1.1.2.3 '
43    PDF_set_info(p, "Title", buf)
44
45    # Load the font
46    font = PDF_load_font(p, "Helvetica", "winansi", "")
47
48    if font == -1:
49        raise PDFlibException("Error: " + PDF_get_errmsg(p))
50
51    # Load the Grayscale image
52    imageGray = PDF_load_image(p, "auto", gray, "")
53    if imageGray == -1:
54        raise PDFlibException("Error: " + PDF_get_errmsg(p))
55
56    # Load the RGB image
57    imageRGB = PDF_load_image(p, "auto", rgb, "")
58    if imageRGB == -1:
59        raise PDFlibException("Error: " + PDF_get_errmsg(p))
60
61    # Define all layers which will be used, and their relationships.
62    # This should be done before the first page if the layers are
63    # used on more than one page.
64
65    # Define the layer "RGB"
66    layerRGB = PDF_define_layer(p, "RGB", "");
67
68    # Define the layer "Grayscale" which is hidden when opening the
69    # document or printing it.
70    layerGray = PDF_define_layer(p, "Grayscale",
71                "initialviewstate=false initialprintstate=false");
72
73    # At most one of the "Grayscale" and "RGB" layers should be visible
74    buf = "group={" + repr(layerGray) + " " + repr(layerRGB) + "}";
75    PDF_set_layer_dependency(p, "Radiobtn", buf);
76
77    # Define the layer "English"
78    layerEN = PDF_define_layer(p, "English", "");
79
80    # Define the layer "German" which is hidden when opening the document
81    # or printing it.
82    layerDE = PDF_define_layer(p, "German",
83                "initialviewstate=false initialprintstate=false");
84
85    # At most one of the "English" and "German" layers should be visible
86    buf = "group={" + repr(layerEN) + " " + repr(layerDE) + "}";
87    PDF_set_layer_dependency(p, "Radiobtn", buf);
88
89    # Start page
90    PDF_begin_page_ext(p, 0, 0, "width=a4.width height=a4.height");
91
92    # Place the RGB image on the layer "RGB"
93    PDF_begin_layer(p, layerRGB);
94    PDF_fit_image(p, imageRGB, 100, 400,
95                "boxsize={400 300} fitmethod=meet");
96
97    # Place the Grayscale image on the layer "Grayscale"
98    PDF_begin_layer(p, layerGray);
99    PDF_fit_image(p, imageGray, 100, 400,
100                "boxsize={400 300} fitmethod=meet");
101
102    # Place an English image caption on the layer "English"
103    PDF_begin_layer(p, layerEN);
104    buf =  "font=" + repr(font) + "  fontsize=20";
105    PDF_fit_textline(p, "This is the Nesrin image.", 100, 370, buf);
106
107    # Place a German image caption on the layer "German"
108    PDF_begin_layer(p, layerDE);
109    buf = "font=" + repr(font) + " fontsize=20";
110    PDF_fit_textline(p, "Das ist das Nesrin-Bild.", 100, 370, buf);
111    PDF_end_layer(p);
112
113    PDF_end_page_ext(p, "");
114
115    PDF_end_document(p, "");
116
117  except PDFlibException:
118    print("PDFlib exception occurred:\n[%d] %s: %s" %
119	((PDF_get_errnum(p)), PDF_get_apiname(p),  PDF_get_errmsg(p)))
120
121  except Exception:
122    print("Exception occurred: %s" % (exc_info()[0]))
123
124finally:
125    PDF_delete(p)
126