FastReport

Форк
0
367 строк · 11.9 Кб
1
using System;
2
using System.ComponentModel;
3
using System.Drawing;
4
using FastReport.Utils;
5
using System.Windows.Forms;
6
using System.Drawing.Design;
7

8
namespace FastReport
9
{
10
    /// <summary>
11
    /// Specifies the watermark image size mode.
12
    /// </summary>
13
    public enum WatermarkImageSize
14
    {
15
        /// <summary>
16
        /// Specifies the normal (original) size.
17
        /// </summary>
18
        Normal,
19

20
        /// <summary>
21
        /// Specifies the centered image.
22
        /// </summary>
23
        Center,
24

25
        /// <summary>
26
        /// Specifies the stretched image.
27
        /// </summary>
28
        Stretch,
29

30
        /// <summary>
31
        /// Specifies the stretched image that keeps its aspect ratio.
32
        /// </summary>
33
        Zoom,
34

35
        /// <summary>
36
        /// Specifies the tiled image.
37
        /// </summary>
38
        Tile
39
    }
40

41
    /// <summary>
42
    /// Specifies the watermark text rotation.
43
    /// </summary>
44
    public enum WatermarkTextRotation
45
    {
46
        /// <summary>
47
        /// Specifies a horizontal text.
48
        /// </summary>
49
        Horizontal,
50

51
        /// <summary>
52
        /// Specifies a vertical text.
53
        /// </summary>
54
        Vertical,
55

56
        /// <summary>
57
        /// Specifies a diagonal text.
58
        /// </summary>
59
        ForwardDiagonal,
60

61
        /// <summary>
62
        /// Specifies a backward diagonal text.
63
        /// </summary>
64
        BackwardDiagonal
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))]
76
    public class Watermark : IDisposable
77
    {
78
        #region Fields
79
        private bool enabled;
80
        private PictureObject pictureObject;
81
        private TextObject textObject;
82
        private WatermarkTextRotation textRotation;
83
        private WatermarkImageSize imageSize;
84
        private bool showImageOnTop;
85
        private 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)]
93
        public bool Enabled
94
        {
95
            get { return enabled; }
96
            set { enabled = value; }
97
        }
98

99
        /// <summary>
100
        /// Gets or sets the watermark image.
101
        /// </summary>
102
        public Image Image
103
        {
104
            get { return pictureObject.Image; }
105
            set { pictureObject.Image = value; }
106
        }
107

108
        /// <summary>
109
        /// Gets or sets the watermark image size mode.
110
        /// </summary>
111
        [DefaultValue(WatermarkImageSize.Zoom)]
112
        public WatermarkImageSize ImageSize
113
        {
114
            get { return imageSize; }
115
            set { 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)]
125
        public float ImageTransparency
126
        {
127
            get { return pictureObject.Transparency; }
128
            set { pictureObject.Transparency = value; }
129
        }
130

131
        /// <summary>
132
        /// Gets or sets the watermark text.
133
        /// </summary>
134
        public string Text
135
        {
136
            get { return textObject.Text; }
137
            set { textObject.Text = value; }
138
        }
139

140
        /// <summary>
141
        /// Gets or sets a font of the watermark text.
142
        /// </summary>
143
        public Font Font
144
        {
145
            get { return textObject.Font; }
146
            set { textObject.Font = value; }
147
        }
148

149
        /// <summary>
150
        /// Gets or sets a text fill.
151
        /// </summary>
152
        [Editor("FastReport.TypeEditors.FillEditor, FastReport", typeof(UITypeEditor))]
153
        public FillBase TextFill
154
        {
155
            get { return textObject.TextFill; }
156
            set { textObject.TextFill = value; }
157
        }
158

159
        /// <summary>
160
        /// Gets or sets a text rotation.
161
        /// </summary>
162
        [DefaultValue(WatermarkTextRotation.ForwardDiagonal)]
163
        public WatermarkTextRotation TextRotation
164
        {
165
            get { return textRotation; }
166
            set { 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)]
173
        public bool ShowTextOnTop
174
        {
175
            get { return showTextOnTop; }
176
            set { 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)]
183
        public bool ShowImageOnTop
184
        {
185
            get { return showImageOnTop; }
186
            set { showImageOnTop = value; }
187
        }
188

189
        internal TextObject TextObject
190
        {
191
            get { return textObject; }
192
        }
193

194
        /// <summary>
195
        /// 
196
        /// </summary>
197
        public PictureObject PictureObject
