News Category

Adding the ability to edit permission area  in a Word document can help users specify certain sections for others to edit while protecting the rest of the document from accidental modifications. This is particularly useful for scenarios like collaborative documents, document reviews, and comments. On the other hand, removing editable area  functionality allows the document to be restored to a read-only state when specific sections do not need to be edited, ensuring the integrity and security of the document content. This article will explain how to use Spire.Doc for .NET to add or remove editable area  in a Word document within a C# project.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Add Editable Area in a Word Document in C#

The steps to add editable area  in a Word document involve inserting PermissionStart and PermissionEnd objects in the document and setting the document to read-only protection mode to ensure that the content within the specified areas can be edited while the rest remains read-only. Here are the detailed steps:

  • Create a Document object.
  • Load a Word document using the Document.LoadFromFile() method.
  • Access a section of the document through the Document.Sections[index] property.
  • Create a PermissionStart object using PermissionStart permissionStart = new PermissionStart(document, id) to mark the beginning of the editable area .
  • Create a PermissionEnd object using PermissionEnd permissionEnd = new PermissionEnd(document, id) to mark the end of the editable area .
  • Access a paragraph using the Section.Paragraphs[index] property.
  • Insert the permission start object at the beginning of the paragraph using the Paragraph.ChildObjects.Insert(0, permissionStart) method.
  • Add the permission end object at the end of the paragraph using the Paragraph.ChildObjects.Add(permissionEnd) method.
  • Set the document to read-only protection mode and restrict editing permissions using the Document.Protect(ProtectionType.AllowOnlyReading, password) method.
  • Save the resulting document using the Document.SaveToFile() method.
  • C#
using Spire.Doc;
using Spire.Doc.Documents;

namespace SpireDocDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Create a new document object
            Document document = new Document();

            // Load the document from the specified path
            document.LoadFromFile("Sample1.docx");

            // Get the first section of the document
            Section section = document.Sections[0];

            // Create a permission start object
            PermissionStart permissionStart = new PermissionStart(document, "restricted1");

            // Create a permission end object
            PermissionEnd permissionEnd = new PermissionEnd(document, "restricted1");

            // Get the second paragraph in the section
            Paragraph paragraph = section.Paragraphs[1];

            // Insert the permission start object at the beginning of the paragraph
            paragraph.ChildObjects.Insert(0, permissionStart);

            // Add the permission end object at the end of the paragraph
            paragraph.ChildObjects.Add(permissionEnd);

            // Set the document to be read-only protected
            document.Protect(ProtectionType.AllowOnlyReading, "123456");

            // Save the modified document to the specified path
            document.SaveToFile("AddedEditingPermissionsArea.docx", FileFormat.Docx);

            // Close the document and release the resources occupied by the document object
            document.Close();
            document.Dispose();
        }
  }
}

C#: Add or Remove Editable area in a Word Document

Remove Editable Area in a Word Document in C#

The key steps to remove editable area  in a Word document involve iterating through each paragraph of the document and removing the PermissionStart and PermissionEnd objects. Here are the detailed steps:

  • Create a Document object.
  • Load a Word document using the Document.LoadFromFile() method.
  • Iterate through each paragraph in each section of the document, check for the presence of PermissionStart or PermissionEnd objects, and remove them.
  • Save the resulting document using the Document.SaveToFile() method.
  • C#
using Spire.Doc;
using Spire.Doc.Documents;

namespace SpireDocDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
             // Create a new document object
            Document document = new Document();

            // Load the document from the specified path
            document.LoadFromFile("Sample2.docx");

            // Iterate through the sections of the document
            for (int a = 0; a < document.Sections.Count; a++)
            {
                // Get the body of the current section
                Body body = document.Sections[a].Body;

                // Iterate through the child objects of the body
                for (int i = 0; i < body.ChildObjects.Count; i++)
                {
                    // Check if the child object is a paragraph
                    if (body.ChildObjects[i] is Paragraph)
                    {
                        // Get the current paragraph
                        Paragraph paragraph = (Paragraph)body.ChildObjects[i];

                        // Iterate backwards from the last child object of the paragraph
                        for (int j = paragraph.ChildObjects.Count - 1; j >= 0; j--)
                        {
                            // Get the current child object
                            DocumentObject documentObject = paragraph.ChildObjects[j];

                            // Remove the current child object if it is a permission start object
                            if (documentObject.DocumentObjectType == DocumentObjectType.PermissionStart)
                            {
                                paragraph.ChildObjects.RemoveAt(j);
                            }
                            // Remove the current child object if it is a permission end object
                            else if (documentObject.DocumentObjectType == DocumentObjectType.PermissionEnd)
                            {
                                paragraph.ChildObjects.RemoveAt(j);
                            }
                        }
                    }
                }
            }

            // Save the modified document to the specified path
            document.SaveToFile("RemovedEditingPermissionsArea.docx", FileFormat.Docx);

            // Close the document and release the resources occupied by the document object
            document.Close();
            document.Dispose();
        }
    }
}

