Python: Set the Transparency of PDF Images

2024-09-13 01:05:40 Written by  support iceblue
Rate this item
(0 votes)

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.