"Fossies" - the Fresh Open Source Software Archive

Member "PDFlib-Lite-7.0.5p3/bind/pdflib/cpp/starter_image.cpp" (6 Jun 2012, 6688 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) C and C++ 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. For more information about "starter_image.cpp" see the Fossies "Dox" file reference documentation.

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