Image

Image (2)

Setting the transparency of images in PDF documents is crucial for achieving professional-grade output, which allows for layering images without hard edges and creating a seamless integration with the background or underlying content. This not only enhances the visual appeal but also creates a polished and cohesive look, especially in graphics-intensive documents. This article will demonstrate how to effectively set the transparency of PDF images using Spire.PDF for Python in Python programs.

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 Windows through the following pip command.

pip install Spire.PDF

If you are unsure how to install, please refer to: How to Install Spire.PDF for Python on Windows

Add Images with Specified Transparency to PDF

Developers can utilize the PdfPageBase.Canvas.DrawImage() method in Spire.PDF for Python to draw an image at a specified location on a PDF page. Before drawing, developers can set the transparency of the canvas using PdfPageBase.Canvas.SetTransparency() method, which in turn sets the transparency level of the image being drawn. Below are the detailed steps:

  • Create an object of PdfDocument class and load a PDF document using PdfDocument.LoadFromFile() method.
  • Get a page in the document using PdfDocument.Pages.get_Item() method.
  • Load an image using PdfImage.FromFile() method.
  • Set the transparency of the canvas using PdfPageBase.Canvas.SetTransparency() method.
  • Draw the image on the page using PdfPageBase.Canvas.DrawImage() method.
  • Save the document using PdfDocument.SaveToFile() method.
  • Python
from spire.pdf import *

# Create a PdfDocument instance
pdf = PdfDocument()

# Load a PDF file
pdf.LoadFromFile("Sample.pdf")

# Get the first page
page = pdf.Pages.get_Item(0)

# Load an image
image = PdfImage.FromFile("Screen.jpg")

# Set the transparency of the canvas
page.Canvas.SetTransparency(0.2)

# Draw the image at the specified location
page.Canvas.DrawImage(image, PointF(80.0, 80.0))

# Save the document
pdf.SaveToFile("output/AddTranslucentPicture.pdf")
pdf.Close()

Python: Set the Transparency of PDF Images

Adjust the Transparency of Existing Images in PDF

To adjust the transparency of an existing image on a PDF page, developers can retrieve the image along with its bounds, delete the image, and finally redraw the image in the same location with the specified transparency. This process allows for the adjustment of the image's opacity while maintaining its original placement. The detailed steps are as follows:

  • Create an object of PdfDocument class and load a PDF document using PdfDocument.LoadFromFile() method.
  • Get a page in the document using PdfDocument.Pages.get_Item() method.
  • Get an image on the page as a stream through PdfPageBase.ImagesInfo[].Image property and get the bounds of the image through PdfPageBase.ImagesInfo[].Bounds property.
  • Remove the image from the page using PdfPageBase.DeleteImage() method.
  • Create a PdfImage instance with the stream using PdfImage.FromStream() method.
  • Set the transparency of the canvas using PdfPageBase.Canvas.SetTransparency() method.
  • Redraw the image in the same location with the specified transparency using PdfPageBase.Canvas.DrawImage() method.
  • Save the document using PdfDocument.SaveToFile() method.
  • Python
from spire.pdf import *

# Create a PdfDocument instance
pdf = PdfDocument()

# Load a PDF file
pdf.LoadFromFile("Sample1.pdf")

# Get the first page
page = pdf.Pages.get_Item(0)

# Get the first image on the page as a stream and the bounds of the image
imageStream = page.ImagesInfo[0].Image
bounds = page.ImagesInfo[0].Bounds

# Delete the original image
page.DeleteImage(0)

# Create a PdfImage instance using the image stream
image = PdfImage.FromStream(imageStream)

# Set the transparency of the canvas
page.Canvas.SetTransparency(0.3)

# Draw the new image at the same location using the canvas
page.Canvas.DrawImage(image, bounds)

# Save the document
pdf.SaveToFile("output/SetExistingImageTransparency.pdf")
pdf.Close()

Python: Set the Transparency of PDF Images

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.

