1

I am trying to stamp existing pdf document using ITextSharp's stamper. I am able to open existing pdf and put an image inside on the desired position. (stamp the pdf)

Probleme is that stamp (red image) is always under the drawing. (black lines are over the red image) I am not able to do it vice versa.

My result: enter image description here

The desired result is exactly the opposite - red image over the black lines

Any idea how to accomplish this properly? Thx for any advice.

Here is my code:

        using (Stream inputPdfStream = new FileStream(@"D:\tmp\go\input.pdf", FileMode.Open, FileAccess.Read, FileShare.Read))
        using (Stream outputPdfStream = new FileStream(@"D:\tmp\go\output.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
        using (Stream inputImageStream = new FileStream(@"D:\tmp\go\stamp.png", FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            var reader = new PdfReader(inputPdfStream);
            var stamper = new PdfStamper(reader, outputPdfStream);

            int lastPage = reader.NumberOfPages;
            Image image = Image.GetInstance(inputImageStream);
            image.ScalePercent(35.5f);
            image.SetAbsolutePosition(30, 30);

            PdfGState graphicsState = new PdfGState();
            graphicsState.BlendMode = PdfGState.BM_DARKEN;

            var pdfContentByte = stamper.GetOverContent(lastPage);

            pdfContentByte.SetGState(graphicsState);
            pdfContentByte.SaveState();
            pdfContentByte.AddImage(image);

            stamper.Close();
        }
fxdx
  • 399
  • 1
  • 3
  • 9
  • 1
    use different blend mode ... *bm=darken Compares the color value of each overlapping pixel from the two layers, and keeps the darker value.* **guess what ... black is darker** – Selvin Feb 01 '19 at 16:11
  • 1
    I assume your image has a white background and by means of that blend mode you want to make that white background purely transparent. As @Selvin explained, though, *all of the image* is subject to the blend mode, in particular also the red parts. If you want white to be transparent but red not, you should employ color coded transparency or (more flexible) a mask or soft mask. – mkl Feb 02 '19 at 07:43
  • @Selvin thx for the hint, I didn't consider changing bm at all. Your advice might help somebody else, sadly in my case, none of the blend modes fulfills my requirement. I ended up with changing the stamp so that it fits into a rectangle without overlapping its borders. @ mkl yes - exactly as you wrote, I gave up further tinkering (with transparency and masks) as I have to invest my efforts to other stuff. Thx both of you – fxdx Feb 04 '19 at 15:35

0 Answers0