-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathForm1.cs
More file actions
145 lines (124 loc) · 4.62 KB
/
Form1.cs
File metadata and controls
145 lines (124 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace tx_watermark
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int iNumberOfPages = 1;
private void insertToolStripMenuItem_Click(object sender, EventArgs e)
{
InsertWatermarkImages();
}
private void InsertWatermarkImages()
{
// remove all existing watermarks
RemoveWatermarkImages(textControl1);
foreach (TXTextControl.Page page in textControl1.GetPages())
{
// create a new watermark image
Bitmap bmp = CreateDraftImage();
// create a new TXTextControl.Image object and mark
// as watermark
TXTextControl.Image img = new TXTextControl.Image(bmp);
img.Name = "Watermark";
img.Sizeable = false;
img.Moveable = false;
// calculate the location to center the image
Point pImageLocation = new Point(
(page.Bounds.Width / 2) - (PixelToTwips(bmp.Size.Width)) / 2,
(page.Bounds.Height / 2) - (PixelToTwips(bmp.Size.Height)) / 2);
// add the image to the page
textControl1.Images.Add(
img,
page.Number,
pImageLocation,
TXTextControl.ImageInsertionMode.BelowTheText);
}
}
// creates a new "DRAFT" sample image
private Bitmap CreateDraftImage()
{
string sText = "DRAFT";
Bitmap destination = new Bitmap(400, 400);
using (Graphics g = Graphics.FromImage(destination))
{
GraphicsUnit units = GraphicsUnit.Pixel;
g.Clear(Color.White);
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
g.DrawString(sText, new Font(
"Arial",
90,
FontStyle.Bold,
GraphicsUnit.Pixel),
new SolidBrush(
Color.FromArgb(64, Color.Black)),
destination.GetBounds(ref units),
stringFormat);
}
return destination;
}
// converts pixels to twips
static int PixelToTwips(int pixel)
{
float DpiX = (float)(1440 / Graphics.FromHwnd(new IntPtr()).DpiX);
return (int)(pixel * DpiX);
}
private void RemoveWatermarkImages(TXTextControl.TextControl textControl)
{
// List of images to be deleted
List<TXTextControl.Image> lImagesToRemove =
new List<TXTextControl.Image>();
// loop through all images and check for the Name
foreach (TXTextControl.Image img in textControl1.Images)
{
// add watermark images to the List
if (img.Name == "Watermark")
lImagesToRemove.Add(img);
}
// remove all tagged watermark images
foreach(TXTextControl.Image img in lImagesToRemove)
{
textControl1.Images.Remove(img);
}
}
// set the EditMode to read only when a watermark is selected
// to avoid the deletion
private void textControl1_ImageSelected(object sender, TXTextControl.ImageEventArgs e)
{
if (e.Image.Name == "Watermark")
textControl1.EditMode = TXTextControl.EditMode.ReadAndSelect;
}
// set back the EditMode
private void textControl1_ImageDeselected(object sender, TXTextControl.ImageEventArgs e)
{
textControl1.EditMode = TXTextControl.EditMode.Edit;
}
// update the watermarks if the number of pages are different
private void textControl1_Changed(object sender, EventArgs e)
{
if (textControl1.Pages > iNumberOfPages)
{
InsertWatermarkImages();
iNumberOfPages = textControl1.Pages;
}
}
private void Form1_Load(object sender, EventArgs e)
{
InsertWatermarkImages();
}
}
}