Ir al contenido
NexPDF

Primeros pasos

Activa una licencia, abre un PDF, cuenta sus páginas, renderiza una página a PNG y extrae su texto con NexPDF para C++.

Español

Este ejemplo abre un PDF, imprime el número de páginas, renderiza la primera página en un archivo PNG e imprime el texto de cada página. También funciona sin clave de licencia: ver siempre está permitido, y las páginas renderizadas llevan entonces una marca de agua.

Instala el SDK

Obtén nexpdf-cpp y la biblioteca nativa, y añade el destino nexdevit::nexpdf a tu compilación, como se describe en Instalación.

Escribe el programa

main.cpp
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

#include <nexpdf.hpp>

namespace nexpdf = nexdevit::nexpdf;

// Small file helpers used throughout the guides.
std::vector<uint8_t> read_file(const std::string& path) {
    std::ifstream f(path, std::ios::binary);
    return {std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>()};
}

std::string read_text(const std::string& path) {
    std::ifstream f(path);
    return {std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>()};
}

void write_file(const std::string& path, const std::vector<uint8_t>& data) {
    std::ofstream f(path, std::ios::binary);
    f.write(reinterpret_cast<const char*>(data.data()), static_cast<std::streamsize>(data.size()));
}

int main(int argc, char** argv) {
    try {
        // 1. Activate the license once per process (optional for viewing).
        if (const char* license_file = std::getenv("NEXPDF_LICENSE")) {
            nexpdf::activate_license(read_text(license_file), "server", "com.acme.quickstart");
        }

        // 2. Open the document.
        std::string path = argc > 1 ? argv[1] : "sample.pdf";
        auto doc = nexpdf::Document::open(path);

        // 3. Page count and size.
        int32_t count = doc.page_count();
        std::printf("%s: %d pages, first page %.0f x %.0f pt\n",
                    path.c_str(), count, doc.page_width(0), doc.page_height(0));

        // 4. Render the first page at 144 dpi.
        write_file("page1.png", doc.render_page_png(0, 144));

        // 5. Extract the text of every page.
        for (int32_t i = 0; i < count; ++i) {
            std::cout << "--- page " << i + 1 << " ---\n" << doc.extract_page_text(i) << "\n";
        }
    } catch (const nexpdf::Error& e) {
        std::cerr << e.what() << "\n";
        return 1;
    }
}

Compílalo y ejecútalo

cmake -S . -B build -DNEXPDF_LIB_DIR=/opt/nexpdf-ffi-1.1.0-linux-x64/lib
cmake --build build
./build/app contract.pdf

page1.png se escribe en la carpeta actual.

Qué hace el código

  • nexpdf::activate_license(key_or_xml, platform, app_id) acepta el XML de la licencia o la clave sola. El identificador de la app debe ser uno para el que se emitió la clave. Consulta Licencia.
  • Document::open(path, password) abre un PDF; password es opcional. Los archivos de Word, Excel y PowerPoint se convierten cuando la licencia incluye conversion; las imágenes JPEG, PNG y TIFF se convierten en una página cada una.
  • page_width(i) y page_height(i) son el tamaño mostrado en puntos: con el cuadro de recorte, la rotación y /UserUnit aplicados.
  • render_page_png(i, dpi) devuelve bytes PNG. A 72 dpi hay un píxel por punto.
  • extract_page_text(i) devuelve el texto de la página en orden de lectura.

Siguientes pasos

En esta página