Opening and saving
Open PDFs from files and bytes, open encrypted files, create PDFs from pictures, HTML, Office and e-book files, and save, linearize and copy documents with NexPDF for C++.
License module
Opening PDFs and pictures, saving and copying are part of the viewer module and work without a
key. Creating a PDF from HTML, Office or e-book files needs the conversion module (Startup tier and
above).
The examples use the read_file and write_file helpers from
Getting started.
Open a file or bytes
Document::open(path, password) reads a file; Document::open_bytes(data, password) takes the
bytes you already have, for example from an upload or object storage. The password is optional.
#include <nexpdf.hpp>
namespace nexpdf = nexdevit::nexpdf;
auto doc = nexpdf::Document::open("contract.pdf");
std::cout << doc.page_count() << " pages\n";
std::vector<uint8_t> data = read_file("contract.pdf");
auto from_bytes = nexpdf::Document::open_bytes(data);Both accept JPEG, PNG and TIFF pictures too (one page per picture). With the conversion module,
Word, Excel and PowerPoint files, EPUB and zipped FictionBook books, XPS files and CBZ comics are
detected and converted on open.
A Document owns native memory and frees it in its destructor. reset() frees it at once; after
that, or after the document was moved from, any method throws std::logic_error.
Encrypted documents
Pass the user (open) password or the owner password. RC4, AES-128 and AES-256 files are supported. Word, Excel and PowerPoint files, e-books and comics are still detected and converted when you pass a password; the password is used only for PDFs.
try {
auto doc = nexpdf::Document::open("locked.pdf", "s3cret");
// ...
} catch (const nexpdf::Error& e) {
std::string msg = e.what();
if (msg == "password required" || msg == "incorrect password") {
ask_the_user_again();
} else {
throw;
}
}nexpdf::open_bytes_with_password(data, password) always opens the bytes as a PDF.
Files that are only restricted by permissions (no open password) open without a password.
doc.security_json() reports the encryption and permissions; see
Security.
Create a PDF from other formats
| Source | Function | Module |
|---|---|---|
| JPEG, PNG, TIFF | nexpdf::open_image(data) | none |
| HTML | nexpdf::from_html(html) | conversion |
| Word, Excel, PowerPoint | nexpdf::open_office(data) | conversion |
| EPUB, FictionBook, XPS/OpenXPS, CBZ | nexpdf::open_ebook(data) | conversion |
auto scan = nexpdf::open_image(read_file("scan.tiff")); // one page per TIFF frame
scan.save("scan.pdf");
auto invoice = nexpdf::from_html("<h1>Invoice 42</h1><p>Total: 120 EUR</p>");
invoice.save("invoice.pdf");To add pictures to an existing document, use insert_image_pages(index, data); see
Editing.
Save
save(path) writes the document with every change as a complete new file. save_bytes() returns
the same bytes as a std::vector<uint8_t> instead of writing a file.
doc.save("out.pdf");
std::vector<uint8_t> pdf = doc.save_bytes(); // e.g. to send in an HTTP responseFast Web View
A linearized file shows its first page before the download ends, which helps when PDFs are served over HTTP:
doc.save("web.pdf", true); // to a file
std::vector<uint8_t> web = doc.save_linearized(); // or as bytesSigned files
Signing returns the finished signed file. Write those bytes as they are: saving the document object again rewrites the file and breaks the signature. See Signatures.
Copy a document
copy() returns an independent document with the same content and changes. Use it as an undo
snapshot, or to hand work to another thread:
auto snapshot = doc.copy();
doc.delete_page(0);
// ... undo:
doc = std::move(snapshot); // frees the edited documentIn C
nexpdf_open_bytes(data, len) opens a PDF or picture, nexpdf_open_bytes_with_password(data, len, password) an encrypted PDF, and nexpdf_open_office, nexpdf_open_ebook and nexpdf_open_image
convert other formats. Each returns NULL on failure. nexpdf_save_bytes(doc) and
nexpdf_save_linearized(doc) return a NexPdfBytes you free with nexpdf_free_bytes; free the
document with nexpdf_free. The C++ open and open_bytes pick the right converter for ZIP-based
files themselves; in C you call the converter for the format.
Related
- Document structure: metadata, bookmarks and attachments
- Conversion: PDF to Word, Excel, HTML and more
- Compare and optimise: smaller files and memory