回答編集履歴

2

見直しキャンペーン中

2023/08/12 10:02

投稿

TN8001
TN8001

スコア9341

test CHANGED
@@ -1,6 +1,6 @@
1
- 注意)`iTextSharp`を初めて使ったので間違いがあるかもしれません。
1
+ 注意)iTextSharpを初めて使ったので間違いがあるかもしれません。
2
2
 
3
- `iTextSharp`は[NuGet Gallery | iTextSharp 5.5.13.2](https://www.nuget.org/packages/iTextSharp/)こちらでいいでしょうか?
3
+ iTextSharpは[NuGet Gallery | iTextSharp 5.5.13.2](https://www.nuget.org/packages/iTextSharp/)こちらでいいでしょうか?
4
4
  > PLEASE NOTE: iTextSharp is EOL, and has been replaced by iText 7. Only security fixes will be added
5
5
  > 注:iTextSharpはEOLであり、iText 7に置き換えられました。セキュリティ修正のみが追加されます
6
6
 
@@ -11,7 +11,7 @@
11
11
 
12
12
  見るところが根本的に違います。なぜそれが使えると思ったのかが不思議です。
13
13
 
14
- 見るのでしたらこちらです(`iTextSharp``iText5`系のようで、Java版しかありませんがヒントにはなります)
14
+ 見るのでしたらこちらです(iTextSharpはiText5系のようで、Java版しかありませんがヒントにはなります)
15
15
  [iText pdf library API documentation for Java and .Net](https://itextpdf.com/en/resources/api-documentation)
16
16
 
17
17
 

1

見直しキャンペーン中

2023/07/23 06:33

投稿

TN8001
TN8001

スコア9341

test CHANGED
@@ -1,317 +1,159 @@
1
1
  注意)`iTextSharp`を初めて使ったので間違いがあるかもしれません。
2
2
 
3
-
4
-
5
3
  `iTextSharp`は[NuGet Gallery | iTextSharp 5.5.13.2](https://www.nuget.org/packages/iTextSharp/)こちらでいいでしょうか?
6
-
7
4
  > PLEASE NOTE: iTextSharp is EOL, and has been replaced by iText 7. Only security fixes will be added
8
-
9
5
  > 注:iTextSharpはEOLであり、iText 7に置き換えられました。セキュリティ修正のみが追加されます
10
-
11
-
12
6
 
13
7
  と移行を促されているようですが。
14
8
 
15
9
 
16
-
17
-
18
-
19
10
  > このページには、BorderStyle列挙型のフィールドで、3は破線であると書かれていました。
20
-
21
-
22
11
 
23
12
  見るところが根本的に違います。なぜそれが使えると思ったのかが不思議です。
24
13
 
25
- 見るのでしたらこちらです。
26
-
27
- `iTextSharp`は`iText5`系のようで、Java版しかありませんがヒントにはなります
14
+ 見るのでしたらこちらです(`iTextSharp`は`iText5`系のようで、Java版しかありませんがヒントにはなります
28
-
29
15
  [iText pdf library API documentation for Java and .Net](https://itextpdf.com/en/resources/api-documentation)
30
16
 
31
17
 
32
-
33
-
34
-
35
18
  本題のボーダーですが、どうやら自分で描画するしかないようです^^;
36
-
37
19
  しかし本家サイトにサンプルがありますので、`CustomBorder3`をベースにざっと移してみました。
38
-
39
20
  [How to define different border types for a single cell?](https://kb.itextpdf.com/home/it5kb/faq/how-to-define-different-border-types-for-a-single-cell)
40
21
 
41
22
 
42
-
43
-
44
-
45
- ```C#
23
+ ```cs
46
-
47
24
  using System.IO;
48
-
49
25
  using iTextSharp.text;
50
-
51
26
  using iTextSharp.text.pdf;
52
27
 
53
-
54
-
55
28
  namespace Questions292675
56
-
57
29
  {
58
-
59
30
  internal class Program
60
-
61
31
  {
62
-
63
32
  private static void Main()
64
-
65
33
  {
66
-
67
34
  using(var doc = new Document(PageSize.A4))
68
-
69
35
  {
70
-
71
36
  PdfWriter.GetInstance(doc, new FileStream("BorderTest.pdf", FileMode.Create));
72
-
73
37
  doc.Open();
74
38
 
75
-
76
-
77
39
  var solid = new SolidLine(1); // 普通の
78
-
79
40
  var dotted = new DottedLine(1); // 点々
80
-
81
41
  var dashed = new DashedLine(1); // 破線
82
-
83
-
84
42
 
85
43
  var table = new PdfPTable(1);
86
44
 
87
-
88
-
89
45
  var cell = new PdfPCell(new Phrase("sample"))
90
-
91
46
  {
92
-
93
47
  HorizontalAlignment = 1,
94
-
95
48
  VerticalAlignment = 1,
96
49
 
97
-
98
-
99
50
  Border = 0, // 通常のボーダーは引かない
100
-
101
51
  // 独自セルレンダラーを追加し描画 左,右,上,下
102
-
103
52
  CellEvent = new CustomBorder(null, solid, dotted, dashed),
104
-
105
53
  };
106
54
 
107
-
108
-
109
55
  table.AddCell(cell);
110
-
111
56
  doc.Add(table);
112
-
113
57
  }
114
58
 
59
+ System.Diagnostics.Process.Start("BorderTest.pdf");
60
+ }
61
+ }
115
62
 
63
+ internal interface ILineDash
64
+ {
65
+ void ApplyLineDash(PdfContentByte canvas);
66
+ }
116
67
 
117
- System.Diagnostics.Process.Start("BorderTest.pdf");
68
+ internal abstract class AbstractLineDash : ILineDash
69
+ {
70
+ private float lineWidth;
71
+ public AbstractLineDash(float lineWidth) => this.lineWidth = lineWidth;
72
+ public virtual void ApplyLineDash(PdfContentByte canvas) => canvas.SetLineWidth(lineWidth);
73
+ }
118
74
 
75
+ internal class SolidLine : AbstractLineDash
76
+ {
77
+ public SolidLine(float lineWidth) : base(lineWidth) { }
78
+ }
79
+
80
+ internal class DottedLine : AbstractLineDash
81
+ {
82
+ public DottedLine(float lineWidth) : base(lineWidth) { }
83
+ public override void ApplyLineDash(PdfContentByte canvas)
84
+ {
85
+ base.ApplyLineDash(canvas);
86
+ canvas.SetLineCap(PdfContentByte.LINE_CAP_ROUND);
87
+ canvas.SetLineDash(0, 4, 2);
88
+ }
89
+ }
90
+
91
+ internal class DashedLine : AbstractLineDash
92
+ {
93
+ public DashedLine(float lineWidth) : base(lineWidth) { }
94
+ public override void ApplyLineDash(PdfContentByte canvas)
95
+ {
96
+ base.ApplyLineDash(canvas);
97
+ canvas.SetLineDash(3, 3);
98
+ }
99
+ }
100
+
101
+ internal class CustomBorder : IPdfPCellEvent
102
+ {
103
+ protected ILineDash left;
104
+ protected ILineDash right;
105
+ protected ILineDash top;
106
+ protected ILineDash bottom;
107
+ public CustomBorder(ILineDash left, ILineDash right, ILineDash top, ILineDash bottom)
108
+ {
109
+ this.left = left;
110
+ this.right = right;
111
+ this.top = top;
112
+ this.bottom = bottom;
119
113
  }
120
114
 
115
+ public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)
116
+ {
117
+ var canvas = canvases[PdfPTable.LINECANVAS];
118
+ if(top != null)
119
+ {
120
+ canvas.SaveState();
121
+ top.ApplyLineDash(canvas);
122
+ canvas.MoveTo(position.Right, position.Top);
123
+ canvas.LineTo(position.Left, position.Top);
124
+ canvas.Stroke();
125
+ canvas.RestoreState();
126
+ }
127
+ if(bottom != null)
128
+ {
129
+ canvas.SaveState();
130
+ bottom.ApplyLineDash(canvas);
131
+ canvas.MoveTo(position.Right, position.Bottom);
132
+ canvas.LineTo(position.Left, position.Bottom);
133
+ canvas.Stroke();
134
+ canvas.RestoreState();
135
+ }
136
+ if(right != null)
137
+ {
138
+ canvas.SaveState();
139
+ right.ApplyLineDash(canvas);
140
+ canvas.MoveTo(position.Right, position.Top);
141
+ canvas.LineTo(position.Right, position.Bottom);
142
+ canvas.Stroke();
143
+ canvas.RestoreState();
144
+ }
145
+ if(left != null)
146
+ {
147
+ canvas.SaveState();
148
+ left.ApplyLineDash(canvas);
149
+ canvas.MoveTo(position.Left, position.Top);
150
+ canvas.LineTo(position.Left, position.Bottom);
151
+ canvas.Stroke();
152
+ canvas.RestoreState();
153
+ }
154
+ }
121
155
  }
122
-
123
-
124
-
125
- internal interface ILineDash
126
-
127
- {
128
-
129
- void ApplyLineDash(PdfContentByte canvas);
130
-
131
- }
132
-
133
-
134
-
135
- internal abstract class AbstractLineDash : ILineDash
136
-
137
- {
138
-
139
- private float lineWidth;
140
-
141
- public AbstractLineDash(float lineWidth) => this.lineWidth = lineWidth;
142
-
143
- public virtual void ApplyLineDash(PdfContentByte canvas) => canvas.SetLineWidth(lineWidth);
144
-
145
- }
146
-
147
-
148
-
149
- internal class SolidLine : AbstractLineDash
150
-
151
- {
152
-
153
- public SolidLine(float lineWidth) : base(lineWidth) { }
154
-
155
- }
156
-
157
-
158
-
159
- internal class DottedLine : AbstractLineDash
160
-
161
- {
162
-
163
- public DottedLine(float lineWidth) : base(lineWidth) { }
164
-
165
- public override void ApplyLineDash(PdfContentByte canvas)
166
-
167
- {
168
-
169
- base.ApplyLineDash(canvas);
170
-
171
- canvas.SetLineCap(PdfContentByte.LINE_CAP_ROUND);
172
-
173
- canvas.SetLineDash(0, 4, 2);
174
-
175
- }
176
-
177
- }
178
-
179
-
180
-
181
- internal class DashedLine : AbstractLineDash
182
-
183
- {
184
-
185
- public DashedLine(float lineWidth) : base(lineWidth) { }
186
-
187
- public override void ApplyLineDash(PdfContentByte canvas)
188
-
189
- {
190
-
191
- base.ApplyLineDash(canvas);
192
-
193
- canvas.SetLineDash(3, 3);
194
-
195
- }
196
-
197
- }
198
-
199
-
200
-
201
- internal class CustomBorder : IPdfPCellEvent
202
-
203
- {
204
-
205
- protected ILineDash left;
206
-
207
- protected ILineDash right;
208
-
209
- protected ILineDash top;
210
-
211
- protected ILineDash bottom;
212
-
213
- public CustomBorder(ILineDash left, ILineDash right, ILineDash top, ILineDash bottom)
214
-
215
- {
216
-
217
- this.left = left;
218
-
219
- this.right = right;
220
-
221
- this.top = top;
222
-
223
- this.bottom = bottom;
224
-
225
- }
226
-
227
-
228
-
229
- public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)
230
-
231
- {
232
-
233
- var canvas = canvases[PdfPTable.LINECANVAS];
234
-
235
- if(top != null)
236
-
237
- {
238
-
239
- canvas.SaveState();
240
-
241
- top.ApplyLineDash(canvas);
242
-
243
- canvas.MoveTo(position.Right, position.Top);
244
-
245
- canvas.LineTo(position.Left, position.Top);
246
-
247
- canvas.Stroke();
248
-
249
- canvas.RestoreState();
250
-
251
- }
252
-
253
- if(bottom != null)
254
-
255
- {
256
-
257
- canvas.SaveState();
258
-
259
- bottom.ApplyLineDash(canvas);
260
-
261
- canvas.MoveTo(position.Right, position.Bottom);
262
-
263
- canvas.LineTo(position.Left, position.Bottom);
264
-
265
- canvas.Stroke();
266
-
267
- canvas.RestoreState();
268
-
269
- }
270
-
271
- if(right != null)
272
-
273
- {
274
-
275
- canvas.SaveState();
276
-
277
- right.ApplyLineDash(canvas);
278
-
279
- canvas.MoveTo(position.Right, position.Top);
280
-
281
- canvas.LineTo(position.Right, position.Bottom);
282
-
283
- canvas.Stroke();
284
-
285
- canvas.RestoreState();
286
-
287
- }
288
-
289
- if(left != null)
290
-
291
- {
292
-
293
- canvas.SaveState();
294
-
295
- left.ApplyLineDash(canvas);
296
-
297
- canvas.MoveTo(position.Left, position.Top);
298
-
299
- canvas.LineTo(position.Left, position.Bottom);
300
-
301
- canvas.Stroke();
302
-
303
- canvas.RestoreState();
304
-
305
- }
306
-
307
- }
308
-
309
- }
310
-
311
156
  }
312
-
313
157
  ```
314
158
 
315
-
316
-
317
159
  ![pdf画像](970fd23adb9283d310396b2b0354a7e7.png)