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)
19const std::string pdfFileName = "${POCO_BASE}/PDF/samples/Image/Image.pdf";
20const std::string pngFileName = "${POCO_BASE}/PDF/samples/Image/logo.png";
21#elif defined(POCO_OS_FAMILY_WINDOWS)
22const std::string pdfFileName = "%POCO_BASE%/PDF/samples/Image/Image.pdf";
23const std::string pngFileName = "%POCO_BASE%/PDF/samples/Image/logo.PNG";
24#endif
25
26
27using Poco::PDF::Document;
28using Poco::PDF::Font;
29using Poco::PDF::Page;
30using Poco::PDF::Image;
31using Poco::Path;
32using Poco::File;
33
34
35int 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