Print on Background of each Page |
Top Previous Next |
Sometimes you need a watermark, an image or just some text on some of the pages.
Although wPDF supports the PDF watermarks it is often a good idea to paint the information on each page. To do so you can use the OnBeforePrintPage event. This event is triggered before the text of one page is exported. You can either paint to the Canvas or use DrawTGraphic to export a metafile to the PDF file.
We modified the previous example to support watermarks. It is easy to modify the example to draw different graphics or notes like 'Confidential' on each page.
uses WPDefs, WPPrint, WpWinCtr, WPRich, WPPDFR1, WPPDFWP
procedure TForm1.RTF2PDF( RTFname, PDFname : string); var WPRichText: TWPRichText; WPPDFExport: TWPPDFExport; Watermark : TPicture; begin WPRichText:= TWPRichText.CreateParented(Application.Handle);; WPPDFExport:= TWPPDFExport.Create(nil); Watermark := TPicture.Create; WPPDFExport.Tag := Integer( Watermark ); WPPDFExport.Source := WPRichText; WPPDFExport.OnBeforePrintPage := DoBeforePrintPage; try WPRichText.LoadFromFile( RTFname ); Watermark.LoadFromFile( watermark_graphic ); WPPDFExport.FileName := PDFname; WPPDFExport.Print; finally WPRichText.Free; WPPDFExport.Free; end; end;
procedure TForm1.DoBeforePrintPage(Sender: TObject; Number, FromPos, Length: Integer); begin if TWPPDFExport(Sender).Tag<>0 then begin TWPPDFExport(Sender).DrawTGraphic(0,0, 0,0,(TObject(TWPPDFExport(Sender).Tag)as TPicture).Graphic); end; end;
If you want to export the background color of the TWPRichText use this code:
procedure TForm1.DoBeforePrintPage(Sender: TObject; Number, FromPos, Length: Integer); begin WPPDFExport1.Canvas.Brush.Color := WPPDFExport1.Source.Color; WPPDFExport1.Canvas.Brush.Style := bsSolid; WPPDFExport1.Canvas.Pen.Style := psClear; WPPDFExport1.Canvas.Rectangle(0,0, MulDiv(WPRichText1.Header.PageWidth,Screen.PixelsPerInch,1440), MulDiv(WPRichText1.Header.PageHeight,Screen.PixelsPerInch,1440) ); end;
If you want to export any background Image
You will need a variable BackImageNr : Integer; which is set to -1 before the PDF is exported. This variable is used to fist initialize the bitmap which is then only used as "clone".
The background image is expected in object 'Image2'.
procedure TForm1.DoBeforePrintPage(Sender: TObject; Number, FromPos, Length: Integer); var x,y : Integer; begin for x:=0 to (MulDiv(WPRichText1.Header.PageWidth,Screen.PixelsPerInch,1440)+ Image2.Picture.Bitmap.Width-1) div Image2.Picture.Bitmap.Width do for y:=0 to (MulDiv(WPRichText1.Header.PageHeight,Screen.PixelsPerInch,1440)+ Image2.Picture.Bitmap.Height-1) div Image2.Picture.Bitmap.Height do begin
if BackImageNr<=0 then BackImageNr := WPPDFExport1.DrawTGraphic( x * Image2.Picture.Bitmap.Width, y * Image2.Picture.Bitmap.Height, Image2.Picture.Bitmap.Width, Image2.Picture.Bitmap.Height, Image2.Picture.Bitmap) else WPPDFExport1.DrawBitmapClone( x * Image2.Picture.Bitmap.Width, y * Image2.Picture.Bitmap.Height, Image2.Picture.Bitmap.Width, Image2.Picture.Bitmap.Height, BackImageNr); end;
|