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-2016 http://www.litePDF.cz
    3  * (c) 2017 zyx [@:] zyx gmx [dot] us
    4  *
    5  * This software is provided 'as-is', without any express or implied
    6  * warranty.  In no event will the authors be held liable for any damages
    7  * arising from the use of this software.
    8  *
    9  * Permission is granted to anyone to use this software for any purpose,
   10  * including commercial applications, and to alter it and redistribute it
   11  * freely, subject to the following restrictions:
   12  *
   13  * 1. The origin of this software must not be misrepresented; you must not
   14  *    claim that you wrote the original software. If you use this software
   15  *    in a product, an acknowledgment in the product documentation would be
   16  *    appreciated but is not required.
   17  * 2. Altered source versions must be plainly marked as such, and must not be
   18  *    misrepresented as being the original software.
   19  * 3. This notice may not be removed or altered from any source distribution.
   20  */ 
   21 
   22 #include <windows.h>
   23 #include <stdio.h>
   24 #include <string.h>
   25 
   26 #include "share/litePDF.h"
   27 
   28 int main(void)
   29 {
   30    int res = 0;
   31 
   32    using namespace litePDF;
   33 
   34    try {
   35       TLitePDF litePDF;
   36 
   37       // begin write-only PDF file
   38       litePDF.CreateFileDocument("helloworld-1.pdf");
   39 
   40       // add a new page to it, with large-enough pixel scale
   41       HDC hDC = litePDF.AddPage(litePDF.MMToUnit(210), litePDF.MMToUnit(297), 2100, 2970, LitePDFDrawFlag_None);
   42 
   43       // draw the text
   44       LOGFONTA lf = {0, };
   45       lf.lfHeight = -300; // ~1/10 of the page height
   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, "Hello World!", 12);
   56 
   57       SelectObject(hDC, oldFnt);
   58       DeleteObject(fnt);
   59 
   60       // finish drawing
   61       litePDF.FinishPage(hDC);
   62 
   63       // close the document
   64       litePDF.Close();
   65    } catch (TLitePDFException &ex) {
   66       fprintf (stderr, "litePDF Exception: %x: %s\n", ex.getCode(), ex.getMessage());
   67       res = 1;
   68    }
   69 
   70    return res;
   71 }