Skip to content
Merged
Show file tree
Hide file tree
Changes from 76 commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
5ff4c7c
feat: add documentation for importing, exporting, and managing AcroFo…
AnHolub Aug 14, 2025
78ae312
feat: add FDF import functionality to PDF form data handling
AnHolub Aug 26, 2025
078e3b3
fix: update installation guide for Aspose.PDF for Rust via C++
AnHolub Aug 27, 2025
e255262
Merge pull request #490 from aspose-pdf/fixrust
AnHolub Aug 28, 2025
aaff771
Merge remote-tracking branch 'origin/develop' into acroforms
AnHolub Aug 28, 2025
d662d61
fix: moved article Working with Headings in PDF into correct location
andruhovski Sep 1, 2025
6b44868
Merge branch 'develop' of https://github.com/aspose-pdf/Aspose.PDF-Do…
andruhovski Sep 1, 2025
5144623
Merge branch 'master' into develop
andruhovski Sep 1, 2025
39b8cce
Refactor AcroForm examples for Python
AnHolub Sep 2, 2025
f1f3269
Update en/python-net/advanced-operations/working-with-forms/acroforms…
AnHolub Sep 2, 2025
9ef3a5c
Update en/python-net/advanced-operations/working-with-forms/acroforms…
AnHolub Sep 2, 2025
9d001d7
Merge pull request #491 from aspose-pdf/acroforms
AnHolub Sep 2, 2025
bf7185b
Update en/python-net/advanced-operations/working-with-forms/acroforms…
andruhovski Sep 3, 2025
f8f998a
Update en/python-net/advanced-operations/working-with-forms/acroforms…
andruhovski Sep 3, 2025
9100630
fix: fixed typos
andruhovski Sep 3, 2025
ace9735
fix: Rename AcroForms JSON export doc and update sitemap
andruhovski Sep 3, 2025
1089acf
Fix formatting and capitalization in AcroForm documentation
andruhovski Sep 3, 2025
9cb7f3b
Update en/python-net/advanced-operations/working-with-forms/acroforms…
andruhovski Sep 3, 2025
a39ad14
Update en/python-net/advanced-operations/working-with-forms/acroforms…
andruhovski Sep 3, 2025
a1017a1
Update en/python-net/advanced-operations/working-with-forms/acroforms…
andruhovski Sep 3, 2025
caada0a
refactor: Refactor PDF form file path handling in docs
andruhovski Sep 3, 2025
4ac073a
refactor: Update file path handling in AcroForm examples for consistency
andruhovski Sep 4, 2025
05ff256
Fix formatting and typos in AcroForms docs
andruhovski Sep 4, 2025
e9b35d2
Merge branch 'master' into develop
AnHolub Sep 5, 2025
8440c4a
feat: Add FAQ section for Aspose.PDF for Python via .NET with common …
AnHolub Sep 5, 2025
efa7cea
Merge pull request #494 from aspose-pdf/faq_page
AnHolub Sep 5, 2025
be982c5
chore: Update documentation for clarity and consistency
AnHolub Sep 5, 2025
b64b348
chore: Update FAQ metadata for Aspose.PDF for Python via .NET
AnHolub Sep 5, 2025
4bb24b6
Merge branch 'develop' of https://github.com/aspose-pdf/Aspose.PDF-Do…
AnHolub Sep 5, 2025
758ca05
Merge branch 'whats_new' into develop
AnHolub Sep 5, 2025
1aa67bf
chore: Update content for adding headers and footers in PDF documenta…
AnHolub Sep 5, 2025
2e5b5ab
chore: Add documentation for working with PDF layers in Python
AnHolub Sep 8, 2025
7853fc7
Merge pull request #495 from aspose-pdf/layers
AnHolub Sep 8, 2025
d602f89
Update documentation for converting files to PDF in Python
AnHolub Sep 12, 2025
4d1da0f
chore: Add code examples for converting CDR and JPEG images to PDF in…
AnHolub Sep 12, 2025
7558102
Add code examples for converting
AnHolub Sep 14, 2025
6831157
Update en/python-net/advanced-operations/working-with-documents/_inde…
AnHolub Sep 17, 2025
b1dae30
Update en/python-net/advanced-operations/working-with-documents/worki…
AnHolub Sep 17, 2025
b34b5bf
Update lastmod dates and add code examples for converting PDF to PNG …
AnHolub Sep 17, 2025
9f2b211
Add conversion guides for PDF/x and PDF/A formats in Python
AnHolub Sep 17, 2025
d979436
Add documentation for replacing and extracting images in PDF using Py…
AnHolub Sep 18, 2025
45030dd
minoe fix
AnHolub Sep 18, 2025
920f5a5
Update en/python-net/converting/convert-pdfx-to-pdf/_index.md
AnHolub Sep 18, 2025
fd5ac18
Update en/python-net/converting/convert-pdf-to-images-format/_index.md
AnHolub Sep 18, 2025
0f666de
Update en/python-net/converting/convert-pdf-to-images-format/_index.md
AnHolub Sep 18, 2025
461c44a
Update en/python-net/converting/convert-pdf-to-images-format/_index.md
AnHolub Sep 18, 2025
ce0042a
Update conversion guide for PDF/x to include PDF/A compliance details
AnHolub Sep 18, 2025
c4c271d
Update en/python-net/converting/convert-html-to-pdf/_index.md
AnHolub Sep 22, 2025
34d2f3e
Update en/python-net/converting/convert-pdf-to-html/_index.md
AnHolub Sep 22, 2025
7a96828
Update en/python-net/converting/convert-pdf-to-html/_index.md
AnHolub Sep 22, 2025
b899822
Update en/python-net/converting/convert-pdf-to-html/_index.md
AnHolub Sep 22, 2025
c748105
Update en/python-net/converting/convert-pdf-to-images-format/_index.md
AnHolub Sep 22, 2025
71a0e82
Update en/python-net/converting/convert-pdf-to-html/_index.md
AnHolub Sep 22, 2025
d31da15
Update en/python-net/converting/convert-pdf-to-html/_index.md
AnHolub Sep 22, 2025
54af073
Update en/python-net/converting/convert-pdf-to-html/_index.md
AnHolub Sep 22, 2025
578a9c6
Refactor documentation for PDF conversion in Python
AnHolub Sep 22, 2025
142302c
Merge branch 'develop' into conversion
AnHolub Sep 22, 2025
36bf6f1
Merge branch 'develop' into images
AnHolub Sep 22, 2025
f5b66a5
Update extract images from PDF documentation for clarity and detail
AnHolub Sep 22, 2025
991d3a4
Update documentation for converting PDF to Word in Python
AnHolub Sep 22, 2025
d978e65
Update DICOM conversion instructions and code snippets in documentation
AnHolub Sep 22, 2025
1938018
Add abbreviations for image formats in conversion documentation
AnHolub Sep 22, 2025
4a15b30
Update documentation for converting various file formats to PDF in Py…
AnHolub Sep 30, 2025
de9fac3
Update en/python-net/converting/convert-html-to-pdf/_index.md
AnHolub Oct 1, 2025
a63f7d3
Update en/python-net/converting/convert-html-to-pdf/_index.md
AnHolub Oct 1, 2025
4b258fa
Update en/python-net/converting/convert-images-format-to-pdf/_index.md
AnHolub Oct 1, 2025
d9dc909
Add HTML and PPTX abbreviations in conversion documentation
AnHolub Oct 1, 2025
e3f3e01
Update en/python-net/advanced-operations/working-with-images/delete-i…
AnHolub Oct 1, 2025
adae019
Update en/python-net/advanced-operations/working-with-images/extract-…
AnHolub Oct 1, 2025
07643e5
Update en/python-net/advanced-operations/working-with-images/extract-…
AnHolub Oct 1, 2025
7c5594c
Update en/python-net/advanced-operations/working-with-images/extract-…
AnHolub Oct 1, 2025
fcaa343
Update en/python-net/advanced-operations/working-with-images/extract-…
AnHolub Oct 1, 2025
4396619
Refine descriptions for image handling examples in PDF documentation
AnHolub Oct 1, 2025
58e8840
Merge branch 'images' of https://github.com/aspose-pdf/Aspose.PDF-Doc…
AnHolub Oct 1, 2025
1f6bf5d
Merge pull request #497 from aspose-pdf/images
AnHolub Oct 1, 2025
cb698c4
Merge pull request #496 from aspose-pdf/conversion
AnHolub Oct 1, 2025
50c47a2
Merge branch 'master' into develop
andruhovski Oct 1, 2025
5b7513b
Update en/python-net/converting/convert-pdf-to-pdfx/_index.md
andruhovski Oct 1, 2025
fe3ebf3
Apply suggestion from @Copilot
andruhovski Oct 1, 2025
9226320
Update en/python-net/converting/convert-pdf-to-pdfx/_index.md
andruhovski Oct 1, 2025
bc13826
Fix formatting and improve image processing logic in PDF extraction e…
andruhovski Oct 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ You are able to do the following:
- [Merge PDF](/pdf/python-net/merge-pdf-documents/) - merge multiple PDF files into a single PDF document using Python.
- [Split PDF](/pdf/python-net/split-document/) - split PDF pages into individual PDF files in your Python applications.
- [Working with Headings](/pdf/python-net/working-with-headings/) - you can create numbering in heading your PDF document with Python.

