使用iTextSharp的PDF中的图像透明度 [英] Image transparency in PDF using iTextSharp

查看:727
本文介绍了使用iTextSharp的PDF中的图像透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个传入的jpg文件,可以将颜色设置为透明.当我将图像添加到另一幅图像时,这可以完美地工作.

I have an incoming jpg file, that I can set a colour to transparent. When I add the image to another image, this works perfectly.

我正在尝试使用iTextSharp将同一图像添加到PDF,但是我无法使透明性发挥作用.

I am trying to add the same image to a PDF using iTextSharp, but I cannot get the transparency to work.

我尝试了两种方法,但是都没有用.第一种方法是在位图中打开图像,设置透明度,然后在PDF中使用该位图对象.第二种方法(如下所示)是将位图保存到磁盘,然后将文件打开到iTextSharp图像中.

I have tried two ways, but neither is working. The first way was to open the image in a Bitmap, set transparency, then use that Bitmap object in the PDF. The second way (shown here) was saving the Bitmap to disk and opening the file into the iTextSharp image.

                    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);
                    }

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

I have seen that there is a Transparency option...

savedImage.Transparency = ???

但我不知道要在值中加上什么.我在搜索中找不到任何东西.

but I don't know what to put in the values. I can't find anything on my searches.

推荐答案

最终找到了答案.

我看到了这个……最初,我是在寻找.transparency来查找透明度设置的.我没看到 c#.NET CORE添加图像使用ITextSharp对现有PDF透明化

I saw this... and originally I was looking for .Transparency to find the transparency settings. I didn't see it. 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删除了布尔值.

Note that the contentByte.AddImage has the boolean removed.

这篇关于使用iTextSharp的PDF中的图像透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