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

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

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

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

PDF

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

.NET Framework 4.0

Microsoft Windows用のソフトウェア開発環境/実行環境である .NET Frameworkの4番目のメジャーバージョンです。

Visual Studio 2013

Microsoft Visual Studio 2013は、Microsoftによる統合開発環境(IDE)であり、多種多様なプログラミング言語に対応しています。 Visual Studio 2012の次のバージョンです

Q&A

解決済

1回答

4032閲覧

iTextSharpで,テーブルの線を点線にしたいです。(C#)

Fernandinho

総合スコア4

C#

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

PDF

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

.NET Framework 4.0

Microsoft Windows用のソフトウェア開発環境/実行環境である .NET Frameworkの4番目のメジャーバージョンです。

Visual Studio 2013

Microsoft Visual Studio 2013は、Microsoftによる統合開発環境(IDE)であり、多種多様なプログラミング言語に対応しています。 Visual Studio 2012の次のバージョンです

1グッド

0クリップ

投稿2020/09/18 05:11

前提・実現したいこと

iTextSharpで、C#を使ってPDFファイルを作成しています。
テーブルを使うのですが、セルのボーダーを点線にするにはどうすればよいでしょうか?
調べてみたり人に聞いたりしたのですが、答えが分からず本当に困っています。
どなたかよろしくお願い致します。
■■な機能を実装中に以下のエラーメッセージが発生しました。

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

下のコードの'cell.BorderStyle = 3;'の部分でこれが出ます。 'PdfPCell'に'BorderStyle'の定義が含まれておらず、型'PdfPCell'の最初の引数を受け付けるアクセス可能な拡張メソッド'BorderStyle'が見つかりませんでした。usingディレクティブまたはアセンブリ参照が不足していないことを確認してください。

該当のソースコード

C#

1PdfPTable tbl = new PdfPTable(1); 2 3PdfPCell cell = new PdfPCell(new Phrase("sample")); 4cell.BorderWidthRight = 1.0f; 5cell.BorderWidthLeft = 1.0f; 6cell.BorderWidthTop = 1.0f; 7cell.BorderWidthBottom = 1.0f; 8 9cell.HorizontalAlignment = 1; 10cell.VerticalAlignment = 1; 11 12cell.BorderStyle = 3; 13 14tbl.AddCell(cell); 15 16

試したこと

cell.BorderStyle = 3;
https://docs.microsoft.com/ja-jp/dotnet/api/system.web.ui.webcontrols.borderstyle?view=netframework-4.8
このページには、BorderStyle列挙型のフィールドで、3は破線であると書かれていました。
しかし、そもそもPdfPCellクラスにBorderStyleというメソッドがないので今回の様なエラーが出たのだと思われます。

この方法自体が間違っていて、他の方法があるのだと思うのですが、
自分の力では分かりませんでした。
どなたかヒントでもいいので、与えてくだされば幸いです。

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

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

TN8001👍を押しています

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

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

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

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

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

guest

回答1

0

ベストアンサー

注意)iTextSharpを初めて使ったので間違いがあるかもしれません。

iTextSharpは「NuGet Gallery | iTextSharp 5.5.13.2」こちらでいいでしょうか?

PLEASE NOTE: iTextSharp is EOL, and has been replaced by iText 7. Only security fixes will be added
注:iTextSharpはEOLであり、iText 7に置き換えられました。セキュリティ修正のみが追加されます

と移行を促されているようですが。

このページには、BorderStyle列挙型のフィールドで、3は破線であると書かれていました。

見るところが根本的に違います。なぜそれが使えると思ったのかが不思議です。

見るのでしたらこちらです(iTextSharpはiText5系のようで、Java版しかありませんがヒントにはなります)
iText pdf library API documentation for Java and .Net

