Examples / split_pdf / activex_cpp

Split Pdf

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

Demonstrates Import & Output APIs

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

examples/activex_cpp/split_pdf/split_pdf.cpp 95 lines
// ============================================================================
//  split_pdf -- C++ ActiveX (COM) port, GENERATED by
//  tools/gen_activex_cpp_examples.py from examples/cpp/split_pdf/split_pdf.cpp
//
//  DO NOT EDIT BY HAND unless you also fix the generator: the next run
//  overwrites this file. If a case cannot be expressed mechanically, teach the
//  generator the rule -- that is what keeps the other 87 honest.
//
//  This drives the SAME engine as examples/cpp/split_pdf/split_pdf.cpp, but through the COM
//  object wrappers/activex exposes rather than the flat C API, so it exercises
//  the marshalling layer no compiled language exercised before.
// ============================================================================
#import "..\\..\\..\\wrappers\\activex\\LumasPdfAX.tlb" no_namespace named_guids

#include <windows.h>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>

// Output goes beside the EXECUTABLE, at the same relative path delphi's lands
// at, because tools/compare_lang_outputs.py keys on exactly that.
static std::string exeDir() {
    char buf[MAX_PATH];
    GetModuleFileNameA(nullptr, buf, MAX_PATH);
    std::string s(buf);
    size_t p = s.find_last_of("\\/");
    return p == std::string::npos ? std::string(".") : s.substr(0, p);
}

// split_pdf -- C++ port of examples\Vb6\split_pdf\split_pdf.bas
// Opens one import file, keeps it open across CloseFile() via
// SetUseGlobalImpFiles, and writes each page into its own PDF.
#include "apputil.h"
#include <cstdio>



int main(int argc, char** argv) {
    (void)argc; (void)argv;
    if (FAILED(CoInitialize(nullptr))) return 1;
    int _rc = 0;
    try {
    ChdirToExe();
    ILumasPDFPtr pdf;
    if (FAILED(pdf.CreateInstance(L"LumasPdf.PDF")))
        _com_issue_error(E_FAIL);
    pdf->RaiseExceptions = VARIANT_TRUE;

    pdf->SetImportFlags(ifImportAll | ifImportAsPage);  // avoid conversion of pages to templates
    pdf->SetImportFlags2(if2UseProxy);                  // reduces the memory usage

    const char* inFile = "license.pdf";
    if(pdf->OpenImportFileA(inFile, ptOpen, "") < 0){

        return 0;
    }

    // Keeps the open import file from being closed when CloseFile() is called.
    pdf->SetUseGlobalImpFiles(1);

    const char* outDir = "out";
    CreateDirectoryA(outDir, 0);

    long count = pdf->GetInPageCount();
    for(long i = 1; i <= count; i++){
        char outPath[512];
        sprintf(outPath, "%s\\page%04d.pdf", outDir, (int)i);
        pdf->CreateNewPDFA(outPath);
        pdf->Append();
        pdf->ImportPageEx(i, 1.0, 1.0);
        pdf->EndPage();
        pdf->CloseFile();
    }

    // Always set the property back to false when finished.
    pdf->SetUseGlobalImpFiles(0);

    printf("Pages written to: %s\n", outDir);

    return 0;
    } catch (const _com_error& e) {
        const _bstr_t d = e.Description();
        if (d.length())
            std::fprintf(stderr, "COM error 0x%08lx: %ws\n",
                         (unsigned long)e.Error(), (const wchar_t*)d);
        else
            std::fprintf(stderr, "COM error 0x%08lx: %s\n",
                         (unsigned long)e.Error(), e.ErrorMessage());
        _rc = 1;
    }
    CoUninitialize();
    return _rc;
}

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