PNG watermarks lose transparency

  • If I specify to use a PNG file that has transparency for a watermark, it looks fine in WPTools Print Preview but the transparency parts of the PNG watermark come out as black when exported to a PDF. Is this expected or am I missing something. I even tried enabling the Export mode to wpAllowTransparentBit. Using WPDF4 and is defined in WPINC.INC.

  • For a test, I added the same PNG image to the body as well as a watermark. In print preview, both were transparent. However, in the PDF, only the body was transparent, the watermark was not.

    I examined the PDF and saw that the only difference in how the PDF "described" the image stream was that the transparent one had "/SMask 6 0 R" as part of the properties but the watermark did not. I edited the PDF and added "/SMask 6 0 R" to the watermark image properties, and then it was transparent.

    I use StretchDraw in the PaintWatermark even when outputting the watermark, but I haven't been able to trace down to how you output a PNG image that is in the body when exporting to PDF so that I could mimic how you do it to support transparency in the body (my customers don't necessarily know how to convert their logo to a non-transparent bitmap with white replacing transparency, so I would like to support it for watermarks if possible). I see you have a printmode option of wppInPaintForwPDF that I could conditionally chose one method over the other.

    Any help you can provide would be great.

    • Offizieller Beitrag

    If you use StretchDraw to paint a PDF wPDF cannot know that it is transparent, it is just a bitmap. The PNG which are printed by wptools are sent to the engine including their mask however.

    You can add your PNG to the header as page dependent image.

  • I was able to finally trace it down. It works if I copy the png graphic to a TMemoryStream and then call WPWriteComment something like this in the PaintWatermark event:

    Code
    WPWriteComment(pCanvas.Handle, WPDFCOMM_DRAWPNGDATA, pPageRect, vStream.Memory, vStream.Size)

    I can also use this approach to paint the watermark as an overlay instead of as a true watermark. This allows me to be able to create fancy cover pages that masks out everything except what is in the transparent area, such as a for framing a picture of something in any frame shape. Looks a bit weird (low resolution for the parts in the transparent area) when in Print Preview, but comes out fine in the PDF. Print Preview is using StretchDraw, so maybe something needs tweaking there on my part.