incrementalupdate (incrementalupdate.cpp) – this example shows how to incrementally update an existing document. It draws content to an existing and a new page, each version saved as the incremental update, then it also merges all the piled document versions into one and saves it.

    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 static void addPageWithText(litePDF::TLitePDF &litePDF,
   21                             const char *text)
   22 {
   23    // add a new page to it, with large-enough pixel scale
   24    HDC hDC = litePDF.AddPage(litePDF.MMToUnit(210), litePDF.MMToUnit(297), 2100, 2970, LitePDFDrawFlag_None);
   25 
   26    // draw the text
   27    LOGFONTA lf = {0, };
   28    lf.lfHeight = -60; // ~1/50 of the page height
   29    strcpy(lf.lfFaceName, "Arial");
   30 
   31    HFONT fnt;
   32    HGDIOBJ oldFnt;
   33 
   34    fnt = CreateFontIndirect(&lf);
   35    oldFnt = SelectObject(hDC, fnt);
   36 
   37    SetTextColor(hDC, RGB(0, 0, 0));
   38    TextOut(hDC, 100, 100, text, strlen (text));
   39 
   40    SelectObject(hDC, oldFnt);
   41    DeleteObject(fnt);
   42 
   43    // finish drawing
   44    litePDF.FinishPage(hDC);
   45 }
   46 
   47 static void drawSomething(litePDF::TLitePDF &litePDF,
   48                           int pageIndex,
   49                           COLORREF color)
   50 {
   51    // update existing page, with large-enough pixel scale
   52    HDC hDC = litePDF.UpdatePage(pageIndex, 2100, 2970, LitePDFDrawFlag_None);
   53 
   54    // prepare pen for drawing
   55    HPEN pen = CreatePen(PS_SOLID, 5, color);
   56    HGDIOBJ oldPen;
   57 
   58    oldPen = SelectObject(hDC, pen);
   59 
   60    // draw three primitives
   61    Rectangle(hDC, 150, 200, 200, 250);
   62    Ellipse(hDC, 210, 200, 260, 250);
   63    MoveToEx(hDC, 260, 250, NULL);
   64    LineTo(hDC, 340, 250);
   65    LineTo(hDC, 300, 200);
   66    LineTo(hDC, 260, 250);
   67 
   68    SelectObject(hDC, oldPen);
   69    DeleteObject(pen);
   70 
   71    // finish drawing
   72    litePDF.FinishPage(hDC);
   73 }
   74 
   75 int main(void)
   76 {
   77    int res = 0;
   78 
   79    using namespace litePDF;
   80 
   81    try {
   82       TLitePDF litePDF;
   83 
   84       // begin write-only PDF file
   85       litePDF.CreateFileDocument("incrementalupdate-1.pdf");
   86 
   87       // the first version of the document will contain only one page with a text
   88       addPageWithText (litePDF, "1st page");
   89 
   90       // close the document
   91       litePDF.Close();
   92 
   93       //-----------------------------------------------------------------
   94 
   95       // make a file copy
   96       CopyFile("incrementalupdate-1.pdf", "incrementalupdate-2.pdf", FALSE);
   97 
   98       //-----------------------------------------------------------------
   99 
  100       // open file for incremental update and load it completely, because will overwrite it
  101       litePDF.LoadFromFile("incrementalupdate-2.pdf", NULL, true, true);
  102 
  103       // update the first page
  104       drawSomething(litePDF, 0, RGB(128, 128, 128));
  105 
  106       // save changes to the same file
  107       litePDF.SaveToFile("incrementalupdate-2.pdf");
  108 
  109       // close the document
  110       litePDF.Close();
  111 
  112       //-----------------------------------------------------------------
  113 
  114       // open file for incremental update
  115       litePDF.LoadFromFile("incrementalupdate-2.pdf", NULL, false, true);
  116 
  117       // add the second page
  118       addPageWithText(litePDF, "2nd page");
  119 
  120       // save changes to a new file
  121       litePDF.SaveToFile("incrementalupdate-3.pdf");
  122 
  123       // close the document
  124       litePDF.Close();
  125 
  126       //-----------------------------------------------------------------
  127 
  128       // open file, this time not for incremental update,
  129       // which will make the save rewrite whole file content
  130       litePDF.LoadFromFile("incrementalupdate-3.pdf", NULL, false, false);
  131 
  132       // also change the second page
  133       drawSomething(litePDF, 1, RGB(0, 128, 0));
  134 
  135       // save to a new file
  136       litePDF.SaveToFile("incrementalupdate-4.pdf");
  137 
  138       // close the document
  139       litePDF.Close();
  140 
  141    } catch (TLitePDFException &ex) {
  142       fprintf (stderr, "litePDF Exception: %x: %s\n", ex.getCode(), ex.getMessage());
  143       res = 1;
  144    }
  145 
  146    return res;
  147 }
TOPlist