Prise en main
Activez une licence, ouvrez un PDF, comptez ses pages, rendez une page en PNG et extrayez son texte avec NexPDF pour C++.
Cet exemple ouvre un PDF, affiche le nombre de pages, rend la première page dans un fichier PNG et affiche le texte de chaque page. Il fonctionne aussi sans clé de licence : la consultation est toujours autorisée, et les pages rendues portent alors un filigrane.
Installer le SDK
Récupérez nexpdf-cpp et la bibliothèque native, et ajoutez la cible nexdevit::nexpdf à votre build, comme
décrit dans Installation.
Écrire le programme
#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;
}
}Le compiler et l'exécuter
cmake -S . -B build -DNEXPDF_LIB_DIR=/opt/nexpdf-ffi-1.1.0-linux-x64/lib
cmake --build build
./build/app contract.pdfpage1.png est écrit dans le dossier courant.
Ce que fait le code
nexpdf::activate_license(key_or_xml, platform, app_id)accepte le XML de licence ou la clé seule. L'identifiant d'application doit être l'un de ceux pour lesquels la clé a été émise. Voir Licence.Document::open(path, password)ouvre un PDF ;passwordest facultatif. Les fichiers Word, Excel et PowerPoint sont convertis lorsque la licence inclutconversion; les images JPEG, PNG et TIFF deviennent une page chacune.page_width(i)etpage_height(i)donnent la taille affichée en points : boîte de rognage, rotation et/UserUnitappliqués.render_page_png(i, dpi)renvoie les octets PNG. 72 dpi correspond à un pixel par point.extract_page_text(i)renvoie le texte de la page dans l'ordre de lecture.