drawtoresource (drawtoresource.cpp) – an example which draws an 'X' into a resource, and then draws it multiple times into a page, at different position and with different transformations, using the resource API of the litePDF library.

    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 unsigned int createResource(litePDF::TLitePDF &litePDF)
   22 {
   23    HDC hDC = litePDF.AddResource(100, 100, 100, 100, LitePDFDrawFlag_None);
   24 
   25    HGDIOBJ oldPen = SelectObject(hDC, GetStockObject(BLACK_PEN));
   26 
   27    MoveToEx(hDC, 0, 0, NULL);
   28    LineTo(hDC, 0, 10);
   29    LineTo(hDC, 45, 50);
   30    LineTo(hDC, 0, 90);
   31    LineTo(hDC, 0, 100);
   32    LineTo(hDC, 10, 100);
   33    LineTo(hDC, 50, 55);
   34    LineTo(hDC, 90, 100);
   35    LineTo(hDC, 100, 100);
   36    LineTo(hDC, 100, 90);
   37    LineTo(hDC, 55, 50);
   38    LineTo(hDC, 100, 10);
   39    LineTo(hDC, 100, 0);
   40    LineTo(hDC, 90, 0);
   41    LineTo(hDC, 50, 45);
   42    LineTo(hDC, 10, 0);
   43    LineTo(hDC, 0, 0);
   44 
   45    SelectObject(hDC, oldPen);
   46 
   47    // finish drawing into the resource
   48    return litePDF.FinishResource(hDC);
   49 }
   50 
   51 int main(void)
   52 {
   53    int res = 0;
   54 
   55    using namespace litePDF;
   56 
   57    try {
   58       TLitePDF litePDF;
   59 
   60       // create a document
   61       litePDF.CreateMemDocument();
   62 
   63       // create the resource
   64       unsigned int resourceID = createResource(litePDF);
   65 
   66       // add an empty page, with large-enough pixel scale
   67       HDC hDC = litePDF.AddPage(litePDF.MMToUnit(210), litePDF.MMToUnit(297), 2100, 2970, LitePDFDrawFlag_None);
   68       litePDF.FinishPage(hDC);
   69 
   70       // draw the resource
   71 
   72       // no need to convert, when the scale is 1 and the ratio 1:1 as well
   73       litePDF.DrawResource(resourceID, 0, LitePDFUnit_mm, 10, 10, 1, 1);
   74       // no need to convert, when the scale is 1 and the ratio 1:1 as well
   75       litePDF.DrawResource(resourceID, 0, LitePDFUnit_mm, 150, 10, 1, 1);
   76       litePDF.DrawResource(resourceID, 0, LitePDFUnit_10th_mm,
   77          litePDF.MMToUnitEx(LitePDFUnit_10th_mm, 150),
   78          litePDF.MMToUnitEx(LitePDFUnit_10th_mm, 120),
   79          litePDF.MMToUnitEx(LitePDFUnit_10th_mm, 0.3),
   80          litePDF.MMToUnitEx(LitePDFUnit_10th_mm, 0.3));
   81       litePDF.DrawResource(resourceID, 0, LitePDFUnit_10th_mm,
   82          litePDF.MMToUnitEx(LitePDFUnit_10th_mm, 10),
   83          litePDF.MMToUnitEx(LitePDFUnit_10th_mm, 150),
   84          litePDF.MMToUnitEx(LitePDFUnit_10th_mm, 0.5),
   85          litePDF.MMToUnitEx(LitePDFUnit_10th_mm, 1.5));
   86       litePDF.DrawResourceWithMatrix(resourceID, 0, 1.0, 0.3, -0.3, 1.2, 123, 153);
   87 
   88       // save to file
   89       litePDF.SaveToFile("drawtoresource-1.pdf");
   90 
   91       // close the document
   92       litePDF.Close();
   93    } catch (TLitePDFException &ex) {
   94       fprintf(stderr, "litePDF Exception: %x: %s\n", ex.getCode(), ex.getMessage());
   95       res = 1;
   96    }
   97 
   98    return res;
   99 }
TOPlist