Examples / edit_page / activex_cpp

Edit Page

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

Demonstrates Core SDK

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

examples/activex_cpp/edit_page/edit_page.cpp 99 lines
// ============================================================================
//  edit_page -- C++ ActiveX (COM) port, following
//  examples/activex/edit_page/edit_page.vbs
//
//  Imports a ROTATED page, opens it for editing and writes a formatted text
//  block onto it. The two things that make editing an imported page awkward
//  are exactly what the text on the page explains:
//
//    * the page's orientation -- SetOrientationEx rotates the coordinate
//      system so the page can be treated as if it were upright
//    * the coordinate origin -- SetUseVisibleCoords(true) moves the zero point
//      into the visible area (crop box if present, else media box), so it does
//      not have to be corrected by hand
//
//  The text uses the engine's list markup: \LI[indent,bullet] \LD[leading]
//  \EL# ends a list. Chr(144) is the cp1252 bullet glyph.
// ============================================================================
#import "..\\..\\wrappers\\activex\\LumasPdfAX.tlb" no_namespace named_guids

#include <windows.h>

#include "axcommon.h"

#include <cstdio>
#include <string>

static const char* IN_FILE = "E:\\LUMASPDFSDK\\examples\\test_files\\rotated_270.pdf";

int main() {
    ChdirToExe();
    ComInit com;
    if (!com.ok()) return 1;
    int rc = 0;
    try {
        ILumasPDFPtr pdf;
        if (FAILED(pdf.CreateInstance(L"LumasPdf.PDF"))) _com_issue_error(E_FAIL);
        pdf->RaiseExceptions = VARIANT_TRUE;

        std::string outFile = OutFile();

        pdf->CreateNewPDFA(B(""));                 // output file opened later
        pdf->SetImportFlags(ifImportAll | ifImportAsPage);

        if (pdf->OpenImportFileA(B(IN_FILE), ptOpen, B("")) < 0) {
            std::printf("Input file not found: %s\n", IN_FILE);
            return 1;
        }
        pdf->ImportPDFFile(1, 1, 1);
        pdf->CloseImportFile();

        pdf->SetPageCoords(pcTopDown);
        pdf->SetUseVisibleCoords(VARIANT_TRUE);

        pdf->EditPage(1);
        long orientation = pdf->GetOrientation();
        if (orientation != 0) pdf->SetOrientationEx(orientation);
        pdf->SetLeading(14);
        long f = pdf->SetFontA(B("Helvetica"), fsRegular, 12, VARIANT_FALSE, cp1252);
        pdf->SetListFont(f);

        const char Bl = (char)144;     // cp1252 bullet
        const char CR = '\r';

        std::string s =
            "It is not difficult to edit an imported page but two things must be considered:";
        s += CR; s += CR;
        s += "\\LI[20,"; s += Bl; s += "]\\LD[16]The page's orientation.\\EL#";
        s += "\\LI[20,"; s += Bl; s += "]\\LD[12]The coordinate origin. The coordinate origin can "
             "be taken from the crop box if present, or from the media box (Left and Bottom).\\EL#";
        s += CR;
        s += "\\LD[12]";
        s += "Although it is possible to correct the coordinate origin manually, it is much easier "
             "to set the property SetUseVisibleCoords() to true. LumasPDF moves the zero point then "
             "automatically into the visible area of the page.";
        s += CR; s += CR;
        s += "The functions GetPageWidth() and GetPageHeight() return then also the logical width "
             "or height of the page depending on the orientation and whether a crop box is present.";
        s += CR; s += CR;
        s += "The handling of rotated pages is a bit more complicated since the orientation is "
             "just a property.";
        s += CR; s += CR;
        s += "SetOrientationEx() rotates the coordinate system so that we can work with the page "
             "as if it was not rotated.";

        pdf->WriteFTextExA(50, 200, pdf->GetPageWidth() - 100, -1, taJustify, B(s));
        pdf->EndPage();

        if (pdf->HaveOpenDoc()) {
            if (!pdf->OpenOutputFileA(B(outFile))) return 1;
            if (pdf->CloseFile()) std::printf("OK: %s\n", outFile.c_str());
        }
    } catch (const _com_error& e) {
        const _bstr_t d = e.Description();
        std::fprintf(stderr, "%ws\n", d.length() ? (const wchar_t*)d : L"COM error");
        rc = 1;
    }
    return rc;
}

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