Skip to Content
NexPDF

Document structure

Read and set metadata, bookmarks and page labels, show and hide layers, and embed or extract file attachments with NexPDF for Python.

English

License module

Reading metadata, bookmarks, page labels, layers and attachments, and switching layers on or off, work without a key. Changing metadata, bookmarks, page labels and attachments needs the editor module (Startup tier and above).

Metadata

metadata_json() returns the document information dictionary (title, author, subject, keywords and the other entries the file has). set_metadata(title, author, subject, keywords) changes it; None keeps the current value.

import nexpdf

with nexpdf.Document.open("report.pdf") as doc:
    info = nexpdf.loads(doc.metadata_json())
    print(info.get("title"), info.get("author"))

    doc.set_metadata(title="Q3 report", author="Finance")
    doc.save("report-titled.pdf")

Bookmarks

Bookmarks (the outline) are a JSON tree of title, 0-based page and optional children. set_outlines_json replaces the whole tree.

outline = nexpdf.loads(doc.outlines_json())

def walk(items, depth=0):
    for item in items:
        print("  " * depth + f'{item["title"]} (page {item["page"] + 1})')
        walk(item.get("children", []), depth + 1)

walk(outline)

doc.set_outlines_json(
    '[{"title":"Summary","page":0},'
    '{"title":"Details","page":2,"children":[{"title":"Costs","page":3}]}]'
)

Page labels

Page labels are the page numbers a viewer shows, such as i, ii for a preface and 1, 2 after it. page_labels_json() returns the label of every page and the ranges that produce them.

labels = nexpdf.loads(doc.page_labels_json())
print(labels["labels"][:4])            # e.g. ["i", "ii", "1", "2"]

doc.set_page_labels_json('[{"start":0,"style":"roman"},{"start":2,"style":"decimal","first":1}]')
doc.set_page_labels_json("[]")         # removes the labels

Styles are decimal, roman, ROMAN, letters, LETTERS and none. A range can also have a prefix, for example "A-".

Layers

Layers (optional content groups) are parts of a page that can be shown or hidden, common in CAD drawings and maps. Each layer is identified by its object number and generation.

for layer in nexpdf.loads(doc.layers_json()):
    print(layer["name"], "visible" if layer["visible"] else "hidden")

notes = next(l for l in nexpdf.loads(doc.layers_json()) if l["name"] == "Notes")
doc.set_layer_visible(notes["num"], notes["gen"], 0)    # hide; 1 shows it again
png = doc.render_page_png(0, 144)                       # rendered without the layer

Attachments

PDFs can carry files: at document level (an attachment list) or on a page (a paperclip annotation). attachments_json() lists both; page is None for document-level files.

for a in nexpdf.loads(doc.attachments_json()):
    print(a["name"], a["mime"], a["size"], a["page"])

with open("invoice.xml", "wb") as f:
    f.write(doc.attachment_data("invoice.xml"))

with open("data.csv", "rb") as f:
    doc.add_attachment("data.csv", f.read(), "text/csv", "Source data")
doc.remove_attachment("old.csv")
doc.save("with-data.pdf")

To attach a file to a page with a visible icon, use add_file_attachment_annot; see Annotations.

On this page