"Fossies" - the Fresh Open Source Software Archive

Member "PDFlib-Lite-7.0.5p3/bind/pdflib/ruby/starter_block.rb" (6 Jun 2012, 2835 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_block.rb,v 1.3.2.5 2008/05/09 10:11:55 rjs Exp $
    3 #
    4 # Block starter:
    5 # Import a PDF page containing, and process all blocks. The blocks are
    6 # retrieved via pCOS, and the block filling functions are used to
    7 # visualize the blocks on the output page. A real-world application would
    8 # of course fill the blocks with data retrieved from some external data
    9 # source.
   10 #
   11 # required software: PPS 7
   12 # required data: input PDF
   13 #/
   14 
   15 require 'PDFlib'
   16 
   17 # This is where the data files are. Adjust as necessary.
   18 searchpath = "../data"
   19 
   20 infile = "boilerplate.pdf"
   21 
   22 begin
   23   p = PDFlib.new
   24 
   25   # This means we must check return values of load_font() etc.
   26   p.set_parameter("errorpolicy", "return")
   27 
   28   p.set_parameter("SearchPath", searchpath)
   29 
   30   # we use "utf8" as textformat, this allows to use unicode encoding
   31   p.set_parameter("textformat", "utf8");
   32 
   33   if (p.begin_document("starter_block.pdf", "") == -1)
   34     raise "Error: " + p.get_errmsg()
   35   end
   36 
   37   p.set_info("Creator", "PDFlib starter sample")
   38   p.set_info("Title", "starter_block")
   39 
   40   # Open a PDF containing blocks
   41   indoc = p.open_pdi_document(infile, "")
   42   if (indoc == -1)
   43     raise "Error: " + p.get_errmsg()
   44   end
   45 
   46   # Open the first page
   47   page = p.open_pdi_page(indoc, 1, "")
   48   if (page == -1)
   49     raise "Error: " + p.get_errmsg()
   50   end
   51 
   52   width = p.pcos_get_number(indoc, "pages[0]/width")
   53   height = p.pcos_get_number(indoc, "pages[0]/height")
   54 
   55   p.begin_page_ext(width, height, "")
   56 
   57   # Place the imported page on the output page
   58   p.fit_pdi_page(page, 0, 0, "")
   59 
   60   # Query the number of blocks on the first page
   61   blockcount = p.pcos_get_number(indoc, "length:pages[0]/blocks")
   62 
   63   if (blockcount == 0)
   64     raise "Error: " + infile + "does not contain any PDFlib blocks"
   65   end
   66 
   67   # Loop over all blocks on the page
   68   0.step(blockcount-1,1) do |i|
   69     # Fetch the name and type of the i-th block on the first page 
   70     # (one of Text/Image/PDF)
   71     #
   72     blockname = p.pcos_get_string(indoc,
   73     "pages[0]/blocks[" + i.to_i.to_s + "]/Name")
   74 
   75     blocktype = p.pcos_get_string(indoc,
   76     "pages[0]/blocks[" + i.to_i.to_s + "]/Subtype")
   77 
   78     # Visualize all text blocks
   79     if (blocktype == "Text")
   80     optlist = "fontname=Helvetica encoding=unicode " +
   81         "fillcolor={rgb 1 0 0} bordercolor={gray 0} linewidth=0.25"
   82 
   83     # We simply use the blockname as content
   84     if (p.fill_textblock(page, blockname, blockname, optlist) == -1)
   85         print "Warning: " + p.get_errmsg()
   86     end
   87     end
   88   end
   89 
   90   p.end_page_ext("")
   91   p.close_pdi_page(page)
   92 
   93   p.end_document("")
   94   p.close_pdi_document(indoc)
   95 
   96 rescue  PDFlibException => pe
   97   print "PDFlib exception occurred:\n"
   98   print "[" + pe.get_errnum.to_s + "] " + pe.get_apiname + \
   99           ": " + pe.get_errmsg + "\n"
  100 rescue  Exception => e
  101   print e.backtrace.join("\n") + "\n" + e.to_s
  102 ensure
  103   p.delete() if p
  104 end
  105 
  106 # vim: sw=2