This article demonstrates how to set the transparency of image on a PDF file using Spire.PDF for Java.
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import java.awt.geom.*;
import java.awt.*;
public class ImageTransparency {
public static void main(String[] args) {
//Create a PDFDocument
PdfDocument doc = new PdfDocument();
//Add a section
PdfSection section = doc.getSections().add();
// Load image and set its width and height
PdfImage image = PdfImage.fromFile("logo.png");
double imageWidth = image.getPhysicalDimension().getWidth() / 3;
double imageHeight = image.getPhysicalDimension().getHeight() / 3;
//Add a page
PdfPageBase page = section.getPages().add();
float pageWidth = (float) page.getCanvas().getClientSize().getWidth();
float y = 10;
//Draw Title and set the font and format
y = y + 5;
PdfBrush brush = new PdfSolidBrush(new PdfRGBColor(new Color(255,69,0)));
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.BOLD,12));
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center);
String text = String.format(" Set image Transparency ");
page.getCanvas().drawString(text, font, brush, pageWidth / 2, y, format);
Dimension2D size = font.measureString(text, format);
y = y + (float) size.getHeight() + 6;
//Set image transparency
page.getCanvas().setTransparency(0.2f, 0.2f, PdfBlendMode.Normal);
//Add image to the page
page.getCanvas().drawImage(image, 0, y, imageWidth, imageHeight);
page.getCanvas().save();
// Save pdf file.
doc.saveToFile("output/Transparency.pdf");
// Close pdf file
doc.close();
}
}
Effective screenshot after adding the image to PDF and set the image transparency:

