2
votes

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.

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

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.

1
Something here does not make sense because JPEG does not support transparency. How do you “add the image to another image”? - Dour High Arch
JPEG do not support transparency, but give it a go. It will become transparent, which you can then save out as .png. That is what MakeTransparent does. In this question, I am not adding to another image, I am adding to a PDF. To add the transparent "jpg" to another image... Bitmap b = (Bitmap)Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("~/IncomingData/" + GuidFileName)); b.MakeTransparent(Color.White); graphics.DrawImage(b, x, y - 10, newWidth, newHeight); - David
how did you check whether either of your two attempts worked or not? - mkl
By running it and checking the output files... also by hitting my breakpoints to ensure it actually ran the code. Also, as I am creating a transparent .png file (in the code above), I have the file to hand and the png is definitely transparent. It is just not transparent when put onto the PDF. - David
FYI. I changed this... iTextSharp.text.Image savedImage = iTextSharp.text.Image.GetInstance(Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("~/IncomingData/" + GuidFileName)), iTextSharp.text.Color.WHITE); to this... iTextSharp.text.Image savedImage = iTextSharp.text.Image.GetInstance(System.Web.HttpContext.Current.Server.MapPath("~/IncomingData/" + GuidFileName)); so that it is taking direct from the file instead of loading into an image object first. Now, my PDF is showing a BLACK background where it should be transparent. - David

1 Answers

3
votes

Found the answer, eventually.

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

My code is now...

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

Note that the contentByte.AddImage has the boolean removed.