-
Notifications
You must be signed in to change notification settings - Fork 5
Add new articles in Python via .NET sections #498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
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 78ae312
feat: add FDF import functionality to PDF form data handling
AnHolub 078e3b3
fix: update installation guide for Aspose.PDF for Rust via C++
AnHolub e255262
Merge pull request #490 from aspose-pdf/fixrust
AnHolub aaff771
Merge remote-tracking branch 'origin/develop' into acroforms
AnHolub d662d61
fix: moved article Working with Headings in PDF into correct location
andruhovski 6b44868
Merge branch 'develop' of https://github.com/aspose-pdf/Aspose.PDF-Do…
andruhovski 5144623
Merge branch 'master' into develop
andruhovski 39b8cce
Refactor AcroForm examples for Python
AnHolub f1f3269
Update en/python-net/advanced-operations/working-with-forms/acroforms…
AnHolub 9ef3a5c
Update en/python-net/advanced-operations/working-with-forms/acroforms…
AnHolub 9d001d7
Merge pull request #491 from aspose-pdf/acroforms
AnHolub bf7185b
Update en/python-net/advanced-operations/working-with-forms/acroforms…
andruhovski f8f998a
Update en/python-net/advanced-operations/working-with-forms/acroforms…
andruhovski 9100630
fix: fixed typos
andruhovski ace9735
fix: Rename AcroForms JSON export doc and update sitemap
andruhovski 1089acf
Fix formatting and capitalization in AcroForm documentation
andruhovski 9cb7f3b
Update en/python-net/advanced-operations/working-with-forms/acroforms…
andruhovski a39ad14
Update en/python-net/advanced-operations/working-with-forms/acroforms…
andruhovski a1017a1
Update en/python-net/advanced-operations/working-with-forms/acroforms…
andruhovski caada0a
refactor: Refactor PDF form file path handling in docs
andruhovski 4ac073a
refactor: Update file path handling in AcroForm examples for consistency
andruhovski 05ff256
Fix formatting and typos in AcroForms docs
andruhovski e9b35d2
Merge branch 'master' into develop
AnHolub 8440c4a
feat: Add FAQ section for Aspose.PDF for Python via .NET with common …
AnHolub efa7cea
Merge pull request #494 from aspose-pdf/faq_page
AnHolub be982c5
chore: Update documentation for clarity and consistency
AnHolub b64b348
chore: Update FAQ metadata for Aspose.PDF for Python via .NET
AnHolub 4bb24b6
Merge branch 'develop' of https://github.com/aspose-pdf/Aspose.PDF-Do…
AnHolub 758ca05
Merge branch 'whats_new' into develop
AnHolub 1aa67bf
chore: Update content for adding headers and footers in PDF documenta…
AnHolub 2e5b5ab
chore: Add documentation for working with PDF layers in Python
AnHolub 7853fc7
Merge pull request #495 from aspose-pdf/layers
AnHolub d602f89
Update documentation for converting files to PDF in Python
AnHolub 4d1da0f
chore: Add code examples for converting CDR and JPEG images to PDF in…
AnHolub 7558102
Add code examples for converting
AnHolub 6831157
Update en/python-net/advanced-operations/working-with-documents/_inde…
AnHolub b1dae30
Update en/python-net/advanced-operations/working-with-documents/worki…
AnHolub b34b5bf
Update lastmod dates and add code examples for converting PDF to PNG …
AnHolub 9f2b211
Add conversion guides for PDF/x and PDF/A formats in Python
AnHolub d979436
Add documentation for replacing and extracting images in PDF using Py…
AnHolub 45030dd
minoe fix
AnHolub 920f5a5
Update en/python-net/converting/convert-pdfx-to-pdf/_index.md
AnHolub fd5ac18
Update en/python-net/converting/convert-pdf-to-images-format/_index.md
AnHolub 0f666de
Update en/python-net/converting/convert-pdf-to-images-format/_index.md
AnHolub 461c44a
Update en/python-net/converting/convert-pdf-to-images-format/_index.md
AnHolub ce0042a
Update conversion guide for PDF/x to include PDF/A compliance details
AnHolub c4c271d
Update en/python-net/converting/convert-html-to-pdf/_index.md
AnHolub 34d2f3e
Update en/python-net/converting/convert-pdf-to-html/_index.md
AnHolub 7a96828
Update en/python-net/converting/convert-pdf-to-html/_index.md
AnHolub b899822
Update en/python-net/converting/convert-pdf-to-html/_index.md
AnHolub c748105
Update en/python-net/converting/convert-pdf-to-images-format/_index.md
AnHolub 71a0e82
Update en/python-net/converting/convert-pdf-to-html/_index.md
AnHolub d31da15
Update en/python-net/converting/convert-pdf-to-html/_index.md
AnHolub 54af073
Update en/python-net/converting/convert-pdf-to-html/_index.md
AnHolub 578a9c6
Refactor documentation for PDF conversion in Python
AnHolub 142302c
Merge branch 'develop' into conversion
AnHolub 36bf6f1
Merge branch 'develop' into images
AnHolub f5b66a5
Update extract images from PDF documentation for clarity and detail
AnHolub 991d3a4
Update documentation for converting PDF to Word in Python
AnHolub d978e65
Update DICOM conversion instructions and code snippets in documentation
AnHolub 1938018
Add abbreviations for image formats in conversion documentation
AnHolub 4a15b30
Update documentation for converting various file formats to PDF in Py…
AnHolub de9fac3
Update en/python-net/converting/convert-html-to-pdf/_index.md
AnHolub a63f7d3
Update en/python-net/converting/convert-html-to-pdf/_index.md
AnHolub 4b258fa
Update en/python-net/converting/convert-images-format-to-pdf/_index.md
AnHolub d9dc909
Add HTML and PPTX abbreviations in conversion documentation
AnHolub e3f3e01
Update en/python-net/advanced-operations/working-with-images/delete-i…
AnHolub adae019
Update en/python-net/advanced-operations/working-with-images/extract-…
AnHolub 07643e5
Update en/python-net/advanced-operations/working-with-images/extract-…
AnHolub 7c5594c
Update en/python-net/advanced-operations/working-with-images/extract-…
AnHolub fcaa343
Update en/python-net/advanced-operations/working-with-images/extract-…
AnHolub 4396619
Refine descriptions for image handling examples in PDF documentation
AnHolub 58e8840
Merge branch 'images' of https://github.com/aspose-pdf/Aspose.PDF-Doc…
AnHolub 1f6bf5d
Merge pull request #497 from aspose-pdf/images
AnHolub cb698c4
Merge pull request #496 from aspose-pdf/conversion
AnHolub 50c47a2
Merge branch 'master' into develop
andruhovski 5b7513b
Update en/python-net/converting/convert-pdf-to-pdfx/_index.md
andruhovski fe3ebf3
Apply suggestion from @Copilot
andruhovski 9226320
Update en/python-net/converting/convert-pdf-to-pdfx/_index.md
andruhovski bc13826
Fix formatting and improve image processing logic in PDF extraction e…
andruhovski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
252 changes: 252 additions & 0 deletions
252
...on-net/advanced-operations/working-with-documents/working-with-layers/_index.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}") | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.