Examples / pdfviewer / activex_cpp

Pdfviewer

A complete, runnable activex_cpp program — 61 lines, shipped in your download.

Demonstrates Rendering & Viewer

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

examples/activex_cpp/pdfviewer/pdfviewer.cpp 61 lines
// ============================================================================
//  pdfviewer -- C++ ActiveX (COM) port, following
//  examples/activex/pdfviewer/pdfviewer.vbs
//
//  Opens a PDF in the SDK's EMBEDDED viewer window via VwrShowFile.
//
//  The VB6 mirror calls the flat vwrShowFileW with raw PWideChar pointers; the
//  COM object exposes the same entry point as a method taking BSTRs, so no
//  pointer plumbing is needed.
//
//  THE CALL BLOCKS until the user closes the window. That makes this example
//  unsuitable for an unattended sweep, so it only opens the viewer when run
//  interactively: with --no-show (what the batch build passes) it verifies the
//  input exists and exits. Same limitation the VBScript sibling documents --
//  the window cannot be closed programmatically from the example.
// ============================================================================
#import "..\\..\\wrappers\\activex\\LumasPdfAX.tlb" no_namespace named_guids

#include <windows.h>

#include "axcommon.h"

#include <cstdio>
#include <cstring>
#include <string>

int main(int argc, char** argv) {
    ChdirToExe();
    ComInit com;
    if (!com.ok()) return 1;

    std::string pdfPath = OutFile("18_invoice.pdf");
    _bstr_t title = B("18_invoice.pdf - LumasPDF embedded preview");

    FILE* f = fopen(pdfPath.c_str(), "rb");
    if (!f) {
        std::printf("Not found: %s\n", pdfPath.c_str());
        return 1;
    }
    fclose(f);

    bool show = true;
    for (int i = 1; i < argc; ++i)
        if (std::strcmp(argv[i], "--no-show") == 0) show = false;

    if (!show) {
        std::printf("Viewer input present: %s (not shown -- --no-show)\n", pdfPath.c_str());
        return 0;
    }

    ILumasPDFPtr pdf;
    if (FAILED(pdf.CreateInstance(L"LumasPdf.PDF"))) return 1;

    // Blocks until the user closes the viewer window.
    if (pdf->VwrShowFileW(B(pdfPath), title))
        std::printf("Viewer closed OK.\n");
    else
        std::printf("The embedded viewer could not be shown for: %s\n", pdfPath.c_str());
    return 0;
}

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