"Fossies" - the Fresh Open Source Software Archive

Member "PDFlib-Lite-7.0.5p3/bind/pdflib/ruby/starter_image.rb" (6 Jun 2012, 5583 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_image.rb,v 1.1.2.2 2008/02/28 14:49:59 rjs Exp $
    3 # Starter image:
    4 # Load and place an image using various options for scaling and positioning
    5 #
    6 # Place the image it its original size.
    7 # Place the image with scaling and orientation to the west.
    8 # Fit the image into a box with clipping.
    9 # Fit the image into a box with proportional resizing.
   10 # Fit the image into a box entirely.
   11 #
   12 # Required software: PDFlib Lite/PDFlib/PDFlib+PDI/PPS 7
   13 # Required data: image file
   14 
   15 require 'PDFlib'
   16 
   17 # This is where the data files are. Adjust as necessary.
   18 searchpath = "../data"
   19 outfile = "starter_image.pdf"
   20 title = "Starter Image"
   21 
   22 imagefile = "lionel.jpg"
   23 bw = 400
   24 bh = 200
   25 x = 20
   26 y = 580
   27 yoffset = 260
   28 
   29 begin
   30     p = PDFlib.new
   31 
   32     p.set_parameter("SearchPath", searchpath)
   33 
   34     # This means we must check return values of load_font() etc.
   35     p.set_parameter("errorpolicy", "return")
   36 
   37     # we use "utf8" as textformat, this allows to use unicode encoding
   38     p.set_parameter("textformat", "utf8");
   39 
   40     if (p.begin_document(outfile, "") == -1)
   41         raise "Error: " + p.get_errmsg()
   42     end
   43 
   44     p.set_info("Creator", "PDFlib Cookbook")
   45     p.set_info("Title", title + " $Revision: 1.1.2.2 $")
   46 
   47     # For PDFlib Lite: change "unicode" to "winansi"
   48     font = p.load_font("Helvetica", "unicode", "")
   49     if font == -1
   50         raise "Error: " + p.get_errmsg + "\n"
   51     end
   52 
   53     # Load the image
   54     image = p.load_image("auto", imagefile, "")
   55     if image == -1
   56         raise "Error: " + p.get_errmsg + "\n"
   57     end
   58 
   59     # Start page 1
   60     p.begin_page_ext(0, 0, "width=a4.width height=a4.height")
   61     p.setfont(font, 12)
   62 
   63 
   64     # ------------------------------------
   65     # Place the image in its original size
   66     # ------------------------------------
   67 
   68 
   69     # Position the image in its original size with its lower left corner
   70     # at the reference point (20, 380)
   71 
   72     p.fit_image(image, 20, 380, "")
   73 
   74     # Output some descriptive text
   75     p.fit_textline("The image is placed with the lower left corner in " + \
   76         "its original size at reference point (20, 380):", 20, 820, "")
   77     p.fit_textline("fit_image(image, 20, 380, \"\")", 20, 800, "")
   78 
   79 
   80     # --------------------------------------------------------
   81     # Place the image with scaling and orientation to the west
   82     # --------------------------------------------------------
   83 
   84 
   85     # Position the image with its lower right corner at the reference
   86     # point (580, 20).
   87     # "scale=0.5" scales the image by 0.5.
   88     # "orientate=west" orientates the image to the west.
   89 
   90     p.fit_image(image, 580, 20, "scale=0.5 position={right bottom} " +
   91         "orientate=west")
   92 
   93     # Output some descriptive text
   94     p.fit_textline("The image is placed with a scaling of 0.5 and an " + \
   95         "orientation to the west with the lower right corner", 580, 320,
   96         "position={right bottom}")
   97     p.fit_textline(" at reference point (580, 20): fit_image(image, 580, " + \
   98         "20, \"scale=0.5 orientate=west position={right bottom}\")",
   99         580, 300, "position={right bottom}")
  100 
  101     p.end_page_ext("")
  102 
  103     # Start page 2
  104     p.begin_page_ext(0, 0, "width=a4.width height=a4.height")
  105     p.setfont(font, 12)
  106 
  107 
  108     # --------------------------------------
  109     # Fit the image into a box with clipping
  110     # --------------------------------------
  111 
  112 
  113     # The "boxsize" option defines a box with a given width and height and
  114     # its lower left corner located at the reference point.
  115     # "position={right top}" positions the image on the top right of the
  116     # box.
  117     # "fitmethod=clip" clips the image to fit it into the box.
  118 
  119     p.fit_image(image, x, y, "boxsize={" + bw.to_s + " " + bh.to_s + "} " + \
  120         "position={right top} fitmethod=clip")
  121 
  122     # Output some descriptive text
  123     p.fit_textline("fit_image(image, x, y, \"boxsize={400 200} " + \
  124         "position={right top} fitmethod=clip\")", 20, y+bh+10, "")
  125 
  126 
  127     # ---------------------------------------------------
  128     # Fit the image into a box with proportional resizing
  129     # ---------------------------------------------------
  130 
  131 
  132     # The "boxsize" option defines a box with a given width and height and
  133     # its lower left corner located at the reference point.
  134     # "position={center}" positions the image in the center of the
  135     # box.
  136     # "fitmethod=meet" resizes the image proportionally until its height
  137     # or width completely fits into the box.
  138     # The "showborder" option is used to illustrate the borders of the box.
  139 
  140     p.fit_image(image, x, y-=yoffset, "boxsize={" + bw.to_s + " " + bh.to_s + "} " + \
  141         "position={center} fitmethod=meet showborder")
  142 
  143     # Output some descriptive text
  144     p.fit_textline("fit_image(image, x, y, \"boxsize={400 200} " +
  145         "position={center} fitmethod=meet showborder\")", 20, y+bh+10, "")
  146 
  147 
  148     # ---------------------------------
  149     # Fit the image into a box entirely
  150     # ---------------------------------
  151 
  152 
  153     # The "boxsize" option defines a box with a given width and height and
  154     # its lower left corner located at the reference point.
  155     # By default, the image is positioned in the lower left corner of the
  156     # box.
  157     # "fitmethod=entire" resizes the image proportionally until its height
  158     # or width completely fits into the box.
  159 
  160     p.fit_image(image, x, y-=yoffset, "boxsize={" + bw.to_s + " " + bh.to_s + "} " + \
  161         "fitmethod=entire")
  162 
  163     # Output some descriptive text
  164     p.fit_textline("fit_image(image, x, y, \"boxsize={400 200} " + \
  165         "fitmethod=entire\")", 20, y+bh+10, "")
  166 
  167     p.end_page_ext("")
  168 
  169     p.close_image(image)
  170 
  171     p.end_document("")
  172 
  173 rescue  PDFlibException => pe
  174   print "PDFlib exception occurred:\n"
  175   print "[" + pe.get_errnum.to_s + "] " + pe.get_apiname + \
  176         ": " + pe.get_errmsg + "\n"
  177 rescue  Exception => e
  178   print e.backtrace.join("\n") + "\n" + e.to_s
  179 ensure
  180   p.delete() if p
  181 end
  182 
  183 # vim: sw=4