Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
This article will demonstrate how to add or remove PDF pages using Python and a PDF library named 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.
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, 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
Install IronPDF package
Once the installation is completed, you are all set to start playing with the code.
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 content to be converted to PDF
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>
"""
# Initialize the renderer
renderer = ChromePdfRenderer()
# Render the HTML as a PDF document
pdf = renderer.RenderHtmlAsPdf(html)
# Save the PDF to a file
pdf.SaveAs("Page1And4.pdf")
from ironpdf import *
# HTML content to be converted to PDF
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>
"""
# Initialize the renderer
renderer = ChromePdfRenderer()
# Render the HTML as a PDF document
pdf = renderer.RenderHtmlAsPdf(html)
# Save the PDF to a file
pdf.SaveAs("Page1And4.pdf")
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.
Page1And4.pdf
This section will remove pages from a PDF created earlier. The following code will remove a page from the PDF file.
from ironpdf import *
# Load the existing PDF document
pdf = PdfDocument.FromFile("Page1And4.pdf")
# Remove the page at index 1 (second page)
pdf.RemovePage(1)
# Save the modified PDF to a new file
pdf.SaveAs("removed.pdf")
from ironpdf import *
# Load the existing PDF document
pdf = PdfDocument.FromFile("Page1And4.pdf")
# Remove the page at index 1 (second page)
pdf.RemovePage(1)
# Save the modified PDF to a new file
pdf.SaveAs("removed.pdf")
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 a page 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 second page from the original PDF document and saving the resulting document as a separate file.
Output file
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 *
# HTML content to represent a new page
pdf_page = """
<h1> Cover Page</h1>
"""
# Initialize the renderer and render the new PDF page
renderer = ChromePdfRenderer()
pdfdoc_a = renderer.RenderHtmlAsPdf(pdf_page)
# Load the existing PDF file
pdf = PdfDocument.FromFile("removed.pdf")
# Prepend the new page to the beginning of the existing PDF
pdf.PrependPdf(pdfdoc_a)
# Save the combined PDF to a new file
pdf.SaveAs("addPage.pdf")
from ironpdf import *
# HTML content to represent a new page
pdf_page = """
<h1> Cover Page</h1>
"""
# Initialize the renderer and render the new PDF page
renderer = ChromePdfRenderer()
pdfdoc_a = renderer.RenderHtmlAsPdf(pdf_page)
# Load the existing PDF file
pdf = PdfDocument.FromFile("removed.pdf")
# Prepend the new page to the beginning of the existing PDF
pdf.PrependPdf(pdfdoc_a)
# Save the combined PDF to a new file
pdf.SaveAs("addPage.pdf")
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.
Output file
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.
IronPDF for Python is a leading PDF library that allows developers to generate, manipulate, and work with PDF documents within Python applications. It facilitates various PDF-related tasks like creating reports, generating invoices, converting web content to PDF, and more.
To install IronPDF for Python, open your terminal or Python compiler like PyCharm, and run the command 'pip install ironpdf'. This will install the IronPDF library in your Python environment.
You can add a page to a PDF document by creating a new PDF page using HTML content and the ChromePdfRenderer, then prepend this new page to an existing PDF using the PrependPdf method from IronPDF.
To remove a specific page from a PDF document, load the existing PDF using PdfDocument.FromFile from IronPDF, then use the RemovePage method with the index of the page you wish to remove.
Key features of IronPDF for Python include the ability to merge PDF files, extract text, add watermarks, rotate pages, and read PDF files, among other PDF manipulations.
Yes, IronPDF can convert HTML content to PDF by using the ChromePdfRenderer to render HTML as a PDF document.
Yes, you can experience the features of IronPDF for Python by opting for a free trial.
Applications such as web applications, desktop software, and automated document workflows can benefit from using IronPDF to handle PDF-related tasks.
Code examples for removing PDF pages can be found at the sample code link provided in the article, and examples for adding pages can be found in the Python code example link.
PDF manipulation is important because it allows for efficient document management, customization of content, and automation of document workflows, which are crucial in a digital world that relies heavily on PDFs.