Some test text!

Discord Logo

Chat with us

PDFTron is now Apryse, learn more here.

Python / Guides / Add layer



PDFTron is now Apryse, learn more here.

Add a new PDF Layer in Python

To add a PDF layer or OCG (optional content group) to a new document.

doc = PDFDoc()
builder = ElementBuilder()    # ElementBuilder is used to build new Element objects
writer = ElementWriter()      # ElementWriter is used to write Elements to the page

# Create a layer
image_layer = Group.Create(doc, "Image Layer")
cfg = Config.Create(doc, True)

# Add the new OCG to the list of layers that should appear in PDF viewer GUI.
layer_order_array = doc.CreateIndirectArray()

# Start a new page
page = doc.PageCreate()
writer.Begin(page)            # Begin writting to the page

# Add new content to the page and associate it with one of the layers.
img = Image.Create(doc.GetSDFDoc(), image_name)
img.GetSDFObj().Put("OC", image_layer)
writer.WritePlacedElement(builder.CreateImage(img, 300, 600, 200, -150))
writer.End()    # save changes to the current page

PDF layers (OCG)
Full sample code which demonstrates how to create, extract, render PDF layers.

Get the answers you need: Support