- [Working with PDF layers](/pdf/python-net/working-with-pdf-layers/) - how to manage and manipulate PDF layers using the Aspose.PDF for Python via .NET library.
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
---
title: Work with PDF layers using Python
linktitle: Work with PDF layers
type: docs
weight: 50
url: /net/working-with-pdf-layers/
description: The next task explains how to lock a PDF layer, extract PDF layer elements, flatten a layered PDF, and merge all layers inside PDF into one.
lastmod: "2025-09-17"
sitemap:
changefreq: "monthly"
priority: 0.7
TechArticle: true
AlternativeHeadline: Manipulate the PDF Layers
Abstract: This guide provides a comprehensive overview of how to manage and manipulate PDF layers using the Aspose.PDF for Python via .NET library. PDF layers—also known as Optional Content Groups (OCGs)—enable the organization of content into separate visual components that can be toggled on or off.
---

PDF layers are a powerful way to organize and present content flexibly inside a single PDF file, allowing users to show or hide different parts depending on their needs.

## Add layers to PDF

This example shows how to create and add multiple layers to a PDF document using Aspose.PDF for Python via .NET. Each layer contains separate graphical content, such as colored lines, which can be turned on or off in PDF viewers that support layers.

1. Create a new PDF document and add a page.
1. Create and add the red layer.
1. Create and add the green layer.
1. Create and add the blue layer.
1. Save the PDF document.

