Primeiros passos
Ative uma licença, abra um PDF, conte as páginas, renderize uma página em PNG e extraia o texto com o NexPDF para C++.
Este exemplo abre um PDF, exibe o número de páginas, renderiza a primeira página em um arquivo PNG e exibe o texto de cada página. Ele roda também sem chave de licença: a visualização é sempre permitida, e as páginas renderizadas saem então com uma marca d'água.
Instale o SDK
Obtenha o nexpdf-cpp e a biblioteca nativa, e adicione o alvo nexdevit::nexpdf ao seu build, como
descrito em Instalação.
Escreva o programa
#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;
}
}Compile e execute
cmake -S . -B build -DNEXPDF_LIB_DIR=/opt/nexpdf-ffi-1.1.0-linux-x64/lib
cmake --build build
./build/app contract.pdfpage1.png é gravado na pasta atual.
O que o código faz
nexpdf::activate_license(key_or_xml, platform, app_id)aceita o XML da licença ou a chave pura. O identificador do app deve ser um para o qual a chave foi emitida. Veja Licença.Document::open(path, password)abre um PDF;passwordé opcional. Arquivos Word, Excel e PowerPoint são convertidos quando a licença incluiconversion; imagens JPEG, PNG e TIFF viram uma página cada.page_width(i)epage_height(i)são o tamanho exibido em pontos: com a caixa de recorte, a rotação e/UserUnitaplicados.render_page_png(i, dpi)devolve bytes PNG. 72 dpi é um pixel por ponto.extract_page_text(i)devolve o texto da página na ordem de leitura.