Getting started
Activate a license, open a PDF, count its pages, render a page to PNG and extract its text with NexPDF for Python.
This example opens a PDF, prints the page count, renders the first page to a PNG file and prints the text of every page. It runs without a license key too: viewing is always allowed, and rendered pages then carry a watermark.
Install the SDK
pip install nexdevit-nexpdfMake the native library available as described in Installation.
Write the script
import os
import sys
import nexpdf
# 1. Activate the license once per process (optional for viewing).
license_file = os.environ.get("NEXPDF_LICENSE")
if license_file:
with open(license_file, encoding="utf-8") as f:
nexpdf.activate_license(f.read(), "server", "com.acme.quickstart")
# 2. Open the document.
path = sys.argv[1] if len(sys.argv) > 1 else "sample.pdf"
with nexpdf.Document.open(path) as doc:
# 3. Page count and size.
count = doc.page_count()
width, height = doc.page_size(0)
print(f"{path}: {count} pages, first page {width:.0f} x {height:.0f} pt")
# 4. Render the first page at 144 dpi.
with open("page1.png", "wb") as f:
f.write(doc.render_page_png(0, 144))
# 5. Extract the text of every page.
for i in range(count):
print(f"--- page {i + 1} ---")
print(doc.extract_page_text(i))What the code does
nexpdf.activate_license(key_or_xml, platform, app_id)accepts the license XML or the bare key. The app id must be one the key was issued for. See License.Document.open(path, password=None)opens a PDF. Word, Excel and PowerPoint files are converted when the license includesconversion; JPEG, PNG and TIFF pictures become one page each.page_size(i)is the displayed size in points: crop box, rotation and/UserUnitapplied.render_page_png(i, dpi)returns PNG bytes. 72 dpi is one pixel per point.extract_page_text(i)returns the page text in reading order.