2012-02-27

Watermark your reports with FoxyPreviewer

Adding watermarks to reports is something that I've already blogged years ago during the development of GdiPlusX. In those times, we needed to create a specific ReportListener to manage the report pages, and draw the watermarks. We needed another external library, GdiPlusX, that was responsible for drawing at the report surface, and for applying some special effects to the images, such as transparencies and converting to grey scale.

FInally, we have this feature in FoxyPreviewer too ! That means that from now on, you can add watermarks to your reports without having absolutely any knowledge about GDI+ or ReportListeners, just by setting some few properties! In fact, by filling just one property you can have watermarks in all pages of your reports. In fact, this feature was asked by many people, but I had to postpone it because I had to implement to the original Gdi+ classes some new classes and functions, to create the transparencies and greyscale effects. If you're curious, and want to check for these functions, have a look at the file pr_gdiplushelper.prg in the sources folder.

The usage is, as always, super simple. The only requirement is that you use FoxyPreviewer in the simplified mode.

Related properties:

  • prop.gif cWatermarkImage - character, the name of the WM image. Important: this image MUST be stored locally, CAN'T be embedded in your EXE, obligatory property if you want to show the watermark. To remove the watermark from the next reports, just store an empty value to this property.

  • prop.gif nWatermarkType - numeric, 1 = Colored (default), 2 = Converts the source image to grey scale, optional property

  • prop.gif nWatermarkTransparency - numeric, from .10 to 1, default = 0.25, the transparency level of the WM image; 1 = Opaque, optional property

  • prop.gif nWaterMarkWidthRatio - numeric, default = 0.75, the width size in proportion of the watermark in the report page, optional property

  • prop.gif nWaterMarkHeightRatio  - numeric, default = 0.75, the height size in proportion of the watermark in the report page, optional property


 

Usage:


DO FoxyPreviewer.App
* Adding watermarks to reports
_Screen.oFoxyPreviewer.cWatermarkImage = ADDBS(HOME()) + "Graphics\Gifs\Morphfox.gif"
_Screen.oFoxyPreviewer.nWaterMarkType = 1 && 1 = Colored (default), 2 = B&W

_Screen.oFoxyPreviewer.nWatermarktransparency = .30 && (0-1) Transparency, 0 = Transparent, 1 = Opaque
_Screen.oFoxyPreviewer.nWaterMarkWidthRatio = .75 && (0-1) Proportion that the WM will occupy in page width
_Screen.oFoxyPreviewer.nWaterMarkHeightRatio = .75 && (0-1) Proportion that the WM will occupy in page height

REPORT FORM LOCFILE(_Samples + "\Solution\Reports\Colors.frx") PREVIEW


If you only set the "cWatermarkImage" property to a valid image file existing on the disk, you'll have your first watermark.
To reset your reports to the original settings, without the watermark, just clear the value of the property '.cWatermarkImage = "" '.

 

Samples:

Below you can see how the watermarks will appear, depending on the property values that you choose:

 

1 -
.nWatermarkType         = 1 && 1 = colored ; 2 = greyscale
.nWatermarkTransparency = 1 && 0 = transparent ; 1 = opaque
.nWatermarkWidthRatio = 0.90 && 0 - 1
.nWatermarkHeightRatio = 0.90 && 0 - 1


 

2 -
.nWatermarkType         = 1 && 1 = colored ; 2 = greyscale
.nWatermarkTransparency = 0.25 && 0 = transparent ; 1 = opaque
.nWatermarkWidthRatio = 0.50 && 0 - 1
.nWatermarkHeightRatio = 0.50 && 0 - 1


 

3 -
.nWatermarkType         = 2 && 1 = colored ; 2 = greyscale
.nWatermarkTransparency = 0.10 && 0 = transparent ; 1 = opaque
.nWatermarkWidthRatio = 1.00 && 0 - 1
.nWatermarkHeightRatio = 1.00 && 0 - 1


 

 

4 -
.nWatermarkType         = 2 && 1 = colored ; 2 = greyscale
.nWatermarkTransparency = 0.25 && 0 = transparent ; 1 = opaque
.nWatermarkWidthRatio = 0.75 && 0 - 1
.nWatermarkHeightRatio = 0.75 && 0 - 1



 

 

