Examples / hello_world / cpp

Hello World

A complete, runnable cpp program — 92 lines, shipped in your download.

Demonstrates Getting Started

Same example, other languages: activex_cpp c cpp_linux csharp delphi php python vbnet

examples/cpp/hello_world/hello_world.cpp 92 lines
// hello_world -- C++ port of examples\Vb6\hello_world\hello_world.bas
#include <lumaspdf.h>
#include <cstdio>
#include <string>
#include <ctime>
static std::string exeDir(const char* a0){ std::string s(a0); auto p=s.find_last_of("\\/"); return p==std::string::npos?".":s.substr(0,p); }

// The Delphi original draws DateTimeToStr(Date + Time), which renders with the
// SYSTEM short-date + long-time patterns -- "02-08-2026 5:55:02 PM" here. A
// plain strftime layout is not an equivalent: dropping the AM/PM designator
// changes the number of whitespace-separated tokens on the page, and that word
// count is exactly what tools/compare_lang_outputs.py diffs, so the row read
// CONTENT_DIFF for a reason that had nothing to do with the engine. Read the
// same two locale values Delphi reads (LOCALE_SSHORTDATE / LOCALE_STIMEFORMAT,
// which is what GetDateFormat(DATE_SHORTDATE) / GetTimeFormat(0) expand).
//
// Declared by hand rather than via <windows.h>: lumaspdf.h rolls its own Win32
// types, and including the real SDK headers after it breaks wincrypt.h with
// ~100 syntax errors. Same reason print_pdf.cpp hand-declares its print dialog.
#ifdef _WIN32
namespace w32 {
    struct SYSTIME { unsigned short wYear, wMonth, wDayOfWeek, wDay,
                                    wHour, wMinute, wSecond, wMilliseconds; };
    extern "C" __declspec(dllimport) int __stdcall
        GetDateFormatA(unsigned long, unsigned long, const SYSTIME*, const char*, char*, int);
    extern "C" __declspec(dllimport) int __stdcall
        GetTimeFormatA(unsigned long, unsigned long, const SYSTIME*, const char*, char*, int);
    const unsigned long USER_DEFAULT = 0x0400;   // LOCALE_USER_DEFAULT
    const unsigned long SHORTDATE    = 0x0001;   // DATE_SHORTDATE
}
#endif

static std::string nowLikeDelphi(){
    time_t tt = time(nullptr);
    struct tm* lt = localtime(&tt);
#ifdef _WIN32
    w32::SYSTIME st = { (unsigned short)(lt->tm_year + 1900), (unsigned short)(lt->tm_mon + 1),
                        (unsigned short)lt->tm_wday, (unsigned short)lt->tm_mday,
                        (unsigned short)lt->tm_hour, (unsigned short)lt->tm_min,
                        (unsigned short)lt->tm_sec, 0 };
    char d[128] = {0}, t[128] = {0};
    w32::GetDateFormatA(w32::USER_DEFAULT, w32::SHORTDATE, &st, nullptr, d, (int)sizeof(d));
    w32::GetTimeFormatA(w32::USER_DEFAULT, 0,              &st, nullptr, t, (int)sizeof(t));
    return std::string(d) + " " + t;
#else
    char buf[64]; strftime(buf, sizeof(buf), "%d-%m-%Y %I:%M:%S %p", lt);
    std::string s(buf);                       // Delphi's 'h' does not pad the
    size_t h = s.find(' ') + 1;               // hour; strftime's %I does.
    if (h && h < s.size() && s[h] == '0') s.erase(h, 1);
    return s;
#endif
}

static SI32 PDF_CALL ErrProc(void* Data, SI32 ErrCode, const char* ErrMessage, SI32 ErrType){
    if(ErrMessage) printf("%s\n", ErrMessage);
    return -1;   // we break processing if an error occurs
}

int main(int argc, char** argv){
    PPDF pdf = pdfNewPDF();
    pdfSetOnErrorProc(pdf, nullptr, ErrProc);
    if(pdfCreateNewPDFA(pdf, "") == 0){
        pdfDeletePDF(pdf);
        return 0;
    }
    pdfSetDocInfoA(pdf, diCreator, "Delphi Example project");
    pdfSetDocInfoA(pdf, diTitle, "My first PDF output");

    pdfAppend(pdf);
    pdfSetFontA(pdf, "Arial", fsItalic, 30.0, 1, cp1252);

    std::string txt = std::string("My first PDF output...\r\r") + nowLikeDelphi();
    pdfWriteFTextA(pdf, taCenter, txt.c_str());
    pdfEndPage(pdf);

    std::string outFile;
    if(pdfHaveOpenDoc(pdf) != 0){
        pdfSetOnErrorProc(pdf, nullptr, nullptr);
        outFile = exeDir(argv[0]) + "/out.pdf";
        if(pdfOpenOutputFileA(pdf, outFile.c_str()) == 0){
            pdfDeletePDF(pdf);
            return 0;
        }
        pdfSetOnErrorProc(pdf, nullptr, ErrProc);
    }
    if(pdfCloseFile(pdf) != 0)
        printf("OK: %s\n", outFile.c_str());

    pdfDeletePDF(pdf);
    return 0;
}

This file is in the SDK at examples/cpp/hello_world/hello_world.cpp. The build fails if this page and that file ever differ.