"Fossies" - the Fresh Open Source Software Archive

Member "PDFlib-Lite-7.0.5p3/bind/pdflib/tcl/starter_table.tcl" (6 Jun 2012, 6213 Bytes) of package /linux/misc/old/PDFlib-Lite-7.0.5p3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Tcl/Tk source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "starter_table.tcl" see the Fossies "Dox" file reference documentation.

    1 #!/bin/sh
    2 # $Id: starter_table.tcl,v 1.2.2.1 2008/03/20 21:31:51 rjs Exp $
    3 #
    4 # Hide the exec to Tcl but not to the shell by appending a backslash\
    5 exec tclsh "$0" ${1+"$@"}
    6 
    7 # Table starter:
    8 # Create table which may span multiple pages
    9 #
   10 # required software: PDFlib/PDFlib+PDI/PPS 7
   11 # required data: image file (dummy text created within the program)
   12 
   13 # The lappend line is unnecessary if PDFlib has been installed
   14 # in the Tcl package directory
   15 set auto_path [linsert $auto_path 0 .libs .]
   16 
   17 package require pdflib 7.0
   18 
   19 # This is where the data files are. Adjust as necessary.
   20 set searchpath "../data"
   21 set imagefile "nesrin.jpg"
   22 set outfilename "starter_table.pdf"
   23 
   24 set tf -1
   25 set tbl -1
   26 set rowmax 50
   27 set colmax 5
   28 set llx 50
   29 set lly 50
   30 set urx 550
   31 set ury 800
   32 
   33 set headertext "Table header (centered across all columns)"
   34 
   35 # Dummy text for filling a cell with multi-line Textflow
   36 set tf_text "Lorem ipsum dolor sit amet, consectetur adi­pi­sicing elit, sed do eius­mod tempor incidi­dunt ut labore et dolore magna ali­qua. Ut enim ad minim ve­niam, quis nostrud exer­citation ull­amco la­bo­ris nisi ut ali­quip ex ea commodo con­sequat. Duis aute irure dolor in repre­henderit in voluptate velit esse cillum dolore eu fugiat nulla pari­atur. Excep­teur sint occae­cat cupi­datat non proident, sunt in culpa qui officia dese­runt mollit anim id est laborum. "
   37 
   38 set p [PDF_new]
   39 
   40 if { [catch {
   41     # This means we must check return values of load_font() etc.
   42     PDF_set_parameter $p "errorpolicy" "return"
   43 
   44     PDF_set_parameter $p "SearchPath" $searchpath
   45 
   46     if {[PDF_begin_document $p $outfilename ""] == -1} {
   47     puts stderr "Error: [PDF_get_errmsg $p]"
   48     }
   49 
   50     PDF_set_info $p "Creator" "PDFlib starter sample"
   51     PDF_set_info $p "Title" "starter_table"
   52 
   53     # -------------------- Add table cells --------------------
   54 
   55     # ---------- row 1: table header (spans all columns)
   56     set row 1
   57     set col  1
   58     set font [PDF_load_font $p "Times-Bold" "unicode" ""]
   59     if {$font == -1} {
   60     puts stderr "Error: [PDF_get_errmsg $p]"
   61     exit
   62     }
   63 
   64     set optlist [format "fittextline={position=center font=%d " $font]
   65     append optlist [format "fontsize=14} colspan=%d" $colmax]
   66 
   67     set tbl [PDF_add_table_cell $p $tbl $col $row $headertext $optlist]
   68     if {$tbl == -1} {
   69     puts stderr "Error: [PDF_get_errmsg $p]"
   70     exit
   71     }
   72 
   73     # ---------- row 2: various kinds of content
   74     # ----- Simple text cell
   75     set row [expr $row + 1]
   76     set col 1
   77 
   78     set optlist [format "fittextline={font=%d fontsize=10 orientate=west}" $font]
   79 
   80     set tbl [PDF_add_table_cell $p $tbl $col $row "vertical line" $optlist]
   81     if {$tbl == -1} {
   82     puts stderr "Error: [PDF_get_errmsg $p]"
   83     exit
   84     }
   85 
   86     # ----- Colorized background
   87     set col [expr $col + 1]
   88 
   89     set optlist [format "fittextline={font=%d fontsize=10} " $font]
   90     append optlist "matchbox={fillcolor={rgb 0.9 0.5 0}}"
   91 
   92     set tbl [PDF_add_table_cell $p $tbl $col $row "some color" $optlist]
   93     if {$tbl == -1} {
   94     puts stderr "Error: [PDF_get_errmsg $p]"
   95     exit
   96     }
   97 
   98     # ----- Multi-line text with Textflow
   99     set col [expr $col + 1]
  100     set font [PDF_load_font $p "Times-Roman" "unicode" ""]
  101     if {$font == -1} {
  102     puts stderr "Error: [PDF_get_errmsg $p]"
  103     exit
  104     }
  105 
  106     set optlist "charref fontname=Times-Roman encoding=unicode fontsize=8 "
  107 
  108     set tf [PDF_add_textflow $p $tf $tf_text $optlist]
  109 
  110     set optlist [format "margin=2 textflow=%d" $tf]
  111 
  112     set tbl [PDF_add_table_cell $p $tbl $col $row "" $optlist]
  113     if {$tbl == -1} {
  114     puts stderr "Error: [PDF_get_errmsg $p]"
  115     exit
  116     }
  117 
  118     # ----- Rotated $image
  119     set col [expr $col + 1]
  120 
  121     set image [PDF_load_image $p "auto" $imagefile ""]
  122     if {$tbl == -1} {
  123     puts stderr "Couldn't load $image: [PDF_get_errmsg $p]"
  124     exit
  125     }
  126 
  127     set optlist [format "image=%d fitimage={orientate=west}" $image]
  128 
  129     set tbl [PDF_add_table_cell $p $tbl $col $row "" $optlist]
  130     if {$tbl == -1} {
  131     puts stderr "Error: [PDF_get_errmsg $p]"
  132     exit
  133     }
  134 
  135     # ----- Diagonal stamp
  136     set col [expr $col + 1]
  137 
  138     set optlist [format "fittextline={font=%d fontsize=10 stamp=ll2ur}" $font]
  139 
  140     set tbl [PDF_add_table_cell $p $tbl $col $row "entry void" $optlist]
  141     if {$tbl == -1} {
  142     puts stderr "Error: [PDF_get_errmsg $p]"
  143     exit
  144     }
  145 
  146     # ---------- Fill $row 3 and above with their numbers
  147     for {set row [expr $row + 1]} {$row <= $rowmax} {set row [expr $row + 1]} {
  148     for {set col 1} {$col <= $colmax} {set col [expr $col + 1]} {
  149         set num [format "Col %d/Row %d" $col $row]
  150         set optlist "colwidth=20% fittextline={font="
  151         append optlist [format "%s fontsize=10}" $font]
  152         set tbl [PDF_add_table_cell $p $tbl $col $row $num $optlist]
  153         if {$tbl == -1} {
  154         puts stderr "Error: [PDF_get_errmsg $p]"
  155         exit
  156         }
  157     }
  158     }
  159 
  160     # ---------- Place the table on one or more pages ----------
  161 
  162     #
  163     # Loop until all of the table is placed; create new pages
  164     # as long as more table instances need to be placed.
  165 
  166     set result "_boxfull"
  167     while {$result == "_boxfull"} {
  168     PDF_begin_page_ext $p 0 0 "width=a4.width height=a4.height"
  169 
  170     # Shade every other $row; draw lines for all table cells.
  171     # Add "showcells showborder" to visualize cell borders 
  172 
  173     set optlist "header=1 fill={{area=rowodd fillcolor={gray 0.9}}} "
  174     append optlist "stroke={{line=other}} "
  175 
  176     # Place the table instance
  177     set result [PDF_fit_table $p $tbl $llx $lly $urx $ury $optlist]
  178     if {$result == "_error"} {
  179         puts stderr "Couldn't place table: [PDF_get_errmsg $p]"
  180         exit
  181     }
  182 
  183     PDF_end_page_ext $p ""
  184 
  185     }
  186 
  187     # Check the $result; "_stop" means all is ok.
  188     if {$result != "_stop"} {
  189     if {$result == "_error"} {
  190         puts stderr "Error when placing table: [PDF_get_errmsg $p]"
  191         exit
  192     } else {
  193         # Any other return value is a user exit caused by
  194         # the "return" option; this requires dedicated code to
  195         # deal with.
  196         puts stderr "User return found in Textflow"
  197         exit
  198     }
  199     }
  200 
  201     # This will also delete Textflow handles used in the table
  202     PDF_delete_table $p $tbl ""
  203 
  204     PDF_end_document $p ""
  205 
  206 } result] } {
  207     puts stderr "$result"
  208 }
  209 
  210 # delete the PDFlib object
  211 PDF_delete $p