Alongside textual content, images in a PDF play a crucial role in conveying messages effectively. Being able to manipulate images within a PDF document, such as adding, replacing, or removing them, can be incredibly useful for enhancing the visual appeal, updating outdated graphics, or modifying the document's content. In this article, you will learn how to add, replace, or delete images in a PDF document in Python using Spire.PDF for Python.

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 Windows 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 on Windows

Add an Image to a PDF Document in Python

To add an image to a PDF page, you can use the PdfPage.Canvas.DrawImage() method. The following are the detailed steps.

  • Create a PdfDocument object.
  • Add a page to the document using PdfDocument.Pages.Add() method.
  • Load an image using PdfImage.FromFile() method.
  • Draw the image on the page using PdfPageBase.Canvas.DrawImage() method.
  • Save the document using PdfDocument.SaveToFile() method.
  • Python
from spire.pdf.common import *
from spire.pdf import *

# Create a PdfDocument instance
doc = PdfDocument()

# Set the page margins 
doc.PageSettings.SetMargins(30.0, 30.0, 30.0, 30.0)

# Add a page
page = doc.Pages.Add()

# Load an image
image = PdfImage.FromFile('C:/Users/Administrator/Desktop/logo.png')

# Specify the size of the image in the document
width = image.Width * 0.70
height = image.Height * 0.70

# Specify the X and Y coordinates where the image will be drawn
x = 10.0
y = 30.0

# Draw the image at a specified location on the page
page.Canvas.DrawImage(image, x, y, width, height)

# Save the result document
doc.SaveToFile("output/AddImage.pdf", FileFormat.PDF)

Python: Add, Replace, or Remove Images in a PDF Document

Replace an Image in a PDF Document in Python

Spire.PDF for Python offers the PdfImageHelper class to help us get and deal with the images in a certain page. To replace an image with a new one, you can use the PdfImageHelper.ReplaceImage() method. The following are the steps.

  • Create a PdfDocument object.
  • Load a PDF document using PdfDocument.LoadFromFile() method.
  • Get a specific page through PdfDocument.Pages[index] property.
  • Load an image using PdfImage.FromFile() method.
  • Create a PdfImageHelper object, and get the image information from the specified page using PdfImageHelper.GetImagesInfo() method.
  • Replace an existing image in the page with the new image using PdfImageHelper.ReplaceImage() method.
  • Save the document using PdfDocument.SaveToFile() method.
  • Python
from spire.pdf.common import *
from spire.pdf import *

# Create a PdfDocument instance
doc = PdfDocument()

# Load a PDF document
doc.LoadFromFile('C:/Users/Administrator/Desktop/input.pdf')

# Get the first page
page = doc.Pages[0]

# Load an image
image = PdfImage.FromFile('C:/Users/Administrator/Desktop/newImage.png')

# Create a PdfImageHelper instance
imageHelper = PdfImageHelper()
            
# Get the image information from the page
imageInfo = imageHelper.GetImagesInfo(page)

# Replace the first image on the page with the loaded image
imageHelper.ReplaceImage(imageInfo[0], image)

# Save the result document
doc.SaveToFile("output/ReplaceImage.pdf", FileFormat.PDF)

Python: Add, Replace, or Remove Images in a PDF Document

Remove an Image from a PDF Document in Python

To remove a specific image from a page, use the PdfPageBase.DeleteImage(index) method. The following are the steps.

  • Create a PdfDocument object.
  • Load a PDF document using PdfDocument.LoadFromFile() method.
  • Get a specific page through PdfDocument.Pages[index] property.
  • Delete a certain image in the page by its index using PdfPageBase.DeleteImage() method.
  • Save the document using PdfDocument.SaveToFile() method.
  • Python
from spire.pdf.common import *
from spire.pdf import *

# Create a PdfDocument instance
doc = PdfDocument()

# Load a PDF document
doc.LoadFromFile('C:/Users/Administrator/Desktop/input.pdf')

# Get the first page
page = doc.Pages[0]

# Delete the first image on the page
page.DeleteImage(0)

# Save the result document
doc.SaveToFile('output/DeleteImage.pdf', FileFormat.PDF)

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