The resulting PDF will contain three separate layers: a red line, a green line, and a blue line. Each can be toggled on or off in PDF readers that support layered content.

```python

from io import FileIO
import aspose.pdf as ap
from os import path

path_outfile = path.join(self.dataDir, outfile)

try:
document = ap.Document()
page = document.pages.add()

# Red layer
layer = ap.Layer("oc1", "Red Line")
layer.contents.append(ap.operators.SetRGBColorStroke(1, 0, 0))
layer.contents.append(ap.operators.MoveTo(500, 700))
layer.contents.append(ap.operators.LineTo(400, 700))
layer.contents.append(ap.operators.Stroke())
page.layers.append(layer)

# Green layer
layer = ap.Layer("oc2", "Green Line")
layer.contents.append(ap.operators.SetRGBColorStroke(0, 1, 0))
layer.contents.append(ap.operators.MoveTo(500, 750))
layer.contents.append(ap.operators.LineTo(400, 750))
layer.contents.append(ap.operators.Stroke())
page.layers.append(layer)

# Blue layer
layer = ap.Layer("oc3", "Blue Line")
layer.contents.append(ap.operators.SetRGBColorStroke(0, 0, 1))
layer.contents.append(ap.operators.MoveTo(500, 800))
layer.contents.append(ap.operators.LineTo(400, 800))
layer.contents.append(ap.operators.Stroke())
page.layers.append(layer)

document.save(path_outfile)
print(f"\nLayers added successfully to PDF file.\nFile saved at {path_outfile}")
except Exception as e:
print(f"Error adding layers: {e}")
```

## Lock a PDF layer

With Aspose.PDF for Python via .NET you can open a PDF, lock a specific layer on the first page, and save the document with the changes.

This example shows how to lock a layer (Optional Content Group, OCG) in a PDF document using Aspose.PDF for Python via .NET. Locking prevents users from changing the visibility of the layer in a PDF viewer, ensuring that the content remains always visible (or hidden) as defined by the document.

1. Open the PDF document.
1. Access the first page of the PDF.
1. Check if the page has layers.
1. Get the first layer and lock it.
1. Save the updated PDF.

If the PDF contains layers, the first layer will be locked, ensuring its visibility state cannot be changed by the user. If no layers are found, a message is printed instead.

```python

from io import FileIO
import aspose.pdf as ap
from os import path

path_infile = path.join(self.dataDir, infile)
path_outfile = path.join(self.dataDir, outfile)

try:
document = ap.Document(path_infile)
page = document.pages[1]

if len(page.layers) > 0:
layer = page.layers[0]
layer.lock()
document.save(path_outfile)
print(f"Layer locked successfully. File saved at {path_outfile}")
else:
print("No layers found in the document.")
except Exception as e:
print(f"Error locking layer: {e}")
```

## Extract PDF layer elements

This example uses the Aspose.PDF for Python via .NET library to extract individual layers from the first page of a PDF document and save each layer as a separate PDF file.

To create a new PDF from a layer, the following code snippet can be used:

1. Load the PDF Document. The input PDF is loaded into an Aspose.PDF.Document object.
1. Access Layers on Page 1. The script retrieves all layers from the first page using document.pages[1].layers.
1. Check for Layers. If no layers are found, a message is printed and the function exits.
1. Iterate and Save Each Layer.

