Some test text!

Discord Logo

Chat with us

PDFTron is now Apryse, learn more here.

Python / Guides / Extract layer



PDFTron is now Apryse, learn more here.

Extract layers from PDF in Python

To extract PDF layers or OCG (optional content group) from a document.

doc = PDFDoc(filename)
page = doc.GetPage(1)

init_cfg = doc.GetOCGConfig()
ctx = Context(init_cfg)

pdfdraw = PDFDraw()
pdfdraw.SetImageSize(1000, 1000)
pdfdraw.SetOCGContext(ctx)  # Render the page using the given OCG context.

# Disable drawing of content that is not optional (i.e. is not part of any layer).

# Now render each layer in the input document to a separate image.
ocgs = doc.GetOCGs()    # Get the array of all OCGs in the document.
sz = ocgs.Size()
i = 0
while i < sz:
    ocg = Group(ocgs.GetAt(i))
    ctx.SetState(ocg, True)
    fname = "pdf_layers_" + ocg.GetName() + ".png"
    pdfdraw.Export(page, output_path + fname)
    i = i + 1

# Now draw content that is not part of any layer...
pdfdraw.Export(page, output_path + "pdf_layers_non_oc.png")

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

Get the answers you need: Support