回答編集履歴
1
見直しキャンペーン中
test
CHANGED
@@ -1,321 +1,161 @@
|
|
1
1
|
[C# - iTextSharpで,テーブルの線を点線にしたいです。(C#)|teratail](https://teratail.com/questions/292675)
|
2
|
-
|
3
2
|
の使いまわしですが、上下左右別々に設定できて自由度が高いのでそのまま採用します。
|
4
3
|
|
5
|
-
|
6
|
-
|
7
4
|
要は2回、線を引けばいいのです。
|
8
|
-
|
9
|
-
```
|
5
|
+
```cs
|
10
|
-
|
11
6
|
using System.IO;
|
12
|
-
|
13
7
|
using iTextSharp.text;
|
14
|
-
|
15
8
|
using iTextSharp.text.pdf;
|
16
9
|
|
17
|
-
|
18
|
-
|
19
10
|
namespace Questions292675
|
20
|
-
|
21
11
|
{
|
22
|
-
|
23
12
|
internal class Program
|
24
|
-
|
25
13
|
{
|
26
|
-
|
27
14
|
private static void Main()
|
28
|
-
|
29
15
|
{
|
30
|
-
|
31
16
|
using(var doc = new Document(PageSize.A4))
|
32
|
-
|
33
17
|
{
|
34
|
-
|
35
18
|
PdfWriter.GetInstance(doc, new FileStream("BorderTest.pdf", FileMode.Create));
|
36
|
-
|
37
19
|
doc.Open();
|
38
20
|
|
39
|
-
|
40
|
-
|
41
21
|
var solid = new SolidLine(1); // 普通の
|
42
|
-
|
43
22
|
var dotted = new DottedLine(1); // 点々
|
44
|
-
|
45
23
|
var dashed = new DashedLine(1); // 破線
|
46
|
-
|
47
|
-
|
48
24
|
|
49
25
|
var table = new PdfPTable(3);
|
50
26
|
|
51
|
-
|
52
|
-
|
53
27
|
var cell = new PdfPCell(new Phrase("sample"))
|
54
|
-
|
55
28
|
{
|
56
|
-
|
57
29
|
HorizontalAlignment = 1,
|
58
|
-
|
59
30
|
VerticalAlignment = 1,
|
60
31
|
|
61
|
-
|
62
|
-
|
63
32
|
Border = 0, // 通常のボーダーは引かない
|
64
|
-
|
65
33
|
// 独自セルレンダラーを追加し描画 左,右,上,下
|
66
|
-
|
67
34
|
CellEvent = new CustomBorder2(solid, solid, solid, solid),
|
68
|
-
|
69
35
|
Padding = 5, // 内側に2本目が来るので適当に開ける
|
70
|
-
|
71
36
|
PaddingBottom = 10,
|
72
|
-
|
73
37
|
};
|
74
38
|
|
75
|
-
|
76
|
-
|
77
39
|
table.AddCell(cell);
|
78
|
-
|
79
40
|
table.AddCell(cell);
|
80
|
-
|
81
41
|
table.AddCell(cell);
|
82
|
-
|
83
42
|
doc.Add(table);
|
84
|
-
|
85
43
|
}
|
86
44
|
|
45
|
+
System.Diagnostics.Process.Start("BorderTest.pdf");
|
46
|
+
}
|
47
|
+
}
|
87
48
|
|
49
|
+
internal interface ILineDash
|
50
|
+
{
|
51
|
+
void ApplyLineDash(PdfContentByte canvas);
|
52
|
+
}
|
88
53
|
|
89
|
-
|
54
|
+
internal abstract class AbstractLineDash : ILineDash
|
55
|
+
{
|
56
|
+
private float lineWidth;
|
57
|
+
public AbstractLineDash(float lineWidth) => this.lineWidth = lineWidth;
|
58
|
+
public virtual void ApplyLineDash(PdfContentByte canvas) => canvas.SetLineWidth(lineWidth);
|
59
|
+
}
|
90
60
|
|
61
|
+
internal class SolidLine : AbstractLineDash
|
62
|
+
{
|
63
|
+
public SolidLine(float lineWidth) : base(lineWidth) { }
|
64
|
+
}
|
65
|
+
|
66
|
+
internal class DottedLine : AbstractLineDash
|
67
|
+
{
|
68
|
+
public DottedLine(float lineWidth) : base(lineWidth) { }
|
69
|
+
public override void ApplyLineDash(PdfContentByte canvas)
|
70
|
+
{
|
71
|
+
base.ApplyLineDash(canvas);
|
72
|
+
canvas.SetLineCap(PdfContentByte.LINE_CAP_ROUND);
|
73
|
+
canvas.SetLineDash(0, 4, 2);
|
74
|
+
}
|
75
|
+
}
|
76
|
+
|
77
|
+
internal class DashedLine : AbstractLineDash
|
78
|
+
{
|
79
|
+
public DashedLine(float lineWidth) : base(lineWidth) { }
|
80
|
+
public override void ApplyLineDash(PdfContentByte canvas)
|
81
|
+
{
|
82
|
+
base.ApplyLineDash(canvas);
|
83
|
+
canvas.SetLineDash(3, 3);
|
84
|
+
}
|
85
|
+
}
|
86
|
+
|
87
|
+
internal class CustomBorder2 : IPdfPCellEvent
|
88
|
+
{
|
89
|
+
protected ILineDash left;
|
90
|
+
protected ILineDash right;
|
91
|
+
protected ILineDash top;
|
92
|
+
protected ILineDash bottom;
|
93
|
+
|
94
|
+
private int offset = 5; // 線の間隔
|
95
|
+
public CustomBorder2(ILineDash left, ILineDash right, ILineDash top, ILineDash bottom)
|
96
|
+
{
|
97
|
+
this.left = left;
|
98
|
+
this.right = right;
|
99
|
+
this.top = top;
|
100
|
+
this.bottom = bottom;
|
91
101
|
}
|
92
102
|
|
103
|
+
public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)
|
104
|
+
{
|
105
|
+
// position.Top > position.Bottom になっていたので直観とは逆のオフセットになっている
|
106
|
+
// (左下が0の座標系?常にそうなのかは調べていない)
|
107
|
+
var canvas = canvases[PdfPTable.LINECANVAS];
|
108
|
+
if(top != null) // 上の線
|
109
|
+
{
|
110
|
+
canvas.SaveState();
|
111
|
+
top.ApplyLineDash(canvas);
|
112
|
+
canvas.MoveTo(position.Right, position.Top);
|
113
|
+
canvas.LineTo(position.Left, position.Top);
|
114
|
+
canvas.Stroke();
|
115
|
+
canvas.MoveTo(position.Right - offset, position.Top - offset);
|
116
|
+
canvas.LineTo(position.Left + offset, position.Top - offset);
|
117
|
+
canvas.Stroke();
|
118
|
+
canvas.RestoreState();
|
119
|
+
}
|
120
|
+
if(bottom != null) // 下の線
|
121
|
+
{
|
122
|
+
canvas.SaveState();
|
123
|
+
bottom.ApplyLineDash(canvas);
|
124
|
+
canvas.MoveTo(position.Right, position.Bottom);
|
125
|
+
canvas.LineTo(position.Left, position.Bottom);
|
126
|
+
canvas.Stroke();
|
127
|
+
canvas.MoveTo(position.Right - offset, position.Bottom + offset);
|
128
|
+
canvas.LineTo(position.Left + offset, position.Bottom + offset);
|
129
|
+
canvas.Stroke();
|
130
|
+
canvas.RestoreState();
|
131
|
+
}
|
132
|
+
if(right != null) // 右の線
|
133
|
+
{
|
134
|
+
canvas.SaveState();
|
135
|
+
right.ApplyLineDash(canvas);
|
136
|
+
canvas.MoveTo(position.Right, position.Top);
|
137
|
+
canvas.LineTo(position.Right, position.Bottom);
|
138
|
+
canvas.Stroke();
|
139
|
+
canvas.MoveTo(position.Right - offset, position.Top - offset);
|
140
|
+
canvas.LineTo(position.Right - offset, position.Bottom + offset);
|
141
|
+
canvas.Stroke();
|
142
|
+
canvas.RestoreState();
|
143
|
+
}
|
144
|
+
if(left != null) // 左の線
|
145
|
+
{
|
146
|
+
canvas.SaveState();
|
147
|
+
left.ApplyLineDash(canvas);
|
148
|
+
canvas.MoveTo(position.Left, position.Top);
|
149
|
+
canvas.LineTo(position.Left, position.Bottom);
|
150
|
+
canvas.Stroke();
|
151
|
+
canvas.MoveTo(position.Left + offset, position.Top - offset);
|
152
|
+
canvas.LineTo(position.Left + offset, position.Bottom + offset);
|
153
|
+
canvas.Stroke();
|
154
|
+
canvas.RestoreState();
|
155
|
+
}
|
156
|
+
}
|
93
157
|
}
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
internal interface ILineDash
|
98
|
-
|
99
|
-
{
|
100
|
-
|
101
|
-
void ApplyLineDash(PdfContentByte canvas);
|
102
|
-
|
103
|
-
}
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
internal abstract class AbstractLineDash : ILineDash
|
108
|
-
|
109
|
-
{
|
110
|
-
|
111
|
-
private float lineWidth;
|
112
|
-
|
113
|
-
public AbstractLineDash(float lineWidth) => this.lineWidth = lineWidth;
|
114
|
-
|
115
|
-
public virtual void ApplyLineDash(PdfContentByte canvas) => canvas.SetLineWidth(lineWidth);
|
116
|
-
|
117
|
-
}
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
internal class SolidLine : AbstractLineDash
|
122
|
-
|
123
|
-
{
|
124
|
-
|
125
|
-
public SolidLine(float lineWidth) : base(lineWidth) { }
|
126
|
-
|
127
|
-
}
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
internal class DottedLine : AbstractLineDash
|
132
|
-
|
133
|
-
{
|
134
|
-
|
135
|
-
public DottedLine(float lineWidth) : base(lineWidth) { }
|
136
|
-
|
137
|
-
public override void ApplyLineDash(PdfContentByte canvas)
|
138
|
-
|
139
|
-
{
|
140
|
-
|
141
|
-
base.ApplyLineDash(canvas);
|
142
|
-
|
143
|
-
canvas.SetLineCap(PdfContentByte.LINE_CAP_ROUND);
|
144
|
-
|
145
|
-
canvas.SetLineDash(0, 4, 2);
|
146
|
-
|
147
|
-
}
|
148
|
-
|
149
|
-
}
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
internal class DashedLine : AbstractLineDash
|
154
|
-
|
155
|
-
{
|
156
|
-
|
157
|
-
public DashedLine(float lineWidth) : base(lineWidth) { }
|
158
|
-
|
159
|
-
public override void ApplyLineDash(PdfContentByte canvas)
|
160
|
-
|
161
|
-
{
|
162
|
-
|
163
|
-
base.ApplyLineDash(canvas);
|
164
|
-
|
165
|
-
canvas.SetLineDash(3, 3);
|
166
|
-
|
167
|
-
}
|
168
|
-
|
169
|
-
}
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
internal class CustomBorder2 : IPdfPCellEvent
|
174
|
-
|
175
|
-
{
|
176
|
-
|
177
|
-
protected ILineDash left;
|
178
|
-
|
179
|
-
protected ILineDash right;
|
180
|
-
|
181
|
-
protected ILineDash top;
|
182
|
-
|
183
|
-
protected ILineDash bottom;
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
private int offset = 5; // 線の間隔
|
188
|
-
|
189
|
-
public CustomBorder2(ILineDash left, ILineDash right, ILineDash top, ILineDash bottom)
|
190
|
-
|
191
|
-
{
|
192
|
-
|
193
|
-
this.left = left;
|
194
|
-
|
195
|
-
this.right = right;
|
196
|
-
|
197
|
-
this.top = top;
|
198
|
-
|
199
|
-
this.bottom = bottom;
|
200
|
-
|
201
|
-
}
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)
|
206
|
-
|
207
|
-
{
|
208
|
-
|
209
|
-
// position.Top > position.Bottom になっていたので直観とは逆のオフセットになっている
|
210
|
-
|
211
|
-
// (左下が0の座標系?常にそうなのかは調べていない)
|
212
|
-
|
213
|
-
var canvas = canvases[PdfPTable.LINECANVAS];
|
214
|
-
|
215
|
-
if(top != null) // 上の線
|
216
|
-
|
217
|
-
{
|
218
|
-
|
219
|
-
canvas.SaveState();
|
220
|
-
|
221
|
-
top.ApplyLineDash(canvas);
|
222
|
-
|
223
|
-
canvas.MoveTo(position.Right, position.Top);
|
224
|
-
|
225
|
-
canvas.LineTo(position.Left, position.Top);
|
226
|
-
|
227
|
-
canvas.Stroke();
|
228
|
-
|
229
|
-
canvas.MoveTo(position.Right - offset, position.Top - offset);
|
230
|
-
|
231
|
-
canvas.LineTo(position.Left + offset, position.Top - offset);
|
232
|
-
|
233
|
-
canvas.Stroke();
|
234
|
-
|
235
|
-
canvas.RestoreState();
|
236
|
-
|
237
|
-
}
|
238
|
-
|
239
|
-
if(bottom != null) // 下の線
|
240
|
-
|
241
|
-
{
|
242
|
-
|
243
|
-
canvas.SaveState();
|
244
|
-
|
245
|
-
bottom.ApplyLineDash(canvas);
|
246
|
-
|
247
|
-
canvas.MoveTo(position.Right, position.Bottom);
|
248
|
-
|
249
|
-
canvas.LineTo(position.Left, position.Bottom);
|
250
|
-
|
251
|
-
canvas.Stroke();
|
252
|
-
|
253
|
-
canvas.MoveTo(position.Right - offset, position.Bottom + offset);
|
254
|
-
|
255
|
-
canvas.LineTo(position.Left + offset, position.Bottom + offset);
|
256
|
-
|
257
|
-
canvas.Stroke();
|
258
|
-
|
259
|
-
canvas.RestoreState();
|
260
|
-
|
261
|
-
}
|
262
|
-
|
263
|
-
if(right != null) // 右の線
|
264
|
-
|
265
|
-
{
|
266
|
-
|
267
|
-
canvas.SaveState();
|
268
|
-
|
269
|
-
right.ApplyLineDash(canvas);
|
270
|
-
|
271
|
-
canvas.MoveTo(position.Right, position.Top);
|
272
|
-
|
273
|
-
canvas.LineTo(position.Right, position.Bottom);
|
274
|
-
|
275
|
-
canvas.Stroke();
|
276
|
-
|
277
|
-
canvas.MoveTo(position.Right - offset, position.Top - offset);
|
278
|
-
|
279
|
-
canvas.LineTo(position.Right - offset, position.Bottom + offset);
|
280
|
-
|
281
|
-
canvas.Stroke();
|
282
|
-
|
283
|
-
canvas.RestoreState();
|
284
|
-
|
285
|
-
}
|
286
|
-
|
287
|
-
if(left != null) // 左の線
|
288
|
-
|
289
|
-
{
|
290
|
-
|
291
|
-
canvas.SaveState();
|
292
|
-
|
293
|
-
left.ApplyLineDash(canvas);
|
294
|
-
|
295
|
-
canvas.MoveTo(position.Left, position.Top);
|
296
|
-
|
297
|
-
canvas.LineTo(position.Left, position.Bottom);
|
298
|
-
|
299
|
-
canvas.Stroke();
|
300
|
-
|
301
|
-
canvas.MoveTo(position.Left + offset, position.Top - offset);
|
302
|
-
|
303
|
-
canvas.LineTo(position.Left + offset, position.Bottom + offset);
|
304
|
-
|
305
|
-
canvas.Stroke();
|
306
|
-
|
307
|
-
canvas.RestoreState();
|
308
|
-
|
309
|
-
}
|
310
|
-
|
311
|
-
}
|
312
|
-
|
313
|
-
}
|
314
|
-
|
315
158
|
}
|
316
|
-
|
317
159
|
```
|
318
160
|
|
319
|
-
|
320
|
-
|
321
161
|
![pdf画像](d1a1441e1ab0f5f243de4c06acfdd11d.png)
|