質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

PDF

PDF(Portable Document Format)とはISOによって国際標準として制定されている電子ドキュメント用の拡張子です。

.NET Framework

.NET Framework は、Microsoft Windowsのオペレーティングシステムのために開発されたソフトウェア開発環境/実行環境です。多くのプログラミング言語をサポートしています。

Q&A

解決済

1回答

2684閲覧

iTextSharpでテーブルの罫線が2重に表示されるようにしたいです。(c#)

u10

総合スコア1

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

PDF

PDF(Portable Document Format)とはISOによって国際標準として制定されている電子ドキュメント用の拡張子です。

.NET Framework

.NET Framework は、Microsoft Windowsのオペレーティングシステムのために開発されたソフトウェア開発環境/実行環境です。多くのプログラミング言語をサポートしています。

1グッド

0クリップ

投稿2020/09/24 08:33

前提・実現したいこと

PDFを出力したときにテーブルの罫線を2重に表示されるようにしたいです。

発生している問題・エラーメッセージ

該当のソースコード

試したこと

PdfPCellクラスのプロパティで線の色や、セルの中身の色などは変更できることが確認できたのですが、2重線として表示する方法がわかりませんでした。

どなたかほかの方法を教えていただければ幸いです。
よろしくお願い致します。

補足情報(FW/ツールのバージョンなど)

使用ツールのバージョン
VisualStudio2019 16.7.3
.NET Framework 4.8

TN8001👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

TN8001

2020/09/24 10:10

タイミングが絶妙すぎるのですが、 [C# - iTextSharpで,テーブルの線を点線にしたいです。(C#)|teratail](https://teratail.com/questions/292675 Fernandinhoさんとは別人なんですか? [利用規約|teratail(テラテイル)](https://teratail.com/legal 第7条(禁止事項) (11) 複数のユーザーIDを1人で保有する行為
TN8001

2020/09/24 10:42

Fernandinhoさんとお友達ということを聞きました。失礼しました。
guest

回答1

0

ベストアンサー

C# - iTextSharpで,テーブルの線を点線にしたいです。(C#)|teratail
の使いまわしですが、上下左右別々に設定できて自由度が高いのでそのまま採用します。

要は2回、線を引けばいいのです。

cs

1using System.IO; 2using iTextSharp.text; 3using iTextSharp.text.pdf; 4 5namespace Questions292675 6{ 7 internal class Program 8 { 9 private static void Main() 10 { 11 using(var doc = new Document(PageSize.A4)) 12 { 13 PdfWriter.GetInstance(doc, new FileStream("BorderTest.pdf", FileMode.Create)); 14 doc.Open(); 15 16 var solid = new SolidLine(1); // 普通の 17 var dotted = new DottedLine(1); // 点々 18 var dashed = new DashedLine(1); // 破線 19 20 var table = new PdfPTable(3); 21 22 var cell = new PdfPCell(new Phrase("sample")) 23 { 24 HorizontalAlignment = 1, 25 VerticalAlignment = 1, 26 27 Border = 0, // 通常のボーダーは引かない 28 // 独自セルレンダラーを追加し描画 左,右,上,下 29 CellEvent = new CustomBorder2(solid, solid, solid, solid), 30 Padding = 5, // 内側に2本目が来るので適当に開ける 31 PaddingBottom = 10, 32 }; 33 34 table.AddCell(cell); 35 table.AddCell(cell); 36 table.AddCell(cell); 37 doc.Add(table); 38 } 39 40 System.Diagnostics.Process.Start("BorderTest.pdf"); 41 } 42 } 43 44 internal interface ILineDash 45 { 46 void ApplyLineDash(PdfContentByte canvas); 47 } 48 49 internal abstract class AbstractLineDash : ILineDash 50 { 51 private float lineWidth; 52 public AbstractLineDash(float lineWidth) => this.lineWidth = lineWidth; 53 public virtual void ApplyLineDash(PdfContentByte canvas) => canvas.SetLineWidth(lineWidth); 54 } 55 56 internal class SolidLine : AbstractLineDash 57 { 58 public SolidLine(float lineWidth) : base(lineWidth) { } 59 } 60 61 internal class DottedLine : AbstractLineDash 62 { 63 public DottedLine(float lineWidth) : base(lineWidth) { } 64 public override void ApplyLineDash(PdfContentByte canvas) 65 { 66 base.ApplyLineDash(canvas); 67 canvas.SetLineCap(PdfContentByte.LINE_CAP_ROUND); 68 canvas.SetLineDash(0, 4, 2); 69 } 70 } 71 72 internal class DashedLine : AbstractLineDash 73 { 74 public DashedLine(float lineWidth) : base(lineWidth) { } 75 public override void ApplyLineDash(PdfContentByte canvas) 76 { 77 base.ApplyLineDash(canvas); 78 canvas.SetLineDash(3, 3); 79 } 80 } 81 82 internal class CustomBorder2 : IPdfPCellEvent 83 { 84 protected ILineDash left; 85 protected ILineDash right; 86 protected ILineDash top; 87 protected ILineDash bottom; 88 89 private int offset = 5; // 線の間隔 90 public CustomBorder2(ILineDash left, ILineDash right, ILineDash top, ILineDash bottom) 91 { 92 this.left = left; 93 this.right = right; 94 this.top = top; 95 this.bottom = bottom; 96 } 97 98 public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) 99 { 100 // position.Top > position.Bottom になっていたので直観とは逆のオフセットになっている 101 // (左下が0の座標系?常にそうなのかは調べていない) 102 var canvas = canvases[PdfPTable.LINECANVAS]; 103 if(top != null) // 上の線 104 { 105 canvas.SaveState(); 106 top.ApplyLineDash(canvas); 107 canvas.MoveTo(position.Right, position.Top); 108 canvas.LineTo(position.Left, position.Top); 109 canvas.Stroke(); 110 canvas.MoveTo(position.Right - offset, position.Top - offset); 111 canvas.LineTo(position.Left + offset, position.Top - offset); 112 canvas.Stroke(); 113 canvas.RestoreState(); 114 } 115 if(bottom != null) // 下の線 116 { 117 canvas.SaveState(); 118 bottom.ApplyLineDash(canvas); 119 canvas.MoveTo(position.Right, position.Bottom); 120 canvas.LineTo(position.Left, position.Bottom); 121 canvas.Stroke(); 122 canvas.MoveTo(position.Right - offset, position.Bottom + offset); 123 canvas.LineTo(position.Left + offset, position.Bottom + offset); 124 canvas.Stroke(); 125 canvas.RestoreState(); 126 } 127 if(right != null) // 右の線 128 { 129 canvas.SaveState(); 130 right.ApplyLineDash(canvas); 131 canvas.MoveTo(position.Right, position.Top); 132 canvas.LineTo(position.Right, position.Bottom); 133 canvas.Stroke(); 134 canvas.MoveTo(position.Right - offset, position.Top - offset); 135 canvas.LineTo(position.Right - offset, position.Bottom + offset); 136 canvas.Stroke(); 137 canvas.RestoreState(); 138 } 139 if(left != null) // 左の線 140 { 141 canvas.SaveState(); 142 left.ApplyLineDash(canvas); 143 canvas.MoveTo(position.Left, position.Top); 144 canvas.LineTo(position.Left, position.Bottom); 145 canvas.Stroke(); 146 canvas.MoveTo(position.Left + offset, position.Top - offset); 147 canvas.LineTo(position.Left + offset, position.Bottom + offset); 148 canvas.Stroke(); 149 canvas.RestoreState(); 150 } 151 } 152 } 153}

pdf画像

投稿2020/09/24 10:50

編集2023/07/23 06:47
TN8001

総合スコア9317

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

u10

2020/09/25 01:16

ご回答ありがとうございます。 無事2重線を引くことができました。 本当にありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問