```python

from io import FileIO
import aspose.pdf as ap
from os import path

path_infile = path.join(self.dataDir, infile)
path_outfile = path.join(self.dataDir, outfile)

try:
document = ap.Document(path_infile)
layers = document.pages[1].layers

if len(layers) == 0:
print("No layers found in the document.")
return

index = 1
for layer in layers:
output_file = path_outfile.replace(".pdf", f"{index}.pdf")
layer.save(output_file)
print(f"Layer {index} saved to {output_file}")
index += 1
except Exception as e:
print(f"Error extracting layers: {e}")
```

It is possible to extract PDF layer elements and save them into a new PDF file stream:

```python

from io import FileIO
import aspose.pdf as ap
from os import path

path_infile = path.join(self.dataDir, infile)
path_outfile = path.join(self.dataDir, outfile)

try:
document = ap.Document(path_infile)

if len(document.pages[1].layers) == 0:
print("No layers found in the document.")
return

layer = document.pages[1].layers[0]

with FileIO(path_outfile, "wb") as output_layer:
layer.save(output_layer)
print(f"Layer extracted to stream: {path_outfile}")
except Exception as e:
print(f"Error extracting layer to stream: {e}")
```

## Flatten a layered PDF

This script uses Aspose.PDF for Python via .NET to flatten all layers on the first page of a PDF document. Flattening merges the visual content of each layer into one unified layer, making it easier to print, share, or archive without losing visual fidelity or layer-specific data.

1. Load the PDF Document. The input PDF is loaded into an Aspose.PDF.Document object.
1. Access Layers on Page 1. The script retrieves all layers from the first page using document.pages[1].layers.
1. Check for Layer Presence. If no layers are found, a message is printed and the function exits.
1. Flatten Each Layer. Each layer is flattened using layer.flatten(True), which merges its content into the page.
1. Save the Modified Document.

```python

from io import FileIO
import aspose.pdf as ap
from os import path

path_infile = path.join(self.dataDir, infile)
path_outfile = path.join(self.dataDir, outfile)

try:
document = ap.Document(path_infile)
layers = document.pages[1].layers

if len(layers) == 0:
print("No layers found in the document.")
return

for layer in layers:
layer.flatten(True)

document.save(path_outfile)
print(f"Layers flattened successfully. File saved at {path_outfile}")
except Exception as e:
print(f"Error flattening layers: {e}")
```

## Merge All Layers inside the PDF into one

This code snippet uses Aspose.PDF to merge all layers on the first page of a PDF into one unified layer with a custom name.

1. Load the PDF Document. The PDF is loaded into an Aspose.PDF.Document object.
1. Access Page and Its Layers. The first page is selected, and its layers are retrieved.
1. Check for Layers. If no layers exist, a message is printed and the process exits.
1. Define New Layer Name. A new layer name ("LayerNew") is specified for the merged result.
1. Merge Layers. If an optional content group ID is provided, it's used in the merge. Otherwise, layers are merged using just the new name.
1. Save the Document

```python

from io import FileIO
import aspose.pdf as ap
from os import path

path_infile = path.join(self.dataDir, infile)
path_outfile = path.join(self.dataDir, outfile)

try:
document = ap.Document(path_infile)
page = document.pages[1]

if len(page.layers) == 0:
print("No layers found in the document.")
return

new_layer_name = "LayerNew"
new_optional_content_group_id = None

if new_optional_content_group_id:
page.merge_layers(new_layer_name, new_optional_content_group_id)
else:
page.merge_layers(new_layer_name)

document.save(path_outfile)
print(f"Layers merged successfully. File saved at {path_outfile}")
except Exception as e:
print(f"Error merging layers: {e}")
```
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type: docs
weight: 40
url: /python-net/working-with-images/
description: This section describes the features of working with images in a PDF file using Python library.
lastmod: "2025-02-27"
lastmod: "2025-09-27"
sitemap:
changefreq: "monthly"
priority: 0.7
Expand All @@ -23,4 +23,6 @@ You are able to do the following:
- [Add Image to Existing PDF File](/pdf/python-net/add-image-to-existing-pdf-file/) - add images and references of a single image in PDF document, after that control quality.
- [Delete Images from PDF File](/pdf/python-net/delete-images-from-pdf-file/) - check code snippet for deleting images from PDF file.
- [Extract Images from PDF File](/pdf/python-net/extract-images-from-pdf-file/) - the next article shows how to extract images from PDF file using Python library.
- [Search and Get Images from PDF Document](/pdf/python-net/search-and-get-images-from-pdf-document/) - you can get an image from an individual page and search among images on all pages with Python.
- [Replace Image in Existing PDF File](/pdf/python-net/replace-image-in-existing-pdf-file/) - check our code snippet, it shows you how to replace an image in a PDF file.

Loading