Repository navigation
Expand file tree
/
Copy pathPdfFile.cs
More file actions
410 lines (376 loc) · 16.1 KB
/
Copy pathPdfFile.cs
File metadata and controls
410 lines (376 loc) · 16.1 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
using Selenium.Internal;
using Selenium.Pdf;
using System;
using System.ComponentModel;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using Bitmap = System.Drawing.Bitmap;
namespace Selenium {
/// <summary>
/// PDF object
/// </summary>
[ProgId("Selenium.PdfFile")]
[Guid("0277FC34-FD1B-4616-BB19-CDCD9EB97FD6")]
[Description("Creates a new empty PDF file")]
[ComVisible(true), ClassInterface(ClassInterfaceType.None)]
public class PdfFile : ComInterfaces._PdfFile, IDisposable {
const int IMG_SPLIT_HEIGHT_MIN_PT = 50;
private int _pageWidthPt = (int)(8.5 * 72);
private int _pageHeightPt = (int)(11 * 72);
private int _marginLeftPt = (int)(0.5 * 72);
private int _marginRightPt = (int)(0.5 * 72);
private int _marginTopPt = (int)(0.5 * 72);
private int _marginBottomPt = (int)(0.5 * 72);
private int _pageNumberRightPt = (int)(0.4 * 72);
private int _pageNumberBottomPt = (int)(0.4 * 72);
private PdfWriter _writer;
private int _contentHeightPt = 0;
private int _title1Count;
/// <summary>
/// Creates a new empty PDF
/// </summary>
public PdfFile() {
_writer = new PdfWriter();
_writer.AddPage(_pageWidthPt, _pageHeightPt);
COMDisposable.Subscribe(this, typeof(ComInterfaces._PdfFile));
}
~PdfFile() {
this.Dispose();
}
/// <summary>
/// Release the resources.
/// </summary>
public void Dispose() {
if (_writer == null)
return;
_writer.Dispose();
_writer = null;
}
private void EnsureNotDisposed() {
if (_writer == null)
throw new Errors.PdfError("The PDF has been disposed.");
}
/// <summary>
/// Set the size of the page in millimeter(mm), inch(in) or point(pt).
/// </summary>
/// <param name="width"></param>
/// <param name="heigth"></param>
/// <param name="metric">Optional metric: mm, in or pt</param>
public void SetPageSize(int width, int heigth, string metric = "in") {
EnsureNotDisposed();
float ratio = GetRatio(metric);
_pageWidthPt = (int)(width * ratio);
_pageHeightPt = (int)(heigth * ratio);
if (_pageWidthPt < 1 || _pageHeightPt < 1)
throw new Errors.PdfError("Out of range: width and heigth.");
_writer.SetPageSize(_pageWidthPt, _pageHeightPt);
}
/// <summary>
/// Set the margins of the page in millimeter(mm), inch(in) or point(pt).
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <param name="top"></param>
/// <param name="bottom"></param>
/// <param name="metric">Optional metric: mm, in or pt</param>
public void SetMargins(int left, int right, int top, int bottom, string metric = "in") {
EnsureNotDisposed();
float ratio = GetRatio(metric);
_marginLeftPt = (int)(left * ratio);
_marginRightPt = (int)(right * ratio);
_marginTopPt = (int)(top * ratio);
_marginBottomPt = (int)(bottom * ratio);
if (left < 0 || right < 0 || ((_marginLeftPt + _marginRightPt) * 100) / _pageWidthPt > 80)
throw new Errors.PdfError("Out of range: left, right.");
if (top < 0 || bottom < 0 || ((_marginTopPt + _marginBottomPt) * 100) / _pageHeightPt > 80)
throw new Errors.PdfError("Out of range: top, bottom.");
}
/// <summary>
/// Adds a new page.
/// </summary>
public void AddPage() {
EnsureNotDisposed();
_writer.AddPage(_pageWidthPt, _pageHeightPt);
_contentHeightPt = 0;
}
/// <summary>
/// Saves as a file.
/// </summary>
/// <param name="filePath"></param>
public void SaveAs(string filePath) {
EnsureNotDisposed();
PdfFont font = PdfFonts.Select(false, false);
int x = -_pageNumberRightPt;
int y = _pageNumberBottomPt;
_writer.WritePagesNumbers(font, 9, x, y, 0);
_writer.Close();
try {
if (filePath.IndexOf('{') != -1)
filePath = string.Format(filePath, DateTime.UtcNow);
filePath = IOExt.ExpandPath(filePath);
filePath = Path.GetFullPath(filePath);
string folder = Path.GetDirectoryName(filePath);
if (!Directory.Exists(folder))
Directory.CreateDirectory(folder);
if (File.Exists(filePath))
File.Delete(filePath);
using (var fs = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write)) {
fs.Write(_writer.Buffer, 0, _writer.Length);
}
} catch (Exception ex) {
throw new SeleniumException(ex);
} finally {
this.Dispose();
}
}
/// <summary>
/// Adds a bookmark.
/// </summary>
/// <param name="name"></param>
public void AddBookmark(string name) {
EnsureNotDisposed();
int x = CurrentY();
_writer.WriteOutline(name, x);
}
/// <summary>
/// Adds a vacant height in millimeter(default), inch or point.
/// </summary>
/// <param name="size"></param>
public void AddSpace(int size) {
EnsureNotDisposed();
_contentHeightPt += size;
}
/// <summary>
/// Adds the text at the current position.
/// </summary>
/// <param name="text">Text</param>
/// <param name="size">Font size</param>
/// <param name="bold">Font wigth</param>
/// <param name="italic">Font italic</param>
/// <param name="color">Name or hexa value. Ex: "red", "FF0000"</param>
public void AddText(string text, short size = 10
, bool bold = false, bool italic = false, string color = null) {
EnsureNotDisposed();
const int indent = 0;
int space = (int)(size * 0.2f);
const int spaceBefore = 0;
const int spaceAfter = 4;
WriteText(text, size, bold, italic, color, false, indent, space, spaceBefore, spaceAfter);
}
/// <summary>
/// Adds text centered horizontally
/// </summary>
/// <param name="text">Text</param>
/// <param name="size">Font size</param>
/// <param name="bold">Font wigth</param>
/// <param name="italic">Font italic</param>
/// <param name="color">Name or hexa value. Ex: "red", "FF0000"</param>
public void AddTextCenter(string text, short size = 10
, bool bold = false, bool italic = false, string color = null) {
EnsureNotDisposed();
const int indent = 0;
const int space = 0;
const int spaceBefore = 0;
const int spaceAfter = 4;
WriteText(text, size, bold, italic, color, true, indent, space, spaceBefore, spaceAfter);
}
/// <summary>
/// Adds the text at the current position.
/// </summary>
/// <param name="text">Text</param>
/// <param name="size">Font size</param>
/// <param name="bold">Font weight</param>
/// <param name="italic">Font is italic</param>
public void AddTitle(string text, short size = 12, bool bold = true, bool italic = false) {
EnsureNotDisposed();
_title1Count++;
string title = _title1Count + ". " + text;
_writer.WriteOutline(title, CurrentY());
int indent = size;
int space = (int)(size * 0.2f);
int spaceBefore = (int)(size * 0.8f);
int spaceAfter = (int)(size * 0.4f);
WriteText(title, size, bold, italic, null, false, indent, space, spaceBefore, spaceAfter);
}
/// <summary>
/// Adds a link
/// </summary>
/// <param name="link">Link to add</param>
/// <param name="text">Optional - Text for the link</param>
/// <param name="size">Optional - Font size</param>
public void AddLink(string link, string text = null, int size = 10) {
if (string.IsNullOrEmpty(text))
text = link;
PdfFont font = PdfFonts.Select(false, false);
int availableWidthPt = AvailableWidth();
int availableHeightPt = AvailableHeight();
float lineHeightPt;
float[] linesWidthPt;
var lines = font.BreakText(text, size, availableWidthPt, out linesWidthPt, out lineHeightPt);
if (availableHeightPt < lineHeightPt * lines.Count) {
this.AddPage();
}
int linesCount = lines.Count;
int x1 = CurrentX();
int x2 = x1 + (int)linesWidthPt[0];
int y1 = CurrentY();
int y2 = (int)(y1 - linesCount * lineHeightPt);
_writer.WriteLines(lines, 0, linesCount, x1, y1, font, size, 0x0000F0, lineHeightPt);
_writer.WriteLink(link, x1, y2, x2, y1);
_contentHeightPt += (int)(linesCount * lineHeightPt) + 4;
}
/// <summary>
/// Writes one or more lines of text
/// </summary>
/// <param name="text">Text</param>
/// <param name="size">Font size</param>
/// <param name="bold">Font bold</param>
/// <param name="italic">Font italic</param>
/// <param name="color">Font color</param>
/// <param name="center">Center the text or not</param>
/// <param name="indent">Indentation in em</param>
/// <param name="spacing">Spacing in em</param>
/// <param name="spacebefore">Space before in em</param>
/// <param name="spaceAfter">Space after in em</param>
private void WriteText(string text, short size, bool bold, bool italic
, string color, bool center, int indent, int spacing, int spacebefore, int spaceAfter) {
EnsureNotDisposed();
PdfFont font = PdfFonts.Select(bold, italic);
int rgb = ParseColorToRGB(color);
int availableWidthPt = AvailableWidth();
float lineHeightPt;
float[] linesWidthPt;
var lines = font.BreakText(text, size, availableWidthPt, out linesWidthPt, out lineHeightPt);
lineHeightPt += spacing;
if (_contentHeightPt != 0)
_contentHeightPt += spacebefore;
int x = (int)(CurrentX() + indent);
for (int iLine = 0, lineCount = lines.Count; iLine < lineCount; ) {
int maxLineCount = (int)(AvailableHeight() / lineHeightPt);
int chunkLineCount = Math.Min(lineCount - iLine, maxLineCount);
if (chunkLineCount < 1) {
this.AddPage();
continue;
}
int y = CurrentY();
if (center) {
int xx = x + (availableWidthPt / 2);
_writer.WriteLinesCenter(lines, iLine, chunkLineCount
, xx, y, font, size, rgb, linesWidthPt, lineHeightPt);
} else {
_writer.WriteLines(lines, iLine, chunkLineCount
, x, y, font, size, rgb, lineHeightPt);
}
iLine += chunkLineCount;
_contentHeightPt += (int)(chunkLineCount * lineHeightPt);
}
_contentHeightPt += spaceAfter;
}
/// <summary>
/// Adds an image at the current position
/// </summary>
/// <param name="image"></param>
/// <param name="autoDispose">Release the resources of the image if true</param>
public void AddImage(global::Selenium.Image image, bool autoDispose = true) {
EnsureNotDisposed();
Bitmap bitmap = image.GetBitmap();
int imgWidthPt = (int)(72.0 * bitmap.Width / bitmap.HorizontalResolution);
int imgHeightPt = (int)(72.0 * bitmap.Height / bitmap.VerticalResolution);
int availableWidthPt = AvailableWidth();
if (imgWidthPt > availableWidthPt) {
imgHeightPt = (imgHeightPt * availableWidthPt) / imgWidthPt;
imgWidthPt = availableWidthPt;
}
int imgOffsetPt = 0;
int x = CurrentX();
while (true) {
int availableHeightPt = AvailableHeight();
if (availableHeightPt < IMG_SPLIT_HEIGHT_MIN_PT
&& imgHeightPt > IMG_SPLIT_HEIGHT_MIN_PT) {
this.AddPage();
continue;
}
int chunkHeightPt = Math.Min(availableHeightPt, imgHeightPt - imgOffsetPt);
int y = CurrentY() - chunkHeightPt;
if (imgHeightPt != chunkHeightPt) {
int chunkHeightPx = (bitmap.Height * chunkHeightPt) / imgHeightPt;
int chunkOffsetPx = (bitmap.Height * imgOffsetPt) / imgHeightPt;
var rect = new System.Drawing.Rectangle(0, chunkOffsetPx, bitmap.Width, chunkHeightPx);
using (var imgChunk = bitmap.Clone(rect, PixelFormat.Format24bppRgb)) {
_writer.WriteBitmap(imgChunk, x, y, imgWidthPt, chunkHeightPt);
}
imgOffsetPt += chunkHeightPt;
if (imgOffsetPt >= imgHeightPt) {
_contentHeightPt += chunkHeightPt;
break;
}
imgOffsetPt -= 5;
this.AddPage();
} else {
_writer.WriteBitmap(bitmap, x, y, imgWidthPt, imgHeightPt);
_contentHeightPt += imgHeightPt;
break;
}
}
_contentHeightPt += 8;
if (autoDispose)
image.Dispose();
}
/// Content available width in points
private int AvailableHeight() {
return _pageHeightPt - _marginTopPt - _contentHeightPt - _marginBottomPt;
}
/// <summary>
/// Content available height in points
/// </summary>
/// <returns></returns>
private int AvailableWidth() {
return _pageWidthPt - _marginLeftPt - _marginRightPt;
}
/// <summary>
/// Current y position in points from the bottom
/// </summary>
/// <returns></returns>
private int CurrentY() {
return _pageHeightPt - _marginTopPt - _contentHeightPt;
}
/// <summary>
/// Current x position in points from the left
/// </summary>
/// <returns></returns>
private int CurrentX() {
return _marginLeftPt;
}
/// <summary>
/// Get or set the metric: millimeter=mm, inch=in or point=pt.
/// </summary>
private static float GetRatio(string metric) {
switch (metric) {
case "pt": return 1f;
case "in": return 72f;
case "mm": return 72f / 25.4f;
default: throw new Errors.PdfError("Invalid metric: " + metric);
}
}
/// <summary>
/// Parse a color to a value. Can be a name or an RGB hexa value.
/// Ex : "red", "FF0000"
/// </summary>
/// <param name="color"></param>
/// <returns></returns>
private static int ParseColorToRGB(string color) {
if (color == null || color.Length == 0)
return 0;
int rgb;
if (int.TryParse(color, NumberStyles.AllowHexSpecifier, CultureInfo.CurrentCulture, out rgb))
return rgb;
try {
return System.Drawing.Color.FromName(color).ToArgb();
} catch {
throw new Errors.PdfError("Invalid color: " + color);
}
}
}
}