C#: Add or Remove Editable area in a Word Document

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Merging multiple PowerPoint presentations can be a daunting task, often leading to a less-than-optimal presentation experience. However, there is a solution that can streamline this process and ensure seamless transitions throughout the presentation. By combining multiple PowerPoint files into a single cohesive presentation, presenters can eliminate the need to repeatedly open different files, saving time and effort. While manually copying slides can be arduous and time-consuming, Python offers a swift and efficient solution. This article is going to show how to leverage Spire.Presentation for Python to merge PowerPoint presentations effortlessly through Python programs.

Install Spire.PDF for Python

This scenario requires Spire.Presentation for Python and plum-dispatch v1.7.4. They can be easily installed in your VS Code through the following pip command.

pip install Spire.Presentation

If you are unsure how to install, please refer to: How to Install Spire.Presentation for Python in VS Code

Merging PowerPoint Presentations and Retain Their Designs

Merging PowerPoint presentations can be accomplished by reading slides from one presentation and adding them to another presentation. During the process of adding to the target presentation, developers can use Presentation.Slides.AppendBySlide(ISlide) method to add slides and retain the original design of the slides.

The detailed steps are as follows:

  • Create two instances of Presentation class.
  • Load two PowerPoint presentations using Presentation.LoadFromFile() method.
  • Iterate through each slide in the second presentation and add them to the first presentation while keeping their design using Presentation.Slides.AppendBySlide() method.
  • Save the first presentation using Presentation.SaveToFile() method.
  • Python
from spire.presentation import *
from spire.presentation.common import *

# Create two instances of Presentation class
pres1 = Presentation()
pres2 = Presentation()

# Load two presentation files
pres1.LoadFromFile("Sample1.pptx")
pres2.LoadFromFile("Sample2.pptx")

# Iterate through the slides of the second presentation
for slide in pres2.Slides:
    # Add each slides to the first presentation and keep the original design
    pres1.Slides.AppendBySlide(slide)

# Save the first presentation
pres1.SaveToFile("output/MergePresentations.pptx", FileFormat.Pptx2016)
pres1.Dispose()
pres2.Dispose()

Python: Merge PowerPoint Presentations

Merging PowerPoint Presentations with Consistent Design

Developers can also use Presentation.Slides.AppendByMaster(slide Islide, master IMasterSlide) method to insert slides into the target presentation and change the design of the slides to the design of the target presentation. This allows for merging presentations and ensuring a consistent design.

The detailed stops are as follows:

  • Create two instances of Presentation class.
  • Load two PowerPoint presentations using Presentation.LoadFromFile() method.
  • Iterate through each slide in the second presentation and add them to the first presentation while changing their design to the design of the first presentation using Presentation.Slides.AppendByMaster() method.
  • Save the first presentation using Presentation.SaveToFile() method.
  • Python
from spire.presentation import *
from spire.presentation.common import *

# Create two instances of Presentation class
pres1 = Presentation()
pres2 = Presentation()

# Load two presentation files
pres1.LoadFromFile("Sample1.pptx")
pres2.LoadFromFile("Sample2.pptx")

# Iterate through each slide in the second presentation
for slide in pres2.Slides:
    # Add each slide to the first presentation
    pres1.Slides.AppendByMaster(slide, pres1.Masters[0])

# Save the first presentation
pres1.SaveToFile("output/MergePresentationsDesign.pptx", FileFormat.Pptx2016)
pres1.Dispose()
pres2.Dispose()

Python: Merge PowerPoint Presentations

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Margins in a PDF document refer to the blank spaces surrounding the content on each page. They act as a buffer zone between the text or images and the edges of the page. Changing the margins of a PDF document can be a useful task when you want to adjust the layout, accommodate annotations or comments, or prepare the document for printing or presentation.

This article introduces how to modify the margins of a PDF document using the Spire.PDF for Python library. You will discover techniques to both increase and reduce the margins of your PDFs, enabling you to customize the layout according to your specific requirements.

Install Spire.PDF for Python

This scenario requires Spire.PDF for Python and plum-dispatch v1.7.4. They can be easily installed in your VS Code through the following pip command.

pip install Spire.PDF

If you are unsure how to install, please refer to this tutorial: How to Install Spire.PDF for Python in VS Code

Increase the Margins of a PDF Document in Python

