2007-07-17

Watermark your reports with GdiPlusX

here's a reportlistener that generates watermark pictures in reports.


very simple to setup, just set some properties, like the logo image, width and height ratio, transparency ratio.


 


do locfile("system.app")


local lolistener as reportlistener
lolistener = createobject
("watermarklistener")
lolistener.
listenertype
= 1
lolistener.watermarkimage =
addbs(home
()) + "graphics\gifs\morphfox.gif"
lolistener.watermarktype = 2
&& 1 = colored ; 2 = greyscale
lolistener.watermarktransparency = 0.25
&& 0 = transparent ; 1 = opaque
lolistener.watermarkwidthratio = 0.75
&& 0 - 1
lolistener.watermarkheightratio = 0.75
&& 0 - 1


* run the report using the new report engine (object-assisted output)
report form (addbs(home()) + "samples/solution/europa/employeesmd.frx") object
lolistener
return


 


below are some samples generated by the watermarklistener class. (source code at the end of this post)


 


important
requires vfp9 and gdiplusx to run. 
please make sure that you have the latest version!
http://www.codeplex.com/vfpx/wiki/view.aspx?title=gdiplusx&referringtitle=home

 


1 -


      watermarktype = 1 && 1 = colored ; 2 = greyscale
      watermarktransparency = 1 && 0 = transparent ; 1 = opaque
      watermarkwidthratio = 0.90 && 0 - 1
      watermarkheightratio = 0.90 && 0 - 1



 


2 -


      watermarktype = 1 && 1 = colored ; 2 = greyscale
      watermarktransparency = 0.25 && 0 = transparent ; 1 = opaque
      watermarkwidthratio = 0.50 && 0 - 1
      watermarkheightratio = 0.50 && 0 - 1





 


3 -


      watermarktype = 2 && 1 = colored ; 2 = greyscale
      watermarktransparency = 0.10 && 0 = transparent ; 1 = opaque
      watermarkwidthratio = 1 && 0 - 1
      watermarkheightratio = 1 && 0 - 1







 


4 -


      watermarktype = 2 && 1 = colored ; 2 = greyscale
      watermarktransparency = 25 && 0 = transparent ; 1 = opaque
      watermarkwidthratio = 0.75 && 0 - 1
      watermarkheightratio = 0.75 && 0 - 1




 


 


 


do locfile("system.app")


local lolistener as reportlistener
lolistener = createobject("watermarklistener")
lolistener.
listenertype = 1
lolistener.watermarkimage =
addbs(home()) + "graphics\gifs\morphfox.gif"
lolistener.watermarktype = 2
&& 1 = colored ; 2 = greyscale
lolistener.watermarktransparency = 0.25 && 0 = transparent ; 1 = opaque
lolistener.watermarkwidthratio = 0.75 && 0 - 1
lolistener.watermarkheightratio = 0.75 && 0 - 1


* run the report using the new report engine (object-assisted output)
report form home() + "samples/solution/europa/employeesmd.frx") object lolistener
return
 



define class
watermarklistener as _reportlistener of home() + "ffc\" + "_reportlistener.vcx"


newpage = .t.
ogdigraphics =
null
watermarkimage = ""
watermarktype = 1
&& 1 = colored ; 2 = greyscale
watermarktransparency = 0.50 && 0 = transparent ; 1 = opaque
watermarkwidthratio = 0.50
watermarkheightratio = 0.50

function beforereport
   dodefault
()
   this.ogdigraphics = _screen.system.drawing.graphics.new()
endfunc


function beforeband(nbandobjcode, nfrxrecno)
   #
define frx_objcod_pageheader 1
   if nbandobjcode==frx_objcod_pageheader
      this.newpage = .t.
      if not this.issuccessor
         this.sharedgdiplusgraphics = this.gdiplusgraphics
      endif
      this
.ogdigraphics.handle = this.sharedgdiplusgraphics
   endif
   dodefault
(nbandobjcode, nfrxrecno)
endfunc


procedure render(nfrxrecno,;
      nleft,ntop,nwidth,nheight,;
      nobjectcontinuationtype, ;
      ccontentstoberendered, gdiplusimage)


   with _screen.system.drawing
      if this.newpage
         local lnx, lny, lnwidth, lnheight
         lnx = (1 -
this.watermarkwidthratio) / 2
         lny = (1 -
this.watermarkheightratio) / 2
         lnwidth =
this.watermarkwidthratio
         lnheight =
this.watermarkheightratio


         * create a rectangle of the size of 60% of the report page
         local lorect as xfcrectangle
         lorect = .rectangle.new(lnx *
this.sharedpagewidth, ;
            lny *
this.sharedpageheight, ;
            this.sharedpagewidth * lnwidth, ;
            this.sharedpageheight * lnheight)


         * load the image file to gdi+
         local lobmp as xfcbitmap
         lobmp = .
bitmap.new(this.watermarkimage)
 
         local loclrmatrix as xfccolormatrix

         if this.watermarktype = 2 && 1 = colored ; 2 = greyscale
            loclrmatrix = .imaging.colormatrix.new( ; 
               .33, .33, .33, 0 , 0, ; 
               .33, .33, .33, 0 , 0, ; 
               .33, .33, .33, 0 , 0, ;
               0, 0, 0,
this.watermarktransparency, 0, ; 
               0, 0, 0, 0, 0)
         else
            loclrmatrix = .imaging.colormatrix.new()
            loclrmatrix.matrix33 =
this.watermarktransparency
         endif


         local loattr as xfcimageattributes
         loattr = .imaging.imageattributes.new() 
         loattr.setcolormatrix(loclrmatrix)


         this.ogdigraphics.drawimage(lobmp, lorect, lobmp.getbounds(), 2, loattr)


         this.newpage = .f.
      endif
   endwith


   dodefault(nfrxrecno,;
         nleft,ntop,nwidth,nheight,;
         nobjectcontinuationtype, ;
         ccontentstoberendered, gdiplusimage)
endproc


enddefine

8 comments:

  1. Great stuff, please keep on inventing.

    ReplyDelete
  2. Can we use this water mark technique in forms ?
    i wanted to use my company logo in semi transparent in middle of top level form.
    when i maximize form logo pic used appears in multiple times.

    thanks.
    Imran,
    Please have a look at my newer posts, there I show how to transform logos in transparent, I'm sure this will help you to get started.
    Hope this helps
    Cesar

    ReplyDelete
  3. Este articulo estra traducido al español en PortalFox en:


    -- Marcas de agua en tus informes con GdiPlusX --

    http://www.portalfox.com/article.php?sid=2481

    ReplyDelete
  4. This is a great utility - Thanks.  Can you help witha problem.  When printing the report with a Watermark, it only seem to print on every other page.  Can you tell me if there is anything else I should do.
    Hi Ken,
    I hope to publish a new version of this class soon. Please keep in touch, and tell me how it works !
    Thians in advance
    Cesar

    ReplyDelete
  5. Love this!

    Any news on the new version?


    What is the best way to include all this in an install ??

    ReplyDelete
  6. If I start watermark program from a .prg file I do not have any problem.
    If I start this prg file from menu every time I get an error.
    Where did I make a mistake?

    ReplyDelete
  7. Hi Sandor,
    If you don't tell me anything about the error you are having, it will be difficult to provide any help.

    ReplyDelete
  8. For example LOLISTENER IS NOT AN OBJECT.

    ReplyDelete