fromdoc (fromdoc.cpp) – this example demonstrates how to create a PDF file from an existing file, including page append and insert. It creates a source PDF document with three pages, on each is written the page number in the source document. Then it creates several documents based on it, namely one with only the second and the third page, one with the first page inserted as the second page and the last with the third page, after which is added a new page which has drawn the second page of the source document as a resource twice on it, and it finally inserts page 1 as the second page to the destination document.

    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 #include <string>
   26 
   27 #include "share/litePDF.h"
   28 
   29 static void addPage(litePDF::TLitePDF &litePDF,
   30                     unsigned int pageWidth,
   31                     unsigned int pageHeight,
   32                     const char *msg)
   33 {
   34    // add a new page to it, with large-enough pixel scale
   35    HDC hDC = litePDF.AddPage(litePDF.MMToUnit(pageWidth), litePDF.MMToUnit(pageHeight),
   36                              pageWidth * 10, pageHeight * 10,
   37                              LitePDFDrawFlag_SubstituteFonts);
   38 
   39    // msg == NULL adds an empty page
   40    if (msg) {
   41       // draw the text
   42       LOGFONTA lf = {0, };
   43       lf.lfHeight = -50; // ~5mm
   44       strcpy(lf.lfFaceName, "Helvetica");
   45 
   46       HFONT fnt;
   47       HGDIOBJ oldFnt;
   48 
   49       fnt = CreateFontIndirect(&lf);
   50       oldFnt = SelectObject(hDC, fnt);
   51 
   52       SetTextColor(hDC, RGB(0, 0, 0));
   53       TextOut(hDC, 100, 100, msg, strlen(msg));
   54 
   55       SelectObject(hDC, oldFnt);
   56       DeleteObject(fnt);
   57    }
   58 
   59    // finish drawing
   60    litePDF.FinishPage(hDC);
   61 }
   62 
   63 int main(void)
   64 {
   65    int res = 0;
   66 
   67    using namespace litePDF;
   68 
   69    try {
   70       TLitePDF litePDFfrom, litePDFto;
   71 
   72       // create a document
   73       litePDFfrom.CreateMemDocument();
   74 
   75       // create the source document's pages
   76       addPage(litePDFfrom, 210, 297, "Page 1");
   77       addPage(litePDFfrom, 210, 297, "Page 2");
   78       addPage(litePDFfrom, 210, 297, "Page 3");
   79 
   80       // save to file
   81       litePDFfrom.SaveToFile("fromdoc-1.pdf");
   82 
   83       // close the document
   84       litePDFfrom.Close();
   85 
   86       //-----------------------------------------------------------------
   87 
   88       // load from file
   89       litePDFfrom.LoadFromFile("fromdoc-1.pdf", NULL, false);
   90 
   91       //-----------------------------------------------------------------
   92 
   93       // create a new document
   94       litePDFto.CreateMemDocument();
   95 
   96       // copy all, but the first page
   97       litePDFto.AddPagesFrom(&litePDFfrom, 1, litePDFfrom.GetPageCount() - 1);
   98 
   99       // save to file
  100       litePDFto.SaveToFile("fromdoc-2.pdf");
  101 
  102       // close the document
  103       litePDFto.Close();
  104 
  105       //-----------------------------------------------------------------
  106 
  107       // create a new document
  108       litePDFto.CreateMemDocument();
  109 
  110       // copy all, but the first page
  111       litePDFto.AddPagesFrom(&litePDFfrom, 1, litePDFfrom.GetPageCount() - 1);
  112 
  113       // insert page 0 as page 1 - note, page inserting is PDF-resource hungry
  114       litePDFto.InsertPageFrom(1, &litePDFfrom, 0);
  115 
  116       // save to file
  117       litePDFto.SaveToFile("fromdoc-3.pdf");
  118 
  119       // close the document
  120       litePDFto.Close();
  121 
  122       //-----------------------------------------------------------------
  123 
  124       // create a new document
  125       litePDFto.CreateMemDocument();
  126 
  127       // copy the third page
  128       litePDFto.AddPagesFrom(&litePDFfrom, 2, 1);
  129 
  130       // add new empty page, it has index 1
  131       addPage(litePDFto, 297, 210, NULL);
  132 
  133       // copy page 2 as a resource
  134       unsigned int resourceID = litePDFto.AddPageFromAsResource(&litePDFfrom, 1);
  135 
  136       // draw the added page (twice)
  137       litePDFto.DrawResource(resourceID, 1, LitePDFUnit_10th_mm,
  138          litePDFto.MMToUnitEx(LitePDFUnit_10th_mm, 10),
  139          litePDFto.MMToUnitEx(LitePDFUnit_10th_mm, 10),
  140          litePDFto.MMToUnitEx(LitePDFUnit_10th_mm, 0.3),
  141          litePDFto.MMToUnitEx(LitePDFUnit_10th_mm, 0.3));
  142       litePDFto.DrawResource(resourceID, 1, LitePDFUnit_10th_mm,
  143          litePDFto.MMToUnitEx(LitePDFUnit_10th_mm, 150),
  144          litePDFto.MMToUnitEx(LitePDFUnit_10th_mm, 150),
  145          litePDFto.MMToUnitEx(LitePDFUnit_10th_mm, 0.2),
  146          litePDFto.MMToUnitEx(LitePDFUnit_10th_mm, 0.2));
  147 
  148       // insert page 0 as page 1 - note, page inserting is PDF-resource hungry
  149       litePDFto.InsertPageFrom(1, &litePDFfrom, 0);
  150 
  151       // save to file
  152       litePDFto.SaveToFile("fromdoc-4.pdf");
  153 
  154       // close the document
  155       litePDFto.Close();
  156 
  157       //-----------------------------------------------------------------
  158 
  159       // close the source document
  160       litePDFfrom.Close();
  161    } catch (TLitePDFException &ex) {
  162       fprintf(stderr, "litePDF Exception: %x: %s\n", ex.getCode(), ex.getMessage());
  163       res = 1;
  164    }
  165 
  166    return res;
  167 }