In Spire.PDF for Python, there isn't a direct method to modify the margins of an existing PDF document. However, you can increase the margins by creating a new PDF document with a page size equal to the original document's page size plus the increased margin values. Then, copy and paste (draw) each page of the original document into the appropriate place on the new document page.

The following are the steps to increase the margins of a PDF document using Python.

  1. Create a PdfDocument object called "originalPdf" and load the original PDF document.
  2. Create another PdfDocument object called "newPdf" for creating a new PDF document.
  3. Specify the desired increase values for the top, bottom, left, and right margins.
  4. Calculate the new page size by adding the margin increase values to the original page dimensions.
  5. Create a template based on the original PDF page using PdfPageBase.CreateTemplate() method.
  6. Add a new page to the "newPdf" document with the calculated page size using PdfDocument.Pages.Add() method.
  7. Draw the template onto the new page at the appropriate location to using PdfTemplate.Draw() method.
  8. Repeat steps 5-7 for each page in the original PDF document.
  9. Save the "newPdf" object to a PDF file.
  • Python
from spire.pdf.common import *
from spire.pdf import *

# Create a PdfDocument object
originalPdf = PdfDocument()

# Load a PDF file
originalPdf.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf")

# Get the first page
firstPage = originalPdf.Pages[0]

# Create another PdfDocument object for creating new document
newPdf = PdfDocument()

# Set the increase values of the margins
marginsToAdd = newPdf.PageSettings.Margins
marginsToAdd.Top = 40
marginsToAdd.Bottom = 40
marginsToAdd.Left = 40
marginsToAdd.Right = 40

# Calculate the new page size
sizeF = SizeF(firstPage.Size.Width + marginsToAdd.Left + marginsToAdd.Right, firstPage.Size.Height + marginsToAdd.Top + marginsToAdd.Bottom)

# Iterate through the pages in the original document
for i in range(originalPdf.Pages.Count):

    # Create a template based on a specific page
    pdfTemplate = originalPdf.Pages[i].CreateTemplate()

    # Add a page to the new PDF
    page = newPdf.Pages.Add(sizeF)

    # Draw template on the page
    pdfTemplate.Draw(page, 0.0, 0.0)

# Save the new document
newPdf.SaveToFile("Output/IncreaseMargins.pdf", FileFormat.PDF)

# Dispose resources
originalPdf.Dispose()
newPdf.Dispose()

Python: Change the Margins of a PDF Document

Reduce the Margins of a PDF Document in Python

Similarly, you can reduce the margins by creating a new PDF document with a page size equal to the page size of the original document minus the margin value to be reduced. Then, copy and paste (draw) each page of the original document into the appropriate place on the new document page.

To reduce the margins of a PDF document using Python, follow these steps:

  1. Create a PdfDocument object called "originalPdf" and load the original PDF document.
  2. Create another PdfDocument object called "newPdf" for creating a new PDF document.
  3. Specify the desired reduction values for the top, bottom, left, and right margins.
  4. Calculate the new page size by subtracting the margin value to be reduced from the original page size.
  5. Create a template based on the original PDF page using PdfPageBase.CreateTemplate() method.
  6. Add a new page to the "newPdf" document with the calculated page size using PdfDocument.Pages.Add() method.
  7. Draw the template onto the new page at the appropriate location using PdfTemplate.Draw() method.
  8. Repeat steps 5-7 for each page in the original PDF document.
  9. Save the "newPdf" object to a PDF file.
  • Python
from spire.pdf.common import *
from spire.pdf import *

# Create a PdfDocument object
originalPdf = PdfDocument()

# Load a PDF file
originalPdf.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf")

# Get the first page
firstPage = originalPdf.Pages[0]

# Create another PdfDocument object
newPdf = PdfDocument()

# Set the reduction value of the margins
topToReduce = 20.0
bottomToReduce = 20.0
leftToReduce = 20.0
rightToReduce = 20.0

# Calculate the new page size
sizeF = SizeF(firstPage.Size.Width - leftToReduce - rightToReduce, firstPage.Size.Height - topToReduce - bottomToReduce)

# Iterate through the pages in the original document
for i in range(originalPdf.Pages.Count):

    # Create a template based on a specific page
    pdfTemplate = originalPdf.Pages[i].CreateTemplate()

    # Add a page to the new PDF
    page = newPdf.Pages.Add(sizeF, PdfMargins(0.0))

    # Draw template on the page
    pdfTemplate.Draw(page, -leftToReduce, -topToReduce)

# Save the new document
newPdf.SaveToFile("Output/ReduceMargins.pdf", FileFormat.PDF)

# Dispose resources
originalPdf.Dispose()
newPdf.Dispose()

Python: Change the Margins of a PDF Document

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Page 3 of 288