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