本題のボーダーですが、どうやら自分で描画するしかないようです^^;
しかし本家サイトにサンプルがありますので、CustomBorder3をベースにざっと移してみました。
How to define different border types for a single cell?

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(1); 21 22 var cell = new PdfPCell(new Phrase("sample")) 23 { 24 HorizontalAlignment = 1, 25 VerticalAlignment = 1, 26 27 Border = 0, // 通常のボーダーは引かない 28 // 独自セルレンダラーを追加し描画 左,右,上,下 29 CellEvent = new CustomBorder(null, solid, dotted, dashed), 30 }; 31 32 table.AddCell(cell); 33 doc.Add(table); 34 } 35 36 System.Diagnostics.Process.Start("BorderTest.pdf"); 37 } 38 } 39 40 internal interface ILineDash 41 { 42 void ApplyLineDash(PdfContentByte canvas); 43 } 44 45 internal abstract class AbstractLineDash : ILineDash 46 { 47 private float lineWidth; 48 public AbstractLineDash(float lineWidth) => this.lineWidth = lineWidth; 49 public virtual void ApplyLineDash(PdfContentByte canvas) => canvas.SetLineWidth(lineWidth); 50 } 51 52 internal class SolidLine : AbstractLineDash 53 { 54 public SolidLine(float lineWidth) : base(lineWidth) { } 55 } 56 57 internal class DottedLine : AbstractLineDash 58 { 59 public DottedLine(float lineWidth) : base(lineWidth) { } 60 public override void ApplyLineDash(PdfContentByte canvas) 61 { 62 base.ApplyLineDash(canvas); 63 canvas.SetLineCap(PdfContentByte.LINE_CAP_ROUND); 64 canvas.SetLineDash(0, 4, 2); 65 } 66 } 67 68 internal class DashedLine : AbstractLineDash 69 { 70 public DashedLine(float lineWidth) : base(lineWidth) { } 71 public override void ApplyLineDash(PdfContentByte canvas) 72 { 73 base.ApplyLineDash(canvas); 74 canvas.SetLineDash(3, 3); 75 } 76 } 77 78 internal class CustomBorder : IPdfPCellEvent 79 { 80 protected ILineDash left; 81 protected ILineDash right; 82 protected ILineDash top; 83 protected ILineDash bottom; 84 public CustomBorder(ILineDash left, ILineDash right, ILineDash top, ILineDash bottom) 85 { 86 this.left = left; 87 this.right = right; 88 this.top = top; 89 this.bottom = bottom; 90 } 91 92 public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) 93 { 94 var canvas = canvases[PdfPTable.LINECANVAS]; 95 if(top != null) 96 { 97 canvas.SaveState(); 98 top.ApplyLineDash(canvas); 99 canvas.MoveTo(position.Right, position.Top); 100 canvas.LineTo(position.Left, position.Top); 101 canvas.Stroke(); 102 canvas.RestoreState(); 103 } 104 if(bottom != null) 105 { 106 canvas.SaveState(); 107 bottom.ApplyLineDash(canvas); 108 canvas.MoveTo(position.Right, position.Bottom); 109 canvas.LineTo(position.Left, position.Bottom); 110 canvas.Stroke(); 111 canvas.RestoreState(); 112 } 113 if(right != null) 114 { 115 canvas.SaveState(); 116 right.ApplyLineDash(canvas); 117 canvas.MoveTo(position.Right, position.Top); 118 canvas.LineTo(position.Right, position.Bottom); 119 canvas.Stroke(); 120 canvas.RestoreState(); 121 } 122 if(left != null) 123 { 124 canvas.SaveState(); 125 left.ApplyLineDash(canvas); 126 canvas.MoveTo(position.Left, position.Top); 127 canvas.LineTo(position.Left, position.Bottom); 128 canvas.Stroke(); 129 canvas.RestoreState(); 130 } 131 } 132 } 133}

pdf画像

投稿2020/09/19 08:10

編集2023/08/12 10:02
TN8001

総合スコア9321

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

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

Fernandinho

2020/09/24 08:21

書いていただいたコードで点線を引くことができました。 本家のサイトが存在していたことは全く知りませんでした。今後参照していきたいと思います。 詰まっていたので、本当に助かりました。 この度は、誠にありがとうございました。
Fernandinho

2020/09/24 08:46

重ねて質問してしまい申し訳無いのですが、 二重線はどの様にすれば引けるかご存知ですか? 見よう見まねでDoubledLineクラスを作ってみましたが、PdfContentByteクラスに二重線を引けそうなメソッドがありませんでした。 これを使うのではないか、というヒントいただけないでしょうか? よろしくお願いします。
TN8001

2020/09/24 10:10

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

2020/09/24 10:31

u10さんは私の友人で、私の別アカウントではありません。 同じ内容で詰まっていたので、早期解決のため2人で質問した次第です。
TN8001

2020/09/24 10:40

お友達ということですか、了解しました。 ではあちらのほうに回答するのでご確認ください。
Fernandinho

2020/09/24 10:46

本当に助かります。 ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問