Joseph Goldberger
Joseph Goldberger

Reputation: 313

How to insert a line break in itext7 table cell

How can I insert a line break in itext7 table cell? here is my code,

PdfWriter writer = new PdfWriter(@"C:\Temp\test123.pdf");
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf, PageSize.LEGAL);
string msg = $"This is line 1{Environment.NewLine}This should be line 2, However it's not showing";
Table table = new Table(1, true);
Cell cell = new Cell().Add(new Paragraph(msg));
table.AddCell(cell);
document.Add(table);
document.Close();
Process.Start(@"C:\Temp\test123.pdf");

Upvotes: 3

Views: 8511

Answers (2)

Metro Smurf
Metro Smurf

Reputation: 38385

As of at least v7 itext now supports using new lines for line breaks:

var table = new Table( 1 );

var p = new Paragraph()
    .Add( "Line 1" )
    .Add( Environment.NewLine ) // or just "\r\n"
    .Add( "Line 2" );

table.AddCell( p );

Upvotes: 0

zc246
zc246

Reputation: 1514

You should generally use Paragraph instead of manually setting line breaks when using PDF generation tools like iText.

var cell = new Cell();
cell.Add(new Paragraph("This is line 1");
cell.Add(new Paragraph("This should be line 2, and it is!~");

Upvotes: 8

Related Questions