"Fossies" - the Fresh Open Source Software Archive

Member "PDFlib-Lite-7.0.5p3/bind/pdflib/ruby/starter_table.rb" (6 Jun 2012, 5635 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) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/usr/bin/env ruby
    2 # $Id: starter_table.rb,v 1.4.2.4 2008/02/28 14:49:59 rjs Exp $
    3 #
    4 # Table starter:
    5 # Create table which may span multiple pages
    6 #
    7 # required software: PDFlib/PDFlib+PDI/PPS 7
    8 # required data: image file (dummy text created within the program)
    9 #/
   10 
   11 require 'PDFlib'
   12 
   13 
   14 searchpath = "../data"
   15 
   16 imagefile = "nesrin.jpg"
   17 
   18 tf=-1
   19 tbl=-1
   20 rowmax=50
   21 colmax=5
   22 
   23 llx= 50
   24 lly=50
   25 urx=550
   26 ury=800
   27 headertext = "Table header (centered across all columns)"
   28 
   29 # Dummy text for filling a cell with multi-line Textflow
   30 tf_text = 
   31 "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. "
   32 
   33 begin
   34   p = PDFlib.new
   35 
   36   # This means we must check return values of load_font() etc.
   37   p.set_parameter("errorpolicy", "return")
   38 
   39   p.set_parameter("SearchPath", searchpath)
   40 
   41   # we use "utf8" as textformat, this allows to use unicode encoding
   42   p.set_parameter("textformat", "utf8");
   43 
   44   if (p.begin_document("starter_table.pdf", "") == -1)
   45     raise "Error: " + p.get_errmsg()
   46   end
   47 
   48   p.set_info("Creator", "PDFlib starter sample")
   49   p.set_info("Title", "starter_table")
   50 
   51   # -------------------- Add table cells --------------------
   52 
   53   # ---------- Row 1: table header (spans all columns)
   54   row = 1
   55   col = 1
   56   font = p.load_font("Times-Bold", "unicode", "")
   57 
   58   if font == -1
   59     raise "Error: " + p.get_errmsg + "\n"
   60   end
   61 
   62 
   63   optlist =
   64   "fittextline={position=center font=" + font.to_s + " fontsize=14} " +
   65   "colspan=" + colmax.to_s
   66 
   67   tbl = p.add_table_cell(tbl, col, row, headertext, optlist)
   68 
   69   if tbl == -1
   70     raise "Error: " + p.get_errmsg + "\n"
   71   end
   72 
   73   # ---------- Row 2: various kinds of content
   74   # ----- Simple text cell
   75   row += 1
   76   col=1
   77 
   78   optlist = "fittextline={font=" + font.to_s + " fontsize=10 orientate=west}"
   79 
   80   tbl = p.add_table_cell(tbl, col, row, "vertical line", optlist)
   81 
   82   if tbl == -1
   83     raise "Error: " + p.get_errmsg + "\n"
   84   end
   85 
   86   # ----- Colorized background
   87   col += 1
   88 
   89   optlist = "fittextline={font=" + font.to_s + " fontsize=10} " +
   90   "matchbox={fillcolor={rgb 0.9 0.5 0}}"
   91 
   92   tbl = p.add_table_cell(tbl, col, row, "some color", optlist)
   93 
   94   if tbl == -1
   95     raise "Error: " + p.get_errmsg + "\n"
   96   end
   97 
   98   # ----- Multi-line text with Textflow
   99   col += 1
  100   font = p.load_font("Times-Roman", "unicode", "")
  101 
  102   if font == -1
  103     raise "Error: " + p.get_errmsg + "\n"
  104   end
  105 
  106 
  107   optlist = "charref fontname=Times-Roman encoding=unicode fontsize=8 "
  108 
  109   tf = p.add_textflow(tf, tf_text, optlist)
  110   if tf == -1
  111     raise "Error: " + p.get_errmsg + "\n"
  112   end
  113 
  114   optlist = "margin=2 textflow=" + tf.to_s
  115 
  116   tbl = p.add_table_cell(tbl, col, row, "", optlist)
  117 
  118   if tbl == -1
  119     raise "Error: " + p.get_errmsg + "\n"
  120   end
  121 
  122   # ----- Rotated image
  123   col += 1
  124 
  125   image = p.load_image("auto", imagefile, "")
  126   if (image == -1)
  127     raise "Couldn't load image: " + p.get_errmsg()
  128   end
  129 
  130   optlist = "image=" + image.to_s + " fitimage={orientate=west}"
  131 
  132   tbl = p.add_table_cell(tbl, col, row, "", optlist)
  133 
  134   if tbl == -1
  135     raise "Error: " + p.get_errmsg + "\n"
  136   end
  137 
  138   # ----- Diagonal stamp
  139   col += 1
  140 
  141   optlist = "fittextline={font=" + font.to_s + " fontsize=10 stamp=ll2ur}"
  142 
  143   tbl = p.add_table_cell(tbl, col, row, "entry void", optlist)
  144 
  145   if tbl == -1
  146     raise "Error: " + p.get_errmsg + "\n"
  147   end
  148 
  149   # ---------- Fill row 3 and above with their numbers
  150   row += 1
  151   row.step(rowmax,1) do |row|
  152     1.step(colmax,1) do |col|
  153       num = "Col " + col.to_s + "/Row " + row.to_s
  154       optlist = "colwidth=20% fittextline={font=" + font.to_s + " fontsize=10}"
  155       tbl = p.add_table_cell(tbl, col, row, num, optlist)
  156 
  157       if tbl == -1
  158     raise "Error: " + p.get_errmsg + "\n"
  159       end
  160     end
  161   end
  162 
  163   # ---------- Place the table on one or more pages ----------
  164 
  165   #
  166   # Loop until all of the table is placed; create new pages
  167   # as long as more table instances need to be placed.
  168   #
  169   begin
  170     p.begin_page_ext(0, 0, "width=a4.width height=a4.height")
  171 
  172     # Shade every other row; draw lines for all table cells.
  173     # Add "showcells showborder" to visualize cell borders 
  174     optlist = "header=1 fill={{area=rowodd fillcolor={gray 0.9}}} " +
  175     "stroke={{line=other}} "
  176 
  177     # Place the table instance
  178     result = p.fit_table(tbl, llx, lly, urx, ury, optlist)
  179     if (result ==  "_error")
  180       raise "Couldn't place table: " + p.get_errmsg()
  181     end
  182 
  183     p.end_page_ext("")
  184 
  185   end while (result == "_boxfull")
  186 
  187   # Check the result; "_stop" means all is ok.
  188   if (!result == "_stop") then
  189     if (result ==  "_error")
  190       raise "Error when placing table: " + p.get_errmsg()
  191     else 
  192       # Any other return value is a user exit caused by
  193       # the "return" option; this requires dedicated code to
  194       # deal with.
  195       raise "User return found in Textflow"
  196     end
  197   end
  198 
  199   # This will also delete Textflow handles used in the table
  200   p.delete_table(tbl, "")
  201 
  202   p.end_document("")
  203 
  204 rescue  PDFlibException => pe
  205   print "PDFlib exception occurred:\n"
  206   print "[" + pe.get_errnum.to_s + "] " + pe.get_apiname + \
  207         ": " + pe.get_errmsg + "\n"
  208   print pe.backtrace.join("\n") + "\n" + pe.to_s
  209 rescue  Exception => e
  210   print e.backtrace.join("\n") + "\n" + e.to_s
  211 ensure
  212   p.delete() if p
  213 end
  214 
  215 # vim: sw=2