-
Notifications
You must be signed in to change notification settings - Fork 26
/
RtfImage.cs
260 lines (232 loc) · 7.42 KB
/
RtfImage.cs
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
using System;
using System.Configuration;
using System.Text;
using System.Drawing;
using System.IO;
using Image = System.Drawing.Image;
namespace Elistia.DotNetRtfWriter
{
/// <summary>
/// Summary description for RtfImage
/// </summary>
public class RtfImage : RtfBlock
{
private string _imgFname;
private ImageFileType _imgType;
private Byte[] _imgByte;
private Align _alignment;
private Margins _margins;
private float _width;
private float _height;
private bool _keepAspectRatio;
private string _blockHead;
private string _blockTail;
private bool _startNewPage;
private bool _startNewPara;
internal RtfImage(string fileName, ImageFileType type)
{
_imgFname = fileName;
_imgType = type;
_alignment = Align.None;
_margins = new Margins();
_keepAspectRatio = true;
_blockHead = @"{\pard";
_blockTail = @"}";
_startNewPage = false;
_startNewPara = false;
Image image = Image.FromFile(fileName);
_width = (image.Width / image.HorizontalResolution) * 72;
_height = (image.Height / image.VerticalResolution) * 72;
using (MemoryStream mStream = new MemoryStream())
{
image.Save(mStream, image.RawFormat);
_imgByte = mStream.ToArray();
}
}
internal RtfImage(System.IO.MemoryStream imageStream)
{
_alignment = Align.Left;
_margins = new Margins();
_keepAspectRatio = true;
_blockHead = @"{\pard";
_blockTail = @"}";
_startNewPage = false;
_startNewPara = false;
_imgByte = imageStream.ToArray();
Image image = Image.FromStream(imageStream);
_width = (image.Width / image.HorizontalResolution) * 72;
_height = (image.Height / image.VerticalResolution) * 72;
if(image.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png)) _imgType = ImageFileType.Png;
else if (image.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg)) _imgType = ImageFileType.Jpg;
else if (image.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif)) _imgType = ImageFileType.Gif;
else throw new Exception("Image format is not supported: " + image.RawFormat.ToString());
}
public override Align Alignment
{
get
{
return _alignment;
}
set
{
_alignment = value;
}
}
public override Margins Margins
{
get
{
return _margins;
}
}
public override bool StartNewPage
{
get
{
return _startNewPage;
}
set
{
_startNewPage = value;
}
}
public bool StartNewPara
{
get
{
return _startNewPara;
}
set
{
_startNewPara = value;
}
}
public float Width
{
get
{
return _width;
}
set
{
if (_keepAspectRatio && _width > 0) {
float ratio = _height / _width;
_height = value * ratio;
}
_width = value;
}
}
public float Heigth
{
get
{
return _height;
}
set
{
if (_keepAspectRatio && _height > 0) {
float ratio = _width / _height;
_width = value * ratio;
}
_height = value;
}
}
public bool KeepAspectRatio
{
get
{
return _keepAspectRatio;
}
set
{
_keepAspectRatio = value;
}
}
public override RtfCharFormat DefaultCharFormat
{
// DefaultCharFormat is meaningless for RtfImage.
get
{
return null;
}
}
private string extractImage()
{
StringBuilder result = new StringBuilder();
for (int i = 0; i < _imgByte.Length; i++)
{
if (i != 0 && i % 60 == 0)
{
result.AppendLine();
}
result.AppendFormat("{0:x2}", _imgByte[i]);
}
return result.ToString();
}
internal override string BlockHead
{
set
{
_blockHead = value;
}
}
internal override string BlockTail
{
set
{
_blockTail = value;
}
}
public override string render()
{
StringBuilder result = new StringBuilder(_blockHead);
if (_startNewPage) {
result.Append(@"\pagebb");
}
if (_margins[Direction.Top] >= 0) {
result.Append(@"\sb" + RtfUtility.pt2Twip(_margins[Direction.Top]));
}
if (_margins[Direction.Bottom] >= 0) {
result.Append(@"\sa" + RtfUtility.pt2Twip(_margins[Direction.Bottom]));
}
if (_margins[Direction.Left] >= 0) {
result.Append(@"\li" + RtfUtility.pt2Twip(_margins[Direction.Left]));
}
if (_margins[Direction.Right] >= 0) {
result.Append(@"\ri" + RtfUtility.pt2Twip(_margins[Direction.Right]));
}
switch (_alignment) {
case Align.Left:
result.Append(@"\ql");
break;
case Align.Right:
result.Append(@"\qr");
break;
case Align.Center:
result.Append(@"\qc");
break;
}
result.AppendLine();
result.Append(@"{\*\shppict{\pict");
if (_imgType == ImageFileType.Jpg) {
result.Append(@"\jpegblip");
} else if (_imgType == ImageFileType.Png || _imgType == ImageFileType.Gif) {
result.Append(@"\pngblip");
} else {
throw new Exception("Image type not supported.");
}
if (_height > 0) {
result.Append(@"\pichgoal" + RtfUtility.pt2Twip(_height));
}
if (_width > 0) {
result.Append(@"\picwgoal" + RtfUtility.pt2Twip(_width));
}
result.AppendLine();
result.AppendLine(extractImage());
result.AppendLine("}}");
if (_startNewPara) result.Append(@"\par");
result.AppendLine(_blockTail);
return result.ToString();
}
}
}