本文将介绍如何使用spire.pdf for java在java程序中为pdf文档中的图片设置透明度:
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) {
//创建pdfdocument对象
pdfdocument doc = new pdfdocument();
pdfsection section = doc.getsections().add();
// 加载图片
pdfimage image = pdfimage.fromfile("logo.png");
double imagewidth = image.getphysicaldimension().getwidth() / 3;
double imageheight = image.getphysicaldimension().getheight() / 3;
//添加一页
pdfpagebase page = section.getpages().add();
float pagewidth = (float) page.getcanvas().getclientsize().getwidth();
float y = 10;
//标题文字
y = y 5;
pdfbrush brush = new pdfsolidbrush(new pdfrgbcolor(new color(255,69,0)));
pdftruetypefont font = new pdftruetypefont(new font("arial unicode ms", font.bold,12));
pdfstringformat format = new pdfstringformat(pdftextalignment.center);
string text = string.format("设置图片透明度");
page.getcanvas().drawstring(text, font, brush, pagewidth / 2, y, format);
dimension2d size = font.measurestring(text, format);
y = y (float) size.getheight() 6;
//设置图片透明度
page.getcanvas().settransparency(0.2f, 0.2f, pdfblendmode.normal);
page.getcanvas().drawimage(image, 0, y, imagewidth, imageheight);
page.getcanvas().save();
// 保存文档
doc.savetofile("output/transparency.pdf");
doc.close();
}
}
设置图片透明度的效果图: