使用 iTextSharp 的 PDF 中的图像透明度

Image transparency in PDF using iTextSharp

我有一个传入的 jpg 文件,我可以将颜色设置为透明。当我将图像添加到另一个图像时,效果很好。

我正在尝试使用 iTextSharp 将相同的图像添加到 PDF,但我无法使透明度正常工作。

我尝试了两种方法,但都不起作用。第一种方法是在位图中打开图像,设置透明度,然后在 PDF 中使用该位图对象。第二种方法(此处显示)是将位图保存到磁盘并将文件打开到 iTextSharp 图像中。

                    using (Bitmap b = new Bitmap(Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("~/IncomingData/" + ImageFileName))))
                    {
                        b.MakeTransparent(Color.White);
                        b.Save(System.Web.HttpContext.Current.Server.MapPath("~/IncomingData/" + GuidFileName), System.Drawing.Imaging.ImageFormat.Png);
                        ImageFileName = GuidFileName;

                        iTextSharp.text.Image savedImage = iTextSharp.text.Image.GetInstance(Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("~/IncomingData/" + GuidFileName)), iTextSharp.text.Color.WHITE);

                        savedImage.SetAbsolutePosition(Convert.ToSingle(x + 1.0), Convert.ToSingle(imageY + 12) - Convert.ToSingle(h));
                        savedImage.ScaleToFit(Convert.ToSingle(w), Convert.ToSingle(h));
                        contentByte.AddImage(savedImage, true);
                    }

我看到有一个透明度选项...

savedImage.Transparency = ???

但我不知道要在值中输入什么。我在搜索中找不到任何内容。

终于找到答案了。

我看到了这个...最初我正在寻找 .Transparency 来找到透明度设置。我没看到。 c# .NET CORE adding image with transparency to existing PDF using ITextSharp

我的代码现在是...

                    using (Bitmap b = new Bitmap(Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("~/IncomingData/" + ImageFileName))))
                    {
                        b.MakeTransparent(Color.White);

                        iTextSharp.text.Image savedImage = iTextSharp.text.Image.GetInstance(b, System.Drawing.Imaging.ImageFormat.Png);

                        savedImage.SetAbsolutePosition(Convert.ToSingle(x + 1.0), Convert.ToSingle(imageY + 12) - Convert.ToSingle(h));
                        savedImage.ScaleToFit(Convert.ToSingle(w), Convert.ToSingle(h));

                        contentByte.AddImage(savedImage);
                    }

请注意 contentByte.AddImage 删除了布尔值。