ITextSharp multiple lines in PdfPCell one below the other

I am using iTextSharp to create a table in a PDF document. I need several rows inside a table cell so that they appear under another as follows:

First line text
   Second Line Text
   Third Line Text
Fourth line text

Several times with an extra line:

First line text

   Second Line Text
   Third Line Text
Fourth line text

I tried several approaches, with paragraphs, pieces, phrases, conducted research on the Internet, but still can not get this result. Please help. Also, how to make the columns dynamically change the width of the content? (not wrapping) Thank you

+7
source share
3 answers

, . , :

var p = new Paragraph();
p.Add("First line text\n");
p.Add("    Second line text\n");
p.Add("    Third line text\n");
p.Add("Fourth line text\n");
myTable.AddCell(p);

, :

var subTable = new PdfPTable(new float[] { 10, 100 });                        
subTable.AddCell(new PdfPCell(new Phrase("First line text")) { Colspan = 2, Border = 0 });
subTable.AddCell(new PdfPCell() { Border = 0 });
subTable.AddCell(new PdfPCell(new Phrase("Second line text")) {  Border = 0 });
subTable.AddCell(new PdfPCell() { Border = 0 });
subTable.AddCell(new PdfPCell(new Phrase("Third line text")) { Border = 0 });
subTable.AddCell(new PdfPCell(new Phrase("Fourth line text")) { Colspan = 2, Border = 0 });
myTable.AddCell(subTable);
+16

, , , :

Font myFont = FontFactory.GetFont("Arial", 8, Font.NORMAL);

string line1 = "First line of text" + "\n";                     
string line2= "Second line of text" + "\n";
string line3= "   Third Line of text";

Paragraph p1 = new Paragraph();
Phrase ph1 = new Phrase(line1, myFont);
Phrase ph2 = new Phrase(line2, myFont);
Phrase ph3 = new Phrase(line3, myFont);

p1.Add(ph1);
p1.Add(ph2);
p1.Add(ph3);

PdfPCell mycell = new PdfPCell(p1);
+6

.

var xstring = "Your first line \n Your 2nd line";
Phrase p = new Phrase();
p.Add(new Chunk(xstring, yourFontFace));

.

+1

Source: https://habr.com/ru/post/1528145/


All Articles