unitvalues (
unitvalues.cpp) –
this example shows how to work with different units. It creates a document
with several pages, each created with a different unit and then reads
the document and verifies that the stored page size matches the size
used to create the page.
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 #define ThrowIfFail(_expr) do { \
30 if (!(_expr)) { \
31 std::string ttmsg; \
32 ttmsg = std::string(_func) + ": Assertion '" + (#_expr) + "' failed"; \
33 throw TLitePDFException(ERROR_INVALID_PARAMETER, ttmsg.c_str()); \
34 } \
35 } while (false)
36
37 static void createPage(TLitePDF &litePDF,
38 TLitePDFUnit unit,
39 int page_width_u,
40 int page_height_u,
41 double multiplier,
42 const char *text)
43 {
44 const char *_func = "unitvalues::createPage";
45
46 litePDF.SetUnit(unit);
47
48 // add a new page
49 HDC hDC = litePDF.AddPage(page_width_u, page_height_u, page_width_u * multiplier, page_height_u * multiplier, LitePDFDrawFlag_SubstituteFonts);
50
51 // draw the text
52 LOGFONTA lf = {0, };
53 lf.lfHeight = -50;
54 strcpy(lf.lfFaceName, "Arial");
55
56 HFONT fnt;
57 HGDIOBJ oldFnt;
58
59 fnt = CreateFontIndirect(&lf);
60 oldFnt = SelectObject(hDC, fnt);
61
62 SetTextColor(hDC, RGB(128, 0, 0));
63 TextOut(hDC, 100, 100, text, strlen(text));
64
65 SelectObject(hDC, oldFnt);
66 DeleteObject(fnt);
67
68 // finish drawing
69 litePDF.FinishPage(hDC);
70 }
71
72 #define MMToInch(x) ((x) / 25.4)
73 #define InchToMM(x) ((x) * 25.4)
74 #define DoubleEqual(a,b) ((a) - (b) >= -1e-9 && (a) - (b) <= 1e-9)
75
76 int main(void)
77 {
78 const char *_func = "unitvalues::main";
79 int res = 0;
80
81 TLitePDF litePDF;
82 try {
83 struct _pages {
84 TLitePDFUnit unit;
85 unsigned int width_u, height_u;
86 double scale;
87 const char *text;
88 double width_mm, height_mm;
89 double width_in, height_in;
90 } pages[] = {
91 { LitePDFUnit_mm, 210, 297, 10.0, "Unit 1 mm, 210 x 297",
92 210.0, 297.0, MMToInch(210.0), MMToInch(297.0) },
93 { LitePDFUnit_10th_mm, 2971, 2102, 1.0, "Unit 1/10 mm, 297.1 x 210.2",
94 297.1, 210.2, MMToInch(297.1), MMToInch(210.2) },
95 { LitePDFUnit_100th_mm, 21003, 29705, 0.1, "Unit 1/100 mm, 210.03 x 297.05",
96 210.03, 297.05, MMToInch(210.03), MMToInch(297.05) },
97 { LitePDFUnit_1000th_mm, 201567, 101234, 0.01, "Unit 1/1000 mm, 201.567 x 101.234",
98 201.567, 101.234, MMToInch(201.567), MMToInch(101.234) },
99 { LitePDFUnit_inch, 4, 5, 25.4 * 10.0, "Unit 1 inch, 4 x 5",
100 InchToMM(4.0), InchToMM(5.0), 4.0, 5.0 },
101 { LitePDFUnit_10th_inch, 85, 110, 25.4 * 1.0, "Unit 1/10 inch, 8.5 x 11",
102 InchToMM(8.5), InchToMM(11.0), 8.5, 11.0 },
103 { LitePDFUnit_100th_inch, 432, 567, 25.4 * 0.1, "Unit 1/100 inch, 4.32 x 5.67",
104 InchToMM(4.32), InchToMM(5.67), 4.32, 5.67 },
105 { LitePDFUnit_1000th_inch, 4598, 7623, 25.4 * 0.01, "Unit 1/1000 inch, 4.598 x 7.623",
106 InchToMM(4.598), InchToMM(7.623), 4.598, 7.623 },
107 { LitePDFUnit_Unknown, -1, -1, -1.0, NULL, -1.0, -1.0, -1.0, -1.0 }
108 };
109 int ii;
110
111 ThrowIfFail(litePDF.GetUnit() == LitePDFUnit_mm);
112
113 // begin write-only PDF file
114 litePDF.CreateFileDocument("unitvalues-1.pdf");
115
116 for (ii = 0; pages[ii].text; ii++) {
117 createPage(litePDF, pages[ii].unit, pages[ii].width_u, pages[ii].height_u, pages[ii].scale, pages[ii].text);
118 }
119
120 // close the document
121 litePDF.Close();
122
123 litePDF.LoadFromFile("unitvalues-1.pdf", "", true);
124 unsigned int width_u, height_u;
125
126 ThrowIfFail(litePDF.GetPageCount() == (unsigned int) ii);
127
128 for (ii = 0; pages[ii].text; ii++) {
129 litePDF.SetUnit(pages[ii].unit);
130 ThrowIfFail(litePDF.GetUnit() == pages[ii].unit);
131
132 litePDF.GetPageSize(ii, &width_u, &height_u);
133 ThrowIfFail(width_u == pages[ii].width_u);
134 ThrowIfFail(height_u == pages[ii].height_u);
135
136 width_u = litePDF.MMToUnit(pages[ii].width_mm) + 0.1; // add 0.1 to "round up"
137 height_u = litePDF.MMToUnit(pages[ii].height_mm) + 0.1; // add 0.1 to "round up"
138 ThrowIfFail(width_u == pages[ii].width_u);
139 ThrowIfFail(height_u == pages[ii].height_u);
140
141 width_u = litePDF.InchToUnit(pages[ii].width_in) + 0.1; // add 0.1 to "round up"
142 height_u = litePDF.InchToUnit(pages[ii].height_in) + 0.1; // add 0.1 to "round up"
143 ThrowIfFail(width_u == pages[ii].width_u);
144 ThrowIfFail(height_u == pages[ii].height_u);
145
146 ThrowIfFail(DoubleEqual(litePDF.UnitToInchEx(pages[ii].unit, pages[ii].width_u), pages[ii].width_in));
147 ThrowIfFail(DoubleEqual(litePDF.UnitToMMEx(pages[ii].unit, pages[ii].width_u), pages[ii].width_mm));
148 }
149
150 litePDF.Close();
151 } catch (TLitePDFException &ex) {
152 fprintf (stderr, "litePDF Exception: %x: %s\n", ex.getCode(), ex.getMessage());
153 res = 1;
154 }
155
156 return res;
157 }