FastReport
367 строк · 11.9 Кб
1using System;
2using System.ComponentModel;
3using System.Drawing;
4using FastReport.Utils;
5using System.Windows.Forms;
6using System.Drawing.Design;
7
8namespace FastReport
9{
10/// <summary>
11/// Specifies the watermark image size mode.
12/// </summary>
13public enum WatermarkImageSize
14{
15/// <summary>
16/// Specifies the normal (original) size.
17/// </summary>
18Normal,
19
20/// <summary>
21/// Specifies the centered image.
22/// </summary>
23Center,
24
25/// <summary>
26/// Specifies the stretched image.
27/// </summary>
28Stretch,
29
30/// <summary>
31/// Specifies the stretched image that keeps its aspect ratio.
32/// </summary>
33Zoom,
34
35/// <summary>
36/// Specifies the tiled image.
37/// </summary>
38Tile
39}
40
41/// <summary>
42/// Specifies the watermark text rotation.
43/// </summary>
44public enum WatermarkTextRotation
45{
46/// <summary>
47/// Specifies a horizontal text.
48/// </summary>
49Horizontal,
50
51/// <summary>
52/// Specifies a vertical text.
53/// </summary>
54Vertical,
55
56/// <summary>
57/// Specifies a diagonal text.
58/// </summary>
59ForwardDiagonal,
60
61/// <summary>
62/// Specifies a backward diagonal text.
63/// </summary>
64BackwardDiagonal
65}
66
67/// <summary>
68/// Represents the report page watermark.
69/// </summary>
70/// <remarks>
71/// Watermark can draw text and/or image behind the page objects on in front of them. To enable
72/// watermark, set its <b>Enabled</b> property to <b>true</b>.
73/// </remarks>
74[TypeConverter(typeof(FastReport.TypeConverters.FRExpandableObjectConverter))]
75[EditorAttribute("FastReport.TypeEditors.WatermarkEditor, FastReport", typeof(UITypeEditor))]
76public class Watermark : IDisposable
77{
78#region Fields
79private bool enabled;
80private PictureObject pictureObject;
81private TextObject textObject;
82private WatermarkTextRotation textRotation;
83private WatermarkImageSize imageSize;
84private bool showImageOnTop;
85private bool showTextOnTop;
86#endregion
87
88#region Properties
89/// <summary>
90/// Gets or sets avalue indicating that watermark is enabled.
91/// </summary>
92[DefaultValue(false)]
93public bool Enabled
94{
95get { return enabled; }
96set { enabled = value; }
97}
98
99/// <summary>
100/// Gets or sets the watermark image.
101/// </summary>
102public Image Image
103{
104get { return pictureObject.Image; }
105set { pictureObject.Image = value; }
106}
107
108/// <summary>
109/// Gets or sets the watermark image size mode.
110/// </summary>
111[DefaultValue(WatermarkImageSize.Zoom)]
112public WatermarkImageSize ImageSize
113{
114get { return imageSize; }
115set { imageSize = value; }
116}
117
118/// <summary>
119/// Gets or sets an image transparency.
120/// </summary>
121/// <remarks>
122/// Valid values are 0..1. 1 means totally transparent image.
123/// </remarks>
124[DefaultValue(0f)]
125public float ImageTransparency
126{
127get { return pictureObject.Transparency; }
128set { pictureObject.Transparency = value; }
129}
130
131/// <summary>
132/// Gets or sets the watermark text.
133/// </summary>
134public string Text
135{
136get { return textObject.Text; }
137set { textObject.Text = value; }
138}
139
140/// <summary>
141/// Gets or sets a font of the watermark text.
142/// </summary>
143public Font Font
144{
145get { return textObject.Font; }
146set { textObject.Font = value; }
147}
148
149/// <summary>
150/// Gets or sets a text fill.
151/// </summary>
152[Editor("FastReport.TypeEditors.FillEditor, FastReport", typeof(UITypeEditor))]
153public FillBase TextFill
154{
155get { return textObject.TextFill; }
156set { textObject.TextFill = value; }
157}
158
159/// <summary>
160/// Gets or sets a text rotation.
161/// </summary>
162[DefaultValue(WatermarkTextRotation.ForwardDiagonal)]
163public WatermarkTextRotation TextRotation
164{
165get { return textRotation; }
166set { textRotation = value; }
167}
168
169/// <summary>
170/// Gets or sets a value indicates that the text should be displayed on top of all page objects.
171/// </summary>
172[DefaultValue(true)]
173public bool ShowTextOnTop
174{
175get { return showTextOnTop; }
176set { showTextOnTop = value; }
177}
178
179/// <summary>
180/// Gets or sets a value indicates that the image should be displayed on top of all page objects.
181/// </summary>
182[DefaultValue(false)]
183public bool ShowImageOnTop
184{
185get { return showImageOnTop; }
186set { showImageOnTop = value; }
187}
188
189internal TextObject TextObject
190{
191get { return textObject; }
192}
193
194/// <summary>
195///
196/// </summary>
197public PictureObject PictureObject
198{
199get { return pictureObject; }
200set { pictureObject = value; }
201}
202#endregion
203
204#region Private Methods
205//private bool ShouldSerializeFont()
206//{
207// return Font.Name != DrawUtils.DefaultReportFont.Name || Font.Size != 60 || Font.Style != FontStyle.Regular;
208//}
209
210private bool ShouldSerializeTextFill()
211{
212return !(TextFill is SolidFill) || (TextFill as SolidFill).Color != Color.LightGray;
213}
214
215private bool ShouldSerializeImage()
216{
217return Image != null;
218}
219#endregion
220
221#region Public Methods
222/// <summary>
223/// Draws watermark image.
224/// </summary>
225/// <param name="e"></param>
226/// <param name="displayRect"></param>
227/// <param name="report"></param>
228/// <param name="isPrinting"></param>
229public virtual void DrawImage(FRPaintEventArgs e, RectangleF displayRect, Report report, bool isPrinting)
230{
231pictureObject.SetReport(report);
232pictureObject.Bounds = displayRect;
233PictureBoxSizeMode sizeMode = PictureBoxSizeMode.Normal;
234if (ImageSize == WatermarkImageSize.Stretch)
235sizeMode = PictureBoxSizeMode.StretchImage;
236else if (ImageSize == WatermarkImageSize.Zoom)
237sizeMode = PictureBoxSizeMode.Zoom;
238else if (ImageSize == WatermarkImageSize.Center)
239sizeMode = PictureBoxSizeMode.CenterImage;
240pictureObject.SizeMode = sizeMode;
241pictureObject.Tile = ImageSize == WatermarkImageSize.Tile;
242pictureObject.SetPrinting(isPrinting);
243pictureObject.DrawImage(e);
244}
245
246/// <summary>
247/// Draws watermark text.
248/// </summary>
249/// <param name="e"></param>
250/// <param name="displayRect"></param>
251/// <param name="report"></param>
252/// <param name="isPrinting"></param>
253public void DrawText(FRPaintEventArgs e, RectangleF displayRect, Report report, bool isPrinting)
254{
255textObject.SetReport(report);
256textObject.Bounds = displayRect;
257int angle = 0;
258switch (TextRotation)
259{
260case WatermarkTextRotation.Horizontal:
261angle = 0;
262break;
263case WatermarkTextRotation.Vertical:
264angle = 270;
265break;
266case WatermarkTextRotation.ForwardDiagonal:
267angle = 360 - (int)(Math.Atan(displayRect.Height / displayRect.Width) * (180 / Math.PI));
268break;
269case WatermarkTextRotation.BackwardDiagonal:
270angle = (int)(Math.Atan(displayRect.Height / displayRect.Width) * (180 / Math.PI));
271break;
272}
273textObject.Angle = angle;
274textObject.SetPrinting(isPrinting);
275textObject.DrawText(e);
276}
277
278/// <summary>
279/// Serializes the watermark.
280/// </summary>
281/// <param name="writer">Writer object.</param>
282/// <param name="prefix">The watermark property name.</param>
283/// <param name="c">Another Watermark object to compare with.</param>
284/// <remarks>
285/// This method is for internal use only.
286/// </remarks>
287public void Serialize(FRWriter writer, string prefix, Watermark c)
288{
289if (Enabled != c.Enabled)
290writer.WriteBool(prefix + ".Enabled", Enabled);
291if (!writer.AreEqual(Image, c.Image))
292writer.WriteValue(prefix + ".Image", Image);
293if (ImageSize != c.ImageSize)
294writer.WriteValue(prefix + ".ImageSize", ImageSize);
295if (ImageTransparency != c.ImageTransparency)
296writer.WriteFloat(prefix + ".ImageTransparency", ImageTransparency);
297if (Text != c.Text)
298writer.WriteStr(prefix + ".Text", Text);
299if ((writer.SerializeTo != SerializeTo.Preview || !writer.AreEqual(Font, c.Font)) && writer.ItemName != "inherited")
300writer.WriteValue(prefix + ".Font", Font);
301TextFill.Serialize(writer, prefix + ".TextFill", c.TextFill);
302if (TextRotation != c.TextRotation)
303writer.WriteValue(prefix + ".TextRotation", TextRotation);
304if (ShowTextOnTop != c.ShowTextOnTop)
305writer.WriteBool(prefix + ".ShowTextOnTop", ShowTextOnTop);
306if (ShowImageOnTop != c.ShowImageOnTop)
307writer.WriteBool(prefix + ".ShowImageOnTop", ShowImageOnTop);
308}
309
310/// <summary>
311/// Disposes resources used by the watermark.
312/// </summary>
313public void Dispose()
314{
315pictureObject.Dispose();
316textObject.Dispose();
317}
318
319/// <summary>
320/// Assigns values from another source.
321/// </summary>
322/// <param name="source">Source to assign from.</param>
323public void Assign(Watermark source)
324{
325Enabled = source.Enabled;
326Image = source.Image == null ? null : source.Image.Clone() as Image;
327ImageSize = source.ImageSize;
328ImageTransparency = source.ImageTransparency;
329Text = source.Text;
330Font = source.Font;
331TextFill = source.TextFill.Clone();
332TextRotation = source.TextRotation;
333ShowTextOnTop = source.ShowTextOnTop;
334ShowImageOnTop = source.ShowImageOnTop;
335}
336
337/// <summary>
338/// Creates exact copy of this <b>Watermark</b>.
339/// </summary>
340/// <returns>Copy of this watermark.</returns>
341public Watermark Clone()
342{
343Watermark result = new Watermark();
344result.Assign(this);
345return result;
346}
347#endregion
348
349/// <summary>
350/// Initializes a new instance of the <see cref="Watermark"/> class with default settings.
351/// </summary>
352public Watermark()
353{
354pictureObject = new PictureObject();
355textObject = new TextObject();
356
357pictureObject.ShowErrorImage = false;
358textObject.HorzAlign = HorzAlign.Center;
359textObject.VertAlign = VertAlign.Center;
360ImageSize = WatermarkImageSize.Zoom;
361Font = new Font(DrawUtils.DefaultReportFont.Name, 60);
362TextFill = new SolidFill(Color.FromArgb(40, Color.Gray));
363TextRotation = WatermarkTextRotation.ForwardDiagonal;
364ShowTextOnTop = true;
365}
366}
367}
368