| 1 | // |
|---|---|
| 2 | // Image.cpp |
| 3 | // |
| 4 | // This sample demonstrates the generation and saving of a PDF |
| 5 | // document that contains an mbedded image loaded from external file. |
| 6 | // |
| 7 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
| 8 | // and Contributors. |
| 9 | // |
| 10 | // SPDX-License-Identifier: BSL-1.0 |
| 11 | // |
| 12 | |
| 13 | #include "Poco/PDF/Document.h" |
| 14 | #include "Poco/Path.h" |
| 15 | #include "Poco/File.h" |
| 16 | |
| 17 | |
| 18 | #if defined(POCO_OS_FAMILY_UNIX) |
| 19 | const std::string pdfFileName = "${POCO_BASE}/PDF/samples/Image/Image.pdf"; |
| 20 | const std::string pngFileName = "${POCO_BASE}/PDF/samples/Image/logo.png"; |
| 21 | #elif defined(POCO_OS_FAMILY_WINDOWS) |
| 22 | const std::string pdfFileName = "%POCO_BASE%/PDF/samples/Image/Image.pdf"; |
| 23 | const std::string pngFileName = "%POCO_BASE%/PDF/samples/Image/logo.PNG"; |
| 24 | #endif |
| 25 | |
| 26 | |
| 27 | using Poco::PDF::Document; |
| 28 | using Poco::PDF::Font; |
| 29 | using Poco::PDF::Page; |
| 30 | using Poco::PDF::Image; |
| 31 | using Poco::Path; |
| 32 | using Poco::File; |
| 33 | |
| 34 | |
| 35 | int main(int argc, char** argv) |
| 36 | { |
| 37 | File pdfFile(Path::expand(pdfFileName)); |
| 38 | if (pdfFile.exists()) pdfFile.remove(); |
| 39 | File pngFile(Path::expand(pngFileName)); |
| 40 | |
| 41 | Document document(pdfFile.path()); |
| 42 | |
| 43 | Page page = document[0]; |
| 44 | Font helv = document.font("Helvetica"); |
| 45 | page.setFont(helv, 24); |
| 46 | |
| 47 | std::string hello = "Hello PDF World from "; |
| 48 | float textWidth = page.textWidth(hello); |
| 49 | float textBegin = (page.getWidth() - textWidth) / 2; |
| 50 | page.writeOnce(textBegin, page.getHeight() - 50, hello); |
| 51 | |
| 52 | Image image = document.loadPNGImage(pngFile.path()); |
| 53 | page.drawImage(image, textBegin + textWidth / 2 - image.width() / 2, |
| 54 | page.getHeight() - 100 - image.height(), |
| 55 | image.width(), |
| 56 | image.height()); |
| 57 | |
| 58 | document.save(); |
| 59 | return 0; |
| 60 | } |
| 61 |