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