1 | // Text.cpp |
2 | // |
3 | // This sample demonstrates the generation and saving of a text PDF |
4 | // document, using external TTF font and UTF-8 encoding. |
5 | // |
6 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
7 | // and Contributors. |
8 | // |
9 | // SPDX-License-Identifier: BSL-1.0 |
10 | // |
11 | |
12 | #include "Poco/PDF/Document.h" |
13 | #include "Poco/Path.h" |
14 | #include "Poco/File.h" |
15 | |
16 | |
17 | #if defined(POCO_OS_FAMILY_UNIX) |
18 | const std::string fileName = "${POCO_BASE}/PDF/samples/Text/Text.pdf" ; |
19 | #elif defined(POCO_OS_FAMILY_WINDOWS) |
20 | const std::string fileName = "%POCO_BASE%/PDF/samples/Text/Text.pdf" ; |
21 | #endif |
22 | |
23 | |
24 | using Poco::PDF::Document; |
25 | using Poco::PDF::Font; |
26 | using Poco::PDF::Page; |
27 | using Poco::Path; |
28 | using Poco::File; |
29 | |
30 | |
31 | int main(int argc, char** argv) |
32 | { |
33 | File file(Path::expand(fileName)); |
34 | if (file.exists()) file.remove(); |
35 | |
36 | Document document(file.path()); |
37 | |
38 | Font font = document.font(document.loadTTFont("DejaVuLGCSans.ttf" , true), "UTF-8" ); |
39 | Page page = document[0]; |
40 | page.setFont(font, 24); |
41 | std::string hello = "Hello PDF World from C++ Portable Components" ; |
42 | float tw = page.textWidth(hello); |
43 | page.writeOnce((page.getWidth() - tw) / 2, page.getHeight() - 50, hello); |
44 | page.setFont(font, 14); |
45 | hello = "~ Courtesy of G\xC3\xBCnter Obiltschnig & Aleksandar Fabijani\xC4\x87 ~" ; |
46 | tw = page.textWidth(hello); |
47 | page.writeOnce((page.getWidth() - tw) / 2, page.getHeight() - 100, hello); |
48 | document.save(); |
49 | return 0; |
50 | } |
51 | |