-
Notifications
You must be signed in to change notification settings - Fork 305
Expand file tree
/
Copy pathPdfCellData.cs
More file actions
445 lines (405 loc) · 12.5 KB
/
PdfCellData.cs
File metadata and controls
445 lines (405 loc) · 12.5 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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*************************************************************************************************
Required Notice: Copyright (C) EPPlus Software AB.
This software is licensed under PolyForm Noncommercial License 1.0.0
and may only be used for noncommercial purposes
https://polyformproject.org/licenses/noncommercial/1.0.0/
A commercial license to use this software can be purchased at https://epplussoftware.com
*************************************************************************************************
Date Author Change
*************************************************************************************************
27/11/2025 EPPlus Software AB EPPlus 9
*************************************************************************************************/
using EPPlus.Export.Pdf.Pdfhelpers;
using EPPlus.Fonts.OpenType;
using EPPlus.Graphics;
using OfficeOpenXml.Interfaces.Fonts;
using OfficeOpenXml.Style;
using System.Collections.Generic;
using System.Drawing;
namespace EPPlus.Export.Pdf.PdfLayout
{
public enum PdfWritingMode
{
HorizontalLtr,
HorizontalRtl,
VerticalTtb, // top-to-bottom
VerticalBtt, // bottom-to-top
}
internal class PdfCellLines
{
public bool IsRichText = false;
public PdfWritingMode WritingMode { get; set; } = PdfWritingMode.HorizontalLtr;
public List<PdfCellLine> Lines = new List<PdfCellLine>();
private string _text = null;
public string Text
{
get
{
_text = string.Empty;
foreach (var t in Lines)
{
_text += t.Text;
}
return _text;
}
}
public double Height
{
get
{
double val = 0;
foreach (var l in Lines)
{
val += l.LineHeight;
}
return val;
}
}
public double TextLength
{
get
{
double val = 0;
foreach (var tp in Lines)
{
val = tp.TextLength > val ? tp.TextLength : val;
}
return val;
}
}
public double TextHeight
{
get
{
double val = 0;
foreach (var tp in Lines)
{
val = tp.TextHeight > val ? tp.TextHeight : val;
}
return val;
}
}
public double LineHeight
{
get
{
double val = 0;
foreach (var tp in Lines)
{
val = tp.LineHeight > val ? tp.LineHeight : val;
}
return val;
}
}
public double FontHeight
{
get
{
double val = 0;
foreach (var tp in Lines)
{
val = tp.FontHeight > val ? tp.FontHeight : val;
}
return val;
}
}
}
internal class PdfCellLine
{
public double Offset = 0d;
public List<PdfCellWord> Words = new List<PdfCellWord>();
private string _text = null;
public string Text
{
get
{
_text = string.Empty;
foreach (var t in Words)
{
_text += t.Text;
}
return _text;
}
}
public double TextLength
{
get
{
double val = 0;
foreach (var w in Words)
{
val += w.TextLength;
}
return val;
}
}
public double TextHeight
{
get
{
double val = 0;
int first = 0;
int last = Words.Count - 1;
while (first <= last && PdfString.IsNullOrWhiteSpace(Words[first].Text))
first++;
while (last >= first && PdfString.IsNullOrWhiteSpace(Words[last].Text))
last--;
// sum heights between them
for (int i = first; i <= last; i++)
{
val += Words[i].TextHeight;
}
return val;
}
}
public double LineHeight
{
get
{
double val = 0;
foreach (var w in Words)
{
val = w.LineHeight > val ? w.LineHeight : val;
}
return val;
}
}
public double FontHeight
{
get
{
double val = 0;
foreach (var w in Words)
{
val = w.FontHeight > val ? w.FontHeight : val;
}
return val;
}
}
}
internal class PdfCellWord
{
public List<PdfCellTextItem> Characters = new List<PdfCellTextItem>();
private string _text = null;
public string Text
{
get
{
_text = string.Empty;
foreach (var t in Characters)
{
_text += t.Text;
}
return _text;
}
}
public double TextLength
{
get
{
double val = 0;
foreach (var c in Characters)
{
val += c.TextLength;
}
return val;
}
}
public double TextHeight
{
get
{
double val = 0;
foreach (var c in Characters)
{
val += c.LineHeight;
}
return val;
}
}
public double LineHeight
{
get
{
double val = 0;
foreach (var c in Characters)
{
val = c.LineHeight > val ? c.LineHeight : val;
}
return val;
}
}
public double FontHeight
{
get
{
double val = 0;
foreach (var c in Characters)
{
val = c.FontHeight > val ? c.FontHeight : val;
}
return val;
}
}
}
internal struct PdfCellTextItem
{
public string FontName;
public int FontFamily;
public FontSubFamily SubFamily;
public double FontSize;
public bool Bold;
public bool Italic;
public bool Strike;
public bool SubScript;
public bool SuperScript;
public bool Underline;
public ExcelUnderLineType UnderlineType;
public string Text;
public Color FontColor;
public double TextLength;
public double LineHeight;
public double FontHeight;
public Rect GlyphBox;
public double characterOffset;
public List<GlyphPosition> GlyphPositions;
public string FullFontName
{ get { return FontName + " " + SubFamily; } }
//Compares stylings.
public bool Equals(PdfCellTextItem other)
{
if (!string.Equals(FontName, other.FontName))
return false;
if (FontFamily != other.FontFamily)
return false;
if (SubFamily != other.SubFamily)
return false;
if (FontSize != other.FontSize)
return false;
if (Bold != other.Bold ||
Italic != other.Italic ||
Strike != other.Strike ||
SubScript != other.SubScript ||
SuperScript != other.SuperScript ||
Underline != other.Underline)
return false;
if (UnderlineType != other.UnderlineType)
return false;
if (!FontColor.Equals(other.FontColor))
return false;
return true;
}
}
internal class GlyphPosition
{
public char Character;
public double AdvanceX;
public double AdvanceY;
public double OffsetX;
public double OffsetY;
public Rect GlyphBox;
}
internal class PdfCellGradientFillData
{
public ExcelFillGradientType GradientType;
public Color Color1;
public Color Color2;
public Color Color3;
public double Degree;
public double Top;
public double Bottom;
public double Left;
public double Right;
public double[] matrix;
public double[] coords;
public override string ToString()
{
return GradientType.ToString() + Color1.ToHexString() + Color2.ToHexString() + Degree + Top + Bottom + Left + Right;
}
}
internal class PdfCellFillData
{
public string id;
public Color BackgroundColor = Color.Empty;
public ExcelFillStyle PatternStyle = ExcelFillStyle.None;
public Color PatternColor = Color.Black;
//Fill Effects
public PdfCellGradientFillData GradientFillData = null;
public bool enhanceGridLine = false;
public PdfCellFillData() { }
}
internal class PdfCellBordersData
{
public PdfCellBorderData Top = new PdfCellBorderData(LineType.Top);
public PdfCellBorderData Bottom = new PdfCellBorderData(LineType.Bottom);
public PdfCellBorderData Left = new PdfCellBorderData(LineType.Left);
public PdfCellBorderData Right = new PdfCellBorderData(LineType.Right);
public PdfCellBorderData DiagonalUp = new PdfCellBorderData(LineType.DiagonalUp);
public PdfCellBorderData DiagonalDown = new PdfCellBorderData(LineType.DiagonalDown);
public PdfCellBordersData() { }
}
internal enum LineType
{
Top = 0,
Bottom,
Left,
Right,
DiagonalUp,
DiagonalDown
}
internal class PdfCellBorderData
{
internal const double Hair = 0.5d;
internal const double Thin = 0.85d;
internal const double Small = 1.1d;
internal const double Medium = 1.5d;
internal const double Thick = 2.0d;
internal const string NoDash = "[] 0 d";
internal const string Dotted = "[0 2] 0 d";
internal const string DashDot = "[4 2 1 2] 0 d";
internal const string DashDotDot = "[4 2 1 2 1 2] 0 d";
internal const string Dashed = "[4 3] 0 d";
internal const string MediumDashDot = "[6 3 2 3] 0 d";
internal const string MediumDashDotDot = "[6 3 2 3 2 3] 0 d";
internal const string MediumDashed = "[6 4] 0 d";
public ExcelBorderStyle BorderStyle = ExcelBorderStyle.None;
public readonly LineType LineType;
public Color BorderColor = Color.Black;
public double MergedDiagonalWidth = 0d;
public double MergedDiagonalHeight = 0d;
public double Width = 0;
public double Height = 0;
public double X = 0;
public double Y = 0;
public PdfCellBorderData(LineType LineType)
{
this.LineType = LineType;
}
}
internal class PdfCellAlignmentData
{
public ExcelHorizontalAlignment HorizontalAlignment = ExcelHorizontalAlignment.General;
public ExcelVerticalAlignment VerticalAlignment = ExcelVerticalAlignment.Bottom;
public int Indent = 0;
public bool WrapText = false;
public bool ShrinkToFit = false;
public int TextRotation = 0;
public ExcelReadingOrder TextDirection = ExcelReadingOrder.ContextDependent;
public bool IsVertical = false;
public PdfCellAlignmentData() { }
}
internal class GridLine
{
public static double Width = 0.125d;
public static double HalfWidth = Width / 2d;
public static double FourthWidth = Width / 4d;
public double X1;
public double Y1;
public double X2;
public double Y2;
public GridLine(double x1, double y1, double x2, double y2)
{
X1 = x1; Y1 = y1; X2 = x2; Y2 = y2;
}
}
}