USING IRONPDF FOR PYTHON

How to Add or Remove PDF Pages Using Python

Updated September 28, 2024
Share:

This article will demonstrate how to add or remove PDF pages using Python and a PDF library named IronPDF for Python.

1. IronPDF for Python

IronPDF is a market-leading PDF Python library that provides developers with the capability to effortlessly generate, manipulate, and work with PDF documents in their applications. With IronPDF, developers can seamlessly integrate PDF functionality into their Python projects, whether it be for creating dynamic reports, generating invoices, or converting web content into PDF files. This library offers a user-friendly and efficient way to handle PDF-related tasks, enabling you to create and manipulate PDFs with ease.

Whether you're building web applications, desktop software, or automating document workflows, IronPDF is a valuable tool that empowers you to work with PDFs in the Python environment, making it an essential addition to any developer's toolkit. This introductory guide will explore the key features and capabilities of IronPDF for Python. Using IronPDF, developers can merge several PDF files into a single document, extract text from a particular page, add watermarks, and perform other operations such as deleting pages, removing a blank page, rotating pages, adding pages, and reading PDF files.

2. Installing IronPDF

To install IronPDF, just open PyCharm or any other Python compiler, and create a new Python project or open an existing one. Once the project is created or opened, just go ahead and open the terminal.

IronPDF for Python can be easily installed using the terminal command. Just run the following command in the terminal, and IronPDF should be installed in a minute.

pip install ironpdf

How to Add or Remove PDF Pages Using Python, Figure 1: Install IronPDF package Install IronPDF package

Once the installation is completed, you are all set to start playing with the code.

3. Code Examples

Before adding and removing PDF pages from a PDF document, let's create a 4-page simple PDF file using HTML to PDF conversion. The code below creates PDF files to use as an input PDF document for the upcoming code examples.

from ironpdf import *

html = """<p> Hello Iron</p>
    <p> This is 1st Page </p>
    <div style='page-break-after: always;'></div>
    <p> This is 2nd Page</p>
    <div style='page-break-after: always;'></div>
    <p> This is 3rd Page</p>
    <div style='page-break-after: always;'></div>
    <p> This is 4th Page</p>"""
renderer = ChromePdfRenderer()
pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("Page1And4.pdf")
PYTHON

This Python code uses the IronPDF library to create a PDF document from HTML content. The HTML content is defined as a string, containing paragraphs and "page-break-after" div tags, indicating page breaks. It's structured to have four pages. The code then uses the ChromePdfRenderer to convert this HTML into a PDF document. Finally, it saves the resulting PDF as "Page1And4.pdf".

Essentially, this code generates a PDF with multiple pages, where each page corresponds to the content between two consecutive "page-break" div tags in the HTML, and it saves this HTML content to a PDF file.

How to Add or Remove PDF Pages Using Python, Figure 2: OUTPUT file: Page1And4.pdf Page1And4.pdf

3.1. Removing Specific Page From PDF Files Using IronPDF

This section will remove pages from a PDF created earlier. The following code will remove a page from the PDF file.

from ironpdf import *

pdf = PdfDocument.FromFile("Page1And4.pdf")
pdf.RemovePage(1)
pdf.SaveAs("removed.pdf")
PYTHON

The above code utilizes the IronPDF library to manipulate a PDF document. It begins by importing the necessary components and then loads an existing PDF document called "Page1And4.pdf" using the FromFile() method. It proceeds to delete PDF pages from the PDF, identified by its index '1', and subsequently calls the SaveAs method that saves the modified document as a new PDF file named removed.pdf. In essence, the code performs the task of removing the first page from the original PDF document and saving the resulting document as a separate file.

3.1.1. Output PDF file

How to Add or Remove PDF Pages Using Python, Figure 3: Output file Output file

3.2. Add A Page in PDF Document Using IronPDF

This section will discuss how to add a new page in existing PDF files. For this, let's create a new PDF file and then add the newly created PDF to the previously created PDF file using page numbers with just a few lines of code.

Below is the sample code of adding a new PDF page into the original document.

from ironpdf import *

pdf_page = """
        <h1> Cover Page</h1>"""
renderer = ChromePdfRenderer()
pdfdoc_a = renderer.RenderHtmlAsPdf(pdf_page)
pdf = PdfDocument.FromFile("removed.pdf")
pdf.PrependPdf(pdfdoc_a)
pdf.SaveAs("addPage.pdf")
PYTHON

This Python code snippet leverages the IronPDF library to manipulate PDF documents. Initially, it defines an HTML content snippet representing a cover page with a title. Then, it employs the ChromePdfRenderer() method to convert this HTML into a PDF document, storing it in pdfdoc_a.

Then, it loads an existing PDF document, "removed.pdf," using PdfDocument.FromFile("removed.pdf"). The code proceeds to prepend the content of pdfdoc_a to this existing PDF using the pdf.PrependPdf(pdfdoc_a) method. Essentially, this code combines the cover page PDF with the "removed.pdf", creating a new PDF document named "addPage.pdf", effectively adding the cover page to the beginning of the original PDF.

How to Add or Remove PDF Pages Using Python, Figure 4: Output file Output file

4. Conclusion

This article explored the world of PDF manipulation using Python, with a focus on the IronPDF library. The ability to add or remove pages from PDF documents is a valuable skill in today's digital landscape, and Python offers an accessible and powerful way to achieve these tasks. The article covered the essential steps for installing IronPDF and provided code examples to illustrate the process of creating, removing, and adding pages in PDFs.

With IronPDF, Python developers can efficiently work with PDF documents, whether for generating reports, customizing content, or improving document workflows. As the digital world continues to rely on PDFs for various purposes, mastering these techniques empowers developers to meet a wide range of needs, making Python and IronPDF a dynamic combination for PDF manipulation.

The code example of removing PDF pages can be found at the following sample code. The code example of adding PDF pages can be found in another Python code example. Also, if you are curious about how HTML to PDF conversion works, please visit this tutorial page.

Explore the versatile features of IronPDF for Python library and experience the transformation by opting for a free trial today.

< PREVIOUS
How to Easily Annotate PDFs in Python Using IronPDF
NEXT >
How to Split PDF Files in Python

Ready to get started? Version: 2024.9 just released

Free pip Install View Licenses >