198
        {
199
            get { return pictureObject; }
200
            set { 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

210
        private bool ShouldSerializeTextFill()
211
        {
212
            return !(TextFill is SolidFill) || (TextFill as SolidFill).Color != Color.LightGray;
213
        }
214

215
        private bool ShouldSerializeImage()
216
        {
217
            return 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>
229
        public virtual void DrawImage(FRPaintEventArgs e, RectangleF displayRect, Report report, bool isPrinting)
230
        {
231
            pictureObject.SetReport(report);
232
            pictureObject.Bounds = displayRect;
233
            PictureBoxSizeMode sizeMode = PictureBoxSizeMode.Normal;
234
            if (ImageSize == WatermarkImageSize.Stretch)
235
                sizeMode = PictureBoxSizeMode.StretchImage;
236
            else if (ImageSize == WatermarkImageSize.Zoom)
237
                sizeMode = PictureBoxSizeMode.Zoom;
238
            else if (ImageSize == WatermarkImageSize.Center)
239
                sizeMode = PictureBoxSizeMode.CenterImage;
240
            pictureObject.SizeMode = sizeMode;
241
            pictureObject.Tile = ImageSize == WatermarkImageSize.Tile;
242
            pictureObject.SetPrinting(isPrinting);
243
            pictureObject.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>
253
        public void DrawText(FRPaintEventArgs e, RectangleF displayRect, Report report, bool isPrinting)
254
        {
255
            textObject.SetReport(report);
256
            textObject.Bounds = displayRect;
257
            int angle = 0;
258
            switch (TextRotation)
259
            {
260
                case WatermarkTextRotation.Horizontal:
261
                    angle = 0;
262
                    break;
263
                case WatermarkTextRotation.Vertical:
264
                    angle = 270;
265
                    break;
266
                case WatermarkTextRotation.ForwardDiagonal:
267
                    angle = 360 - (int)(Math.Atan(displayRect.Height / displayRect.Width) * (180 / Math.PI));
268
                    break;
269
                case WatermarkTextRotation.BackwardDiagonal:
270
                    angle = (int)(Math.Atan(displayRect.Height / displayRect.Width) * (180 / Math.PI));
271
                    break;
272
            }
273
            textObject.Angle = angle;
274
            textObject.SetPrinting(isPrinting);
275
            textObject.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>
287
        public void Serialize(FRWriter writer, string prefix, Watermark c)
288
        {
289
            if (Enabled != c.Enabled)
290
                writer.WriteBool(prefix + ".Enabled", Enabled);
291
            if (!writer.AreEqual(Image, c.Image))
292
                writer.WriteValue(prefix + ".Image", Image);
293
            if (ImageSize != c.ImageSize)
294
                writer.WriteValue(prefix + ".ImageSize", ImageSize);
295
            if (ImageTransparency != c.ImageTransparency)
296
                writer.WriteFloat(prefix + ".ImageTransparency", ImageTransparency);
297
            if (Text != c.Text)
298
                writer.WriteStr(prefix + ".Text", Text);
299
            if ((writer.SerializeTo != SerializeTo.Preview || !writer.AreEqual(Font, c.Font)) && writer.ItemName != "inherited")
300
                writer.WriteValue(prefix + ".Font", Font);
301
            TextFill.Serialize(writer, prefix + ".TextFill", c.TextFill);
302
            if (TextRotation != c.TextRotation)
303
                writer.WriteValue(prefix + ".TextRotation", TextRotation);
304
            if (ShowTextOnTop != c.ShowTextOnTop)
305
                writer.WriteBool(prefix + ".ShowTextOnTop", ShowTextOnTop);
306
            if (ShowImageOnTop != c.ShowImageOnTop)
307
                writer.WriteBool(prefix + ".ShowImageOnTop", ShowImageOnTop);
308
        }
309

310
        /// <summary>
311
        /// Disposes resources used by the watermark.
312
        /// </summary>
313
        public void Dispose()
314
        {
315
            pictureObject.Dispose();
316
            textObject.Dispose();
317
        }
318

319
        /// <summary>
320
        /// Assigns values from another source.
321
        /// </summary>
322
        /// <param name="source">Source to assign from.</param>
323
        public void Assign(Watermark source)
324
        {
325
            Enabled = source.Enabled;
326
            Image = source.Image == null ? null : source.Image.Clone() as Image;
327
            ImageSize = source.ImageSize;
328
            ImageTransparency = source.ImageTransparency;
329
            Text = source.Text;
330
            Font = source.Font;
331
            TextFill = source.TextFill.Clone();
332
            TextRotation = source.TextRotation;
333
            ShowTextOnTop = source.ShowTextOnTop;
334
            ShowImageOnTop = source.ShowImageOnTop;
335
        }
336

337
        /// <summary>
338
        /// Creates exact copy of this <b>Watermark</b>.
339
        /// </summary>
340
        /// <returns>Copy of this watermark.</returns>
341
        public Watermark Clone()
342
        {
343
            Watermark result = new Watermark();
344
            result.Assign(this);
345
            return result;
346
        }
347
        #endregion
348

349
        /// <summary>
350
        /// Initializes a new instance of the <see cref="Watermark"/> class with default settings.
351
        /// </summary>
352
        public Watermark()
353
        {
354
            pictureObject = new PictureObject();
355
            textObject = new TextObject();
356

357
            pictureObject.ShowErrorImage = false;
358
            textObject.HorzAlign = HorzAlign.Center;
359
            textObject.VertAlign = VertAlign.Center;
360
            ImageSize = WatermarkImageSize.Zoom;
361
            Font = new Font(DrawUtils.DefaultReportFont.Name, 60);
362
            TextFill = new SolidFill(Color.FromArgb(40, Color.Gray));
363
            TextRotation = WatermarkTextRotation.ForwardDiagonal;
364
            ShowTextOnTop = true;
365
        }
366
    }
367
}
368

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.