helloworld (helloworld.cpp) – the simplest example, which prints a “Hello World!” text into a page. It demonstrates how to create a document, add a page, draw into it and save the document into a file. This example uses the C++ interface.

    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 
   18 #include "share/litePDF.h"
   19 
   20 int main(void)
   21 {
   22    int res = 0;
   23 
   24    using namespace litePDF;
   25 
   26    try {
   27       TLitePDF litePDF;
   28 
   29       // begin write-only PDF file
   30       litePDF.CreateFileDocument("helloworld-1.pdf");
   31 
   32       // add a new page to it, with large-enough pixel scale
   33       HDC hDC = litePDF.AddPage(litePDF.MMToUnit(210), litePDF.MMToUnit(297), 2100, 2970, LitePDFDrawFlag_None);
   34 
   35       // draw the text
   36       LOGFONTA lf = {0, };
   37       lf.lfHeight = -300; // ~1/10 of the page height
   38       strcpy(lf.lfFaceName, "Arial");
   39 
   40       HFONT fnt;
   41       HGDIOBJ oldFnt;
   42 
   43       fnt = CreateFontIndirect(&lf);
   44       oldFnt = SelectObject(hDC, fnt);
   45 
   46       SetTextColor(hDC, RGB(128, 0, 0));
   47       TextOut(hDC, 100, 100, "Hello World!", 12);
   48 
   49       SelectObject(hDC, oldFnt);
   50       DeleteObject(fnt);
   51 
   52       // finish drawing
   53       litePDF.FinishPage(hDC);
   54 
   55       // close the document
   56       litePDF.Close();
   57    } catch (TLitePDFException &ex) {
   58       fprintf (stderr, "litePDF Exception: %x: %s\n", ex.getCode(), ex.getMessage());
   59       res = 1;
   60    }
   61 
   62    return res;
   63 }
TOPlist