| 1 |
// SPDX-FileCopyrightText: 2006 Dominik Seichter <[email protected]> |
| 2 |
// SPDX-License-Identifier: MIT-0 |
| 3 |
|
| 4 |
// Include the standard headers for cout to write |
| 5 |
// some output to the console. |
| 6 |
#include <iostream> |
| 7 |
|
| 8 |
// Now include all PoDoFo header files, to have access |
| 9 |
// to all functions of PoDoFo and so that you do not have |
| 10 |
// to care about the order of includes. |
| 11 |
// |
| 12 |
// You should always use podofo.h and not try to include |
| 13 |
// the required headers on your own. |
| 14 |
#include <podofo/podofo.h> |
| 15 |
|
| 16 |
// All PoDoFo classes are member of the PoDoFo namespace. |
| 17 |
using namespace std; |
| 18 |
using namespace PoDoFo; |
| 19 |
|
| 20 |
void PrintHelp() |
| 21 |
{ |
| 22 |
std::cout << "This is a example application for the PoDoFo PDF library." << std::endl |
| 23 |
<< "It creates a small PDF file containing the text >Hello World!<" << std::endl |
| 24 |
<< "Please see https://github.com/podofo/podofo for more information" << std::endl << std::endl; |
| 25 |
std::cout << "Usage:" << std::endl; |
| 26 |
std::cout << " helloworld-base14 [outputfile.pdf]" << std::endl << std::endl; |
| 27 |
} |
| 28 |
|
| 29 |
const char* GetBase14FontName(unsigned i); |
| 30 |
void DemoBase14Fonts(PdfPainter& painter, PdfPage& page, PdfDocument& document); |
| 31 |
|
| 32 |
void HelloWorld(const string_view& filename) |
| 33 |
{ |
| 34 |
PdfMemDocument document; |
| 35 |
|
| 36 |
// PdfPainter is the class which is able to draw text and graphics |
| 37 |
// directly on a PdfPage object. |
| 38 |
PdfPainter painter; |
| 39 |
|
| 40 |
// A PdfFont object is required to draw text on a PdfPage using a PdfPainter. |
| 41 |
// PoDoFo will find the font using fontconfig on your system and embed truetype |
| 42 |
// fonts automatically in the PDF file. |
| 43 |
PdfFont* font; |
| 44 |
|
| 45 |
try |
| 46 |
{ |
| 47 |
// The PdfDocument object can be used to create new PdfPage objects. |
| 48 |
// The PdfPage object is owned by the PdfDocument will also be deleted automatically |
| 49 |
// by the PdfDocument object. |
| 50 |
// |
| 51 |
// You have to pass only one argument, i.e. the page size of the page to create. |
| 52 |
// There are predefined enums for some common page sizes. |
| 53 |
auto& page = document.GetPages().CreatePage(PdfPageSize::A4); |
| 54 |
|
| 55 |
// If the page cannot be created because of an error (e.g. ePdfError_OutOfMemory ) |
| 56 |
// a nullptr pointer is returned. |
| 57 |
// We check for a nullptr pointer here and throw an exception using the RAISE_ERROR macro. |
| 58 |
// The raise error macro initializes a PdfError object with a given error code and |
| 59 |
// the location in the file in which the error occurred and throws it as an exception. |
| 60 |
|
| 61 |
// Set the page as drawing target for the PdfPainter. |
| 62 |
// Before the painter can draw, a page has to be set first. |
| 63 |
painter.SetCanvas(page); |
| 64 |
|
| 65 |
// Create a PdfFont object using the font "Arial". |
| 66 |
// The font is found on the system using fontconfig and embedded into the |
| 67 |
// PDF file. If Arial is not available, a default font will be used. |
| 68 |
// |
| 69 |
// The created PdfFont will be deleted by the PdfDocument. |
| 70 |
font = document.GetFonts().SearchFont("Arial"); |
| 71 |
|
| 72 |
// If the PdfFont object cannot be allocated return an error. |
| 73 |
if (font == nullptr) |
| 74 |
throw runtime_error("Invalid handle"); |
| 75 |
|
| 76 |
auto& metrics = font->GetMetrics(); |
| 77 |
cout << "The font name is " << metrics.GetFontName() << endl; |
| 78 |
cout << "The family font name is " << metrics.GetFontFamilyName() << endl; |
| 79 |
cout << "The font file path is " << metrics.GetFilePath() << endl; |
| 80 |
cout << "The font face index is " << metrics.GetFaceIndex() << endl; |
| 81 |
|
| 82 |
// Set the font as default font for drawing. |
| 83 |
// A font has to be set before you can draw text on |
| 84 |
// a PdfPainter. |
| 85 |
painter.TextState.SetFont(*font, 18.0); |
| 86 |
|
| 87 |
// You could set a different color than black to draw |
| 88 |
// the text. |
| 89 |
// |
| 90 |
// painter.GraphicsState.SetNonStrokingColor(PdfColor(1.0, 0.0, 0.0)); |
| 91 |
|
| 92 |
// Actually draw the line "Hello World!" on to the PdfPage at |
| 93 |
// the position 2cm,2cm from the top left corner. |
| 94 |
// Please remember that PDF files have their origin at the |
| 95 |
// bottom left corner. Therefore we substract the y coordinate |
| 96 |
// from the page height. |
| 97 |
// |
| 98 |
// The position specifies the start of the baseline of the text. |
| 99 |
// |
| 100 |
// All coordinates in PoDoFo are in PDF units. |
| 101 |
painter.DrawText("Hello World!", 56.69, page.GetRect().Height - 56.69); |
| 102 |
|
| 103 |
|
| 104 |
DemoBase14Fonts(painter, page, document); |
| 105 |
|
| 106 |
painter.FinishDrawing(); |
| 107 |
|
| 108 |
// The last step is to close the document. |
| 109 |
document.Save(filename); |
| 110 |
|
| 111 |
} |
| 112 |
catch (PdfError&) |
| 113 |
{ |
| 114 |
// All PoDoFo methods may throw exceptions |
| 115 |
// make sure that painter.FinishPage() is called |
| 116 |
// or who will get an assert in its destructor |
| 117 |
try |
| 118 |
{ |
| 119 |
painter.FinishDrawing(); |
| 120 |
} |
| 121 |
catch (...) |
| 122 |
{ |
| 123 |
// Ignore errors this time |
| 124 |
} |
| 125 |
|
| 126 |
throw; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
int main(int argc, char* argv[]) |
| 131 |
{ |
| 132 |
// Check if a filename was passed as commandline argument. |
| 133 |
// If more than 1 argument or no argument is passed, |
| 134 |
// a help message is displayed and the example application |
| 135 |
// will quit. |
| 136 |
if (argc != 2) |
| 137 |
{ |
| 138 |
PrintHelp(); |
| 139 |
return -1; |
| 140 |
} |
| 141 |
|
| 142 |
// All PoDoFo functions will throw an exception in case of an error. |
| 143 |
// |
| 144 |
// You should catch the exception to either fix it or report |
| 145 |
// back to the user. |
| 146 |
// |
| 147 |
// All exceptions PoDoFo throws are objects of the class PdfError. |
| 148 |
// Thats why we simply catch PdfError objects. |
| 149 |
try |
| 150 |
{ |
| 151 |
// Call the drawing routing which will create a PDF file |
| 152 |
// with the filename of the output file as argument. |
| 153 |
HelloWorld(argv[1]); |
| 154 |
} |
| 155 |
catch (PdfError& err) |
| 156 |
{ |
| 157 |
// We have to check if an error has occurred. |
| 158 |
// If yes, we return and print an error message |
| 159 |
// to the commandline. |
| 160 |
err.PrintErrorMsg(); |
| 161 |
return (int)err.GetCode(); |
| 162 |
} |
| 163 |
|
| 164 |
// The PDF was created successfully. |
| 165 |
std::cout << std::endl |
| 166 |
<< "Created a PDF file containing the line \"Hello World!\": " << argv[1] << std::endl << std::endl; |
| 167 |
|
| 168 |
return 0; |
| 169 |
} |
| 170 |
|
| 171 |
// Base14 + other non-Base14 fonts for comparison |
| 172 |
|
| 173 |
static const char* s_base14fonts[] = |
| 174 |
{ |
| 175 |
"Times-Roman", |
| 176 |
"Times-Italic", |
| 177 |
"Times-Bold", |
| 178 |
"Times-BoldItalic", |
| 179 |
"Helvetica", |
| 180 |
"Helvetica-Oblique", |
| 181 |
"Helvetica-Bold", |
| 182 |
"Helvetica-BoldOblique", |
| 183 |
"Courier", |
| 184 |
"Courier-Oblique", |
| 185 |
"Courier-Bold", |
| 186 |
"Courier-BoldOblique", |
| 187 |
"Symbol", |
| 188 |
"ZapfDingbats", |
| 189 |
"Arial", |
| 190 |
"Verdana" |
| 191 |
}; |
| 192 |
|
| 193 |
const char* GetBase14FontName(unsigned i) |
| 194 |
{ |
| 195 |
if (i >= std::size(s_base14fonts)) |
| 196 |
return nullptr; |
| 197 |
|
| 198 |
return s_base14fonts[i]; |
| 199 |
} |
| 200 |
|
| 201 |
void DrawRedFrame(PdfPainter& painter, double x, double y, double width, double height) |
| 202 |
{ |
| 203 |
// draw red box |
| 204 |
painter.GraphicsState.SetNonStrokingColor(PdfColor(1.0f, 0.0f, 0.0f)); |
| 205 |
painter.GraphicsState.SetStrokingColor(PdfColor(1.0f, 0.0f, 0.0f)); |
| 206 |
painter.DrawLine(x, y, x + width, y); |
| 207 |
if (height > 0.0f) |
| 208 |
{ |
| 209 |
painter.DrawLine(x, y, x, y + height); |
| 210 |
painter.DrawLine(x + width, y, x + width, y + height); |
| 211 |
painter.DrawLine(x, y + height, x + width, y + height); |
| 212 |
} |
| 213 |
// restore to black |
| 214 |
painter.GraphicsState.SetNonStrokingColor(PdfColor(0.0f, 0.0f, 0.0f)); |
| 215 |
painter.GraphicsState.SetStrokingColor(PdfColor(0.0f, 0.0f, 0.0f)); |
| 216 |
} |
| 217 |
|
| 218 |
void DemoBase14Fonts(PdfPainter& painter, PdfPage& page, PdfDocument& document) |
| 219 |
{ |
| 220 |
PdfFontSearchParams params; |
| 221 |
params.AutoSelect = PdfFontAutoSelectBehavior::Standard14; |
| 222 |
|
| 223 |
double x = 56, y = page.GetRect().Height - 56.69; |
| 224 |
string_view demo_text = "abcdefgABCDEFG12345!#$%&+-@? "; |
| 225 |
double height = 0.0f, width = 0.0f; |
| 226 |
|
| 227 |
// draw sample of all types |
| 228 |
for (unsigned i = 0; i < std::size(s_base14fonts); i++) |
| 229 |
{ |
| 230 |
x = 56; y = y - 25; |
| 231 |
string text; |
| 232 |
if (i == 12) |
| 233 |
{ |
| 234 |
// Or u8"♠♣♥♦": Symbol font doesn't support regular characters |
| 235 |
text = u8"\u2660\u2663\u2665\u2666"; |
| 236 |
} |
| 237 |
else if (i == 13) |
| 238 |
{ |
| 239 |
// Or u8"❏❑▲▼": ZapfDingbats font doesn't support regular characters |
| 240 |
text = u8"\u274f\u2751\u25b2\u25bc"; |
| 241 |
} |
| 242 |
else |
| 243 |
{ |
| 244 |
text = demo_text; |
| 245 |
text.append(GetBase14FontName(i)); |
| 246 |
} |
| 247 |
|
| 248 |
PdfFont* font = document.GetFonts().SearchFont(GetBase14FontName(i), params); |
| 249 |
if (font == nullptr) |
| 250 |
throw runtime_error("Font not found"); |
| 251 |
|
| 252 |
painter.TextState.SetFont(*font, 12.0); |
| 253 |
|
| 254 |
width = font->GetStringLength(text, painter.TextState); |
| 255 |
height = font->GetLineSpacing(painter.TextState); |
| 256 |
|
| 257 |
std::cout << GetBase14FontName(i) << " Width = " << width << " Height = " << height << std::endl; |
| 258 |
|
| 259 |
// draw red box |
| 260 |
DrawRedFrame(painter, x, y, width, height); |
| 261 |
|
| 262 |
// draw text |
| 263 |
painter.DrawText(text, x, y); |
| 264 |
} |
| 265 |
|
| 266 |
// draw some individual characters: |
| 267 |
string_view demo_text2 = " @_1jiPlg .;"; |
| 268 |
|
| 269 |
auto helveticaStd14 = document.GetFonts().SearchFont("Helvetica", params); |
| 270 |
auto arialImported = document.GetFonts().SearchFont("Arial"); |
| 271 |
auto& metrics = arialImported->GetMetrics(); |
| 272 |
cout << "Non base 14 font characteristics" << endl; |
| 273 |
cout << "Font name: " << metrics.GetFontName() << endl; |
| 274 |
cout << "Family font name: " << metrics.GetFontFamilyName() << endl; |
| 275 |
cout << "Font file path: " << metrics.GetFilePath() << endl; |
| 276 |
cout << "Font face index: " << metrics.GetFaceIndex() << endl; |
| 277 |
|
| 278 |
// draw individuals |
| 279 |
for (unsigned i = 0; i < demo_text2.length(); i++) |
| 280 |
{ |
| 281 |
x = 56; y = y - 25; |
| 282 |
string text; |
| 283 |
if (i == 0) |
| 284 |
text = "Helvetica / Arial Comparison:"; |
| 285 |
else |
| 286 |
text = (string)demo_text2.substr(i, 1); |
| 287 |
|
| 288 |
|
| 289 |
painter.TextState.SetFont(*helveticaStd14, 12); |
| 290 |
height = helveticaStd14->GetLineSpacing(painter.TextState); |
| 291 |
width = helveticaStd14->GetStringLength(text, painter.TextState); |
| 292 |
|
| 293 |
// draw red box |
| 294 |
DrawRedFrame(painter, x, y, width, height); |
| 295 |
|
| 296 |
// draw text |
| 297 |
painter.DrawText(text, x, y); |
| 298 |
|
| 299 |
if (i > 0) |
| 300 |
{ |
| 301 |
// draw again, with non-Base14 font |
| 302 |
painter.TextState.SetFont(*arialImported, 12); |
| 303 |
height = arialImported->GetLineSpacing(painter.TextState); |
| 304 |
width = arialImported->GetStringLength((string_view)text, painter.TextState); |
| 305 |
|
| 306 |
// draw red box |
| 307 |
DrawRedFrame(painter, x + 100, y, width, height); |
| 308 |
|
| 309 |
// draw text |
| 310 |
painter.DrawText(text, x + 100, y); |
| 311 |
} |
| 312 |
} |
| 313 |
} |