podofodocument (podofodocument.cpp) – this example shows how to access the PoDoFo document. It creates a simple document and then checks how many internal objects contain a stream, which is printed at the end of the example. This is the only example which requires linking against litePDF.lib, due to usage of the PoDoFo classes.

    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 #include "podofo/podofo.h"
   20 
   21 // fills PDF content with a text from a helloworld example
   22 void fillContent(litePDF::TLitePDF &litePDF)
   23 {
   24    // add a new page to it, with large-enough pixel scale
   25    HDC hDC = litePDF.AddPage(litePDF.MMToUnit(210), litePDF.MMToUnit(297), 2100, 2970, LitePDFDrawFlag_None);
   26 
   27    // draw the text
   28    LOGFONTA lf = {0, };
   29    lf.lfHeight = -300; // ~1/10 of the page height
   30    strcpy(lf.lfFaceName, "Arial");
   31 
   32    HFONT fnt;
   33    HGDIOBJ oldFnt;
   34 
   35    fnt = CreateFontIndirect(&lf);
   36    oldFnt = SelectObject(hDC, fnt);
   37 
   38    SetTextColor(hDC, RGB(128, 0, 0));
   39    TextOut(hDC, 100, 100, "Hello World!", 12);
   40 
   41    SelectObject(hDC, oldFnt);
   42    DeleteObject(fnt);
   43 
   44    // finish drawing
   45    litePDF.FinishPage(hDC);
   46 }
   47 
   48 int main(void)
   49 {
   50    int res = 0;
   51 
   52    using namespace litePDF;
   53    using namespace PoDoFo;
   54 
   55    try {
   56       TLitePDF litePDF;
   57 
   58       // create an in-memory PDF doument
   59       litePDF.CreateMemDocument();
   60 
   61       // fill some content
   62       fillContent(litePDF);
   63 
   64       // obtain current PoDoFo document pointer
   65       PdfDocument *document = (PdfDocument *) litePDF.GetPoDoFoDocument();
   66       if (!document) {
   67          throw TLitePDFException(ERROR_INVALID_HANDLE,
   68                                  "Failed to obtain PoDoFo document");
   69       }
   70 
   71       // traverse all document objects and count those with streams
   72       int streams = 0, total = 0;
   73       PdfVecObjects *objs = document->GetObjects();
   74       if (!objs) {
   75          throw TLitePDFException(ERROR_INVALID_HANDLE,
   76                                  "Failed to get PdfVecObjects");
   77       }
   78 
   79       std::vector<PdfObject *>::const_iterator it, end = objs->end();
   80       for (it = objs->begin(); it != end; it++, total++) {
   81          PdfObject *obj = *it;
   82          if (obj && obj->HasStream()) {
   83             const PdfStream *strm = obj->GetStream();
   84             if (strm) {
   85                streams++;
   86             }
   87          }
   88       }
   89 
   90       // print the result
   91       printf ("Found %d out of %d objects with stream\n", streams, total);
   92 
   93       // close the document, without saving it
   94       litePDF.Close();
   95    } catch (TLitePDFException &ex) {
   96       fprintf (stderr, "litePDF Exception: %x: %s\n", ex.getCode(), ex.getMessage());
   97       res = 1;
   98    }
   99 
  100    return res;
  101 }
TOPlist