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