将Itext中的背景图像更改为水印或更改不透明度c#ASP.net
我想使iText中的背景图像透明 以下是我的图像代码:将Itext中的背景图像更改为水印或更改不透明度c#ASP.net,c#,asp.net,itextsharp,C#,Asp.net,Itextsharp,我想使iText中的背景图像透明 以下是我的图像代码: string root = Server.MapPath("~"); string parent = Path.GetDirectoryName(root); string grandParent = Path.GetDirectoryName(parent); string imageFilePath = parent + "/Images/logo.png"; iTextSharp.text.Im
string root = Server.MapPath("~");
string parent = Path.GetDirectoryName(root);
string grandParent = Path.GetDirectoryName(parent);
string imageFilePath = parent + "/Images/logo.png";
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
jpg.ScaleToFit(1700, 800);
jpg.Alignment = iTextSharp.text.Image.UNDERLYING;
jpg.SetAbsolutePosition(100, 250);
jpg.ScaleAbsoluteHeight(500);
jpg.ScaleAbsoluteWidth(500);
有什么想法吗?请看一看这个例子。这是这个例子的一个变体
在代码中,您正在将图像
添加到文档
实例中。这没关系,但如果你想让这样的图像透明,你需要引入一个软遮罩。这并不困难,但有一种更简单的方法可以使背景透明:将图像添加到直接内容中,并引入PdfGState
定义不透明度:
PdfContentByte canvas = writer.getDirectContentUnder();
Image image = Image.getInstance(IMAGE);
image.SetAbsolutePosition(0, 0);
canvas.SaveState();
PdfGState state = new PdfGState();
state.setFillOpacity(0.6f);
canvas.setGState(state);
canvas.addImage(image);
canvas.restoreState();
比较一下,看看有什么不同
我的示例是用Java编写的,但很容易将其移植到C#:
PdfContentByte canvas = writer.DirectContentUnder;
Image image = Image.GetInstance(IMAGE);
image.SetAbsolutePosition(0, 0);
canvas.SaveState();
PdfGState state = new PdfGState();
state.FillOpacity = 0.6f;
canvas.SetGState(state);
canvas.AddImage(image);
canvas.RestoreState();