19 comments:

  1. Cesar you are inspiring me to use Foxypreview! :)
    Great Effort.

    ReplyDelete
  2. Great work Cesar
    thanks for sharing

    ReplyDelete
  3. Great post,
    enjoyed to read it. Thanks for sharing.

    Tip: include a hyperlink in the main Documentation document (http://foxypreviewer.codeplex.com/documentation ) to this post - so we have everything together.

    Ciao,
    Koen

    ReplyDelete
  4. Hi Koen,
    Yes! That's exactly what I'm doing, and the reason for these last blog posts is to provide complimentary documentation to Foxypreviewer. The hyperlink you suggested is at the section "Special features" in the main docs page.

    ReplyDelete
  5. Where is the download link? Thanks.

    ReplyDelete
  6. Hi Greg,
    Here it is:
    http://foxypreviewer.codeplex.com/releases/

    Regards

    Cesar

    ReplyDelete
  7. 1E6 thank you for this great feature! Only 1 issue so far...the watermark is being displayed in the first page without transparency...from second page on everything looks good.

    Thanks again

    Jorge

    ReplyDelete
  8. Thanks Jorge !
    Please provide more details. How are you calling your report ?
    What kind of image? What's the vaule of the properties you are setting?

    ReplyDelete
  9. 1. I'm using FoxyPreviewer297c

    2. In simplified mode:

    DO FOXYPREVIEWER.APP
    WITH _Screen.oFoxyPreviewer
    *!* Previewer settings
    .nWindowState = 2
    .nDockType = 0
    .lSendToEmail = .F.
    .lSaveToFile = .T.
    .lShowCopies = .T.
    .lShowMiniatures = .T.
    .lShowSetup = .F.
    .lShowPrinters = .T.
    .lShowSearch = .T.
    .nCopies = 1
    .lPrintVisible = .T.
    .lPrinterPref = .T.
    .nPrinterPropType = 1
    .nCanvasCount = 1
    .cLanguage = IIF(TYPE([pcLang]) = [L], [ENGLISH], pcLang)
    .cSaveDefName = IIF(TYPE([pcOutputFileName]) = [L], [], pcOutputFileName)
    *!* Previewer settings

    *!* new watermark
    .cWatermarkImage = IIF(TYPE([pcWaterMark]) = [L], [], pcWaterMark)
    .nWaterMarkType = 1 && 1 = Colored (default), 2 = B&W
    .nWatermarktransparency = .50 && (0-1) Transparency, 0 = Transparent, 1 = Opaque
    .nWaterMarkWidthRatio = 1 && (0-1) Proportion that the WM will occupy in page width
    .nWaterMarkHeightRatio = 1 &&
    *!* new watermark

    REPORT FORM (pcRepKey) PREVIEW

    *!* other not relevant stuff here
    ENDWITH

    3. The Image is a PNG file but, I have tried with a jpeg one, and I got the same behavior.

    4. I don't know if this is important but our reports are embedded on the EXE

    If you need more details or even one of the images, just let me know.

    Gracias!

    ReplyDelete
  10. Hi Cesar

    After some tests I found what is causing the issue in our side. All our reports are using the report variable _pagetotal inside the page footer. If we remove it, the watermark is displaying with the correct transparency, in all the pages. Hope this can help to find the real cause. As I told you before, our reports are embedded in an "APP"

    ReplyDelete
  11. I made a few changes for now; wherever we process watermark stuff (pr_reportlistener.foxylistener BEFOREREPORT, INIT, RENDER, DESTROY) I enclosed it as below:

    IF THIS.CurrentPass = 1 OR !THIS.TwoPassProcess && If we have two passes (using _pagetotal in the report for example) we do watermark stuff only at the second pass
    *!* watermark stuff
    ENDIF

    and it fixed the issue.

    Thanks

    ReplyDelete
  12. Hi Cesar
    My name is Marco and i have a problem with watermark in vers. 298b:

    if i print directly a pdf file without preview the watermark is not in PDF file; my command is
    REPORT FORM ;
    ("test") ;
    OBJECT TYPE 10 ; && OBJTYPE 10 = PDF , 11 = PDF AS IMAGE
    TO FILE "D:\Test.pdf" ; && Destination
    PREVIEW && Open the default PDF viewer

    if i save from preview is perfect.

    great work Cesar.

    ReplyDelete
  13. @jorge i would suggest dont use [] instead of ' or "
    eg. .cLanguage = IIF(TYPE([pcLang]) = [L], [ENGLISH], pcLang)

    when you come across #define you'll fail so that use ' or " for strings !

    ReplyDelete
  14. hi for all
    good for app
    u very very top
    app u need all user in vfp
    bye king

    ReplyDelete
  15. Hi, Marco, I have the same problem, you resolved the problem?

    ReplyDelete
  16. Foxpreviewer Just keeps getting better everyday!! thank you Cesar, Great Job

    ReplyDelete
  17. Thanks for the kind words Micheal !

    ReplyDelete