unitvalues (unitvalues.cpp) – this example shows how to work with different units. It creates a document with several pages, each created with a different unit and then reads the document and verifies that the stored page size matches the size used to create the page.

    1 /*
    2  * (c) 2013-2017 http://www.litePDF.cz
    3  *
    4  * The example code is supplied "AS IS". It disclaims all warranties, expressed
    5  * or implied, including, without limitation, the warranties of merchantability
    6  * and of fitness for any purpose. It assumes no liability for direct, indirect,
    7  * incidental, special, exemplary, or consequential damages, which may result
    8  * from the use of the code, even if advised of the possibility of such damage.
    9  *
   10  * Permission is hereby granted to use, copy, modify, and distribute this
   11  * source code, or portions hereof, for any purpose, without fee.
   12  */ 
   13 
   14 #include <windows.h>
   15 #include <stdio.h>
   16 #include <string.h>
   17 #include <string>
   18 
   19 #include "share/litePDF.h"
   20 
   21 #define ThrowIfFail(_expr) do {                                                \
   22       if (!(_expr)) {                                                          \
   23          std::string ttmsg;                                                    \
   24          ttmsg = std::string(_func) + ": Assertion '" + (#_expr) + "' failed"; \
   25          throw TLitePDFException(ERROR_INVALID_PARAMETER, ttmsg.c_str());      \
   26       }                                                                        \
   27    } while (false)
   28 
   29 static void createPage(TLitePDF &litePDF,
   30                        TLitePDFUnit unit,
   31                        int page_width_u,
   32                        int page_height_u,
   33                        double multiplier,
   34                        const char *text)
   35 {
   36    const char *_func = "unitvalues::createPage";
   37 
   38    litePDF.SetUnit(unit);
   39 
   40    // add a new page
   41    HDC hDC = litePDF.AddPage(page_width_u, page_height_u, page_width_u * multiplier, page_height_u * multiplier, LitePDFDrawFlag_SubstituteFonts);
   42 
   43    // draw the text
   44    LOGFONTA lf = {0, };
   45    lf.lfHeight = -50;
   46    strcpy(lf.lfFaceName, "Arial");
   47 
   48    HFONT fnt;
   49    HGDIOBJ oldFnt;
   50 
   51    fnt = CreateFontIndirect(&lf);
   52    oldFnt = SelectObject(hDC, fnt);
   53 
   54    SetTextColor(hDC, RGB(128, 0, 0));
   55    TextOut(hDC, 100, 100, text, strlen(text));
   56 
   57    SelectObject(hDC, oldFnt);
   58    DeleteObject(fnt);
   59 
   60    // finish drawing
   61    litePDF.FinishPage(hDC);
   62 }
   63 
   64 #define MMToInch(x) ((x) / 25.4)
   65 #define InchToMM(x) ((x) * 25.4)
   66 #define DoubleEqual(a,b) ((a) - (b) >= -1e-9 && (a) - (b) <= 1e-9)
   67 
   68 int main(void)
   69 {
   70    const char *_func = "unitvalues::main";
   71    int res = 0;
   72 
   73    TLitePDF litePDF;
   74    try {
   75       struct _pages {
   76          TLitePDFUnit unit;
   77          unsigned int width_u, height_u;
   78          double scale;
   79          const char *text;
   80          double width_mm, height_mm;
   81          double width_in, height_in;
   82       } pages[] = {
   83          { LitePDFUnit_mm, 210, 297, 10.0, "Unit 1 mm, 210 x 297",
   84             210.0, 297.0, MMToInch(210.0), MMToInch(297.0) },
   85          { LitePDFUnit_10th_mm, 2971, 2102, 1.0, "Unit 1/10 mm, 297.1 x 210.2",
   86             297.1, 210.2, MMToInch(297.1), MMToInch(210.2) },
   87          { LitePDFUnit_100th_mm, 21003, 29705, 0.1, "Unit 1/100 mm, 210.03 x 297.05",
   88             210.03, 297.05, MMToInch(210.03), MMToInch(297.05) },
   89          { LitePDFUnit_1000th_mm, 201567, 101234, 0.01, "Unit 1/1000 mm, 201.567 x 101.234",
   90             201.567, 101.234, MMToInch(201.567), MMToInch(101.234) },
   91          { LitePDFUnit_inch, 4, 5, 25.4 * 10.0, "Unit 1 inch, 4 x 5",
   92             InchToMM(4.0), InchToMM(5.0), 4.0, 5.0 },
   93          { LitePDFUnit_10th_inch, 85, 110, 25.4 * 1.0, "Unit 1/10 inch, 8.5 x 11",
   94             InchToMM(8.5), InchToMM(11.0), 8.5, 11.0 },
   95          { LitePDFUnit_100th_inch, 432, 567, 25.4 * 0.1, "Unit 1/100 inch, 4.32 x 5.67",
   96             InchToMM(4.32), InchToMM(5.67), 4.32, 5.67 },
   97          { LitePDFUnit_1000th_inch, 4598, 7623, 25.4 * 0.01, "Unit 1/1000 inch, 4.598 x 7.623",
   98             InchToMM(4.598), InchToMM(7.623), 4.598, 7.623 },
   99          { LitePDFUnit_Unknown, -1, -1, -1.0, NULL, -1.0, -1.0, -1.0, -1.0 }
  100       };
  101       int ii;
  102 
  103       ThrowIfFail(litePDF.GetUnit() == LitePDFUnit_mm);
  104 
  105       // begin write-only PDF file
  106       litePDF.CreateFileDocument("unitvalues-1.pdf");
  107 
  108       for (ii = 0; pages[ii].text; ii++) {
  109          createPage(litePDF, pages[ii].unit, pages[ii].width_u, pages[ii].height_u, pages[ii].scale, pages[ii].text);
  110       }
  111 
  112       // close the document
  113       litePDF.Close();
  114 
  115       litePDF.LoadFromFile("unitvalues-1.pdf", "", true);
  116       unsigned int width_u, height_u;
  117 
  118       ThrowIfFail(litePDF.GetPageCount() == (unsigned int) ii);
  119 
  120       for (ii = 0; pages[ii].text; ii++) {
  121          litePDF.SetUnit(pages[ii].unit);
  122          ThrowIfFail(litePDF.GetUnit() == pages[ii].unit);
  123 
  124          litePDF.GetPageSize(ii, &width_u, &height_u);
  125          ThrowIfFail(width_u == pages[ii].width_u);
  126          ThrowIfFail(height_u == pages[ii].height_u);
  127 
  128          width_u = litePDF.MMToUnit(pages[ii].width_mm) + 0.1; // add 0.1 to "round up"
  129          height_u = litePDF.MMToUnit(pages[ii].height_mm) + 0.1; // add 0.1 to "round up"
  130          ThrowIfFail(width_u == pages[ii].width_u);
  131          ThrowIfFail(height_u == pages[ii].height_u);
  132 
  133          width_u = litePDF.InchToUnit(pages[ii].width_in) + 0.1; // add 0.1 to "round up"
  134          height_u = litePDF.InchToUnit(pages[ii].height_in) + 0.1; // add 0.1 to "round up"
  135          ThrowIfFail(width_u == pages[ii].width_u);
  136          ThrowIfFail(height_u == pages[ii].height_u);
  137 
  138          ThrowIfFail(DoubleEqual(litePDF.UnitToInchEx(pages[ii].unit, pages[ii].width_u), pages[ii].width_in));
  139          ThrowIfFail(DoubleEqual(litePDF.UnitToMMEx(pages[ii].unit, pages[ii].width_u), pages[ii].width_mm));
  140       }
  141 
  142       litePDF.Close();
  143    } catch (TLitePDFException &ex) {
  144       fprintf (stderr, "litePDF Exception: %x: %s\n", ex.getCode(), ex.getMessage());
  145       res = 1;
  146    }
  147 
  148    return res;
  149 }
TOPlist