Backgrounds & Foregrounds
You can easily add a background or foreground overlay to either a newly rendered or an existing PDF document.
In the code example below, we begin by rendering a PDF from a specified URL. This PDF serves as the base document. Next, we open another PDF document named "MyBackground.pdf" to use as a background layer.
To add a background, simply pass the background PDF to the addBackgroundFromAnotherPdf
method. This action effectively positions the content of "MyBackground.pdf" behind the content of the URL-rendered PDF.
Additionally, we open another PDF document named "MyForeground.pdf" to be used as a foreground overlay. To include a foreground overlay PDF on the first page (page index 0) of the existing PDF, we utilize the addForegroundFromAnotherPdf
method. This overlay is then displayed on top of the existing content.
Finally, the merged PDF document, which includes the base PDF, background layer, and foreground overlay, is saved as "Complete.pdf."
import PyPDF2 # Import the PyPDF2 library which allows for PDF manipulation
# Render the base PDF from a specified URL
base_pdf_url = "http://example.com/sample.pdf"
base_pdf = PyPDF2.PdfReader(base_pdf_url)
# Open the background PDF to be used as a background layer
background_pdf_path = "MyBackground.pdf"
background_pdf = PyPDF2.PdfReader(background_pdf_path)
# Method to add background from another PDF
def add_background_from_another_pdf(base_pdf, background_pdf):
# Assuming the background is added to all pages
for page_num in range(len(base_pdf.pages)):
# Logic to merge background_pdf content into base_pdf
base_page = base_pdf.pages[page_num]
background_page = background_pdf.pages[0] # Using page 0 if single-page background
# Call to a method that merges background page to base page
base_page.merge_page(background_page)
# Call the function to add the background
add_background_from_another_pdf(base_pdf, background_pdf)
# Open the foreground PDF to be used as a foreground overlay
foreground_pdf_path = "MyForeground.pdf"
foreground_pdf = PyPDF2.PdfReader(foreground_pdf_path)
# Method to add foreground from another PDF
def add_foreground_from_another_pdf(base_pdf, foreground_pdf, page_index):
# Logic to add foreground overlay to a specific page
base_page = base_pdf.pages[page_index]
foreground_page = foreground_pdf.pages[0] # Assuming first page of foreground is used
# Call to a method that merges foreground page to base page
base_page.merge_page(foreground_page)
# Add the foreground overlay to the first page (index 0)
add_foreground_from_another_pdf(base_pdf, foreground_pdf, 0)
# Save the merged PDF document
output_pdf_path = "Complete.pdf"
with open(output_pdf_path, "wb") as output_file:
base_pdf_writer = PyPDF2.PdfWriter()
for page in base_pdf.pages:
base_pdf_writer.add_page(page)
base_pdf_writer.write(output_file) # Save the final document
import PyPDF2 # Import the PyPDF2 library which allows for PDF manipulation
# Render the base PDF from a specified URL
base_pdf_url = "http://example.com/sample.pdf"
base_pdf = PyPDF2.PdfReader(base_pdf_url)
# Open the background PDF to be used as a background layer
background_pdf_path = "MyBackground.pdf"
background_pdf = PyPDF2.PdfReader(background_pdf_path)
# Method to add background from another PDF
def add_background_from_another_pdf(base_pdf, background_pdf):
# Assuming the background is added to all pages
for page_num in range(len(base_pdf.pages)):
# Logic to merge background_pdf content into base_pdf
base_page = base_pdf.pages[page_num]
background_page = background_pdf.pages[0] # Using page 0 if single-page background
# Call to a method that merges background page to base page
base_page.merge_page(background_page)
# Call the function to add the background
add_background_from_another_pdf(base_pdf, background_pdf)
# Open the foreground PDF to be used as a foreground overlay
foreground_pdf_path = "MyForeground.pdf"
foreground_pdf = PyPDF2.PdfReader(foreground_pdf_path)
# Method to add foreground from another PDF
def add_foreground_from_another_pdf(base_pdf, foreground_pdf, page_index):
# Logic to add foreground overlay to a specific page
base_page = base_pdf.pages[page_index]
foreground_page = foreground_pdf.pages[0] # Assuming first page of foreground is used
# Call to a method that merges foreground page to base page
base_page.merge_page(foreground_page)
# Add the foreground overlay to the first page (index 0)
add_foreground_from_another_pdf(base_pdf, foreground_pdf, 0)
# Save the merged PDF document
output_pdf_path = "Complete.pdf"
with open(output_pdf_path, "wb") as output_file:
base_pdf_writer = PyPDF2.PdfWriter()
for page in base_pdf.pages:
base_pdf_writer.add_page(page)
base_pdf_writer.write(output_file) # Save the final document