Question

I am using iTextSharp to create table in PDF document. I need several lines inside table cell to appear one under another like this:

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

Some times with extra line like this :

First line text

   Second Line Text
   Third Line Text
Fourth line text

I have tried several approaches, with Paragraphs, Chunks, Phrases, did research online but still can not get this result. Please help. Also, how to make columns to adjust width dynamically to content ? (not wrapping) Thank you

Was it helpful?

Solution

If you need to align at the text level you'll need to switch to a fixed-width font. But if you're just looking to indent you can just add spaces to new lines within a paragraph:

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);

You could also get complicated and use a sub-table if you need more control:

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);

OTHER TIPS

Though pretty tedious, but for setting font, following seem to work:

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);

You can also do it the following way ..

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

I text will notice the new line return code and render your phrase on two separate lines.

Your first line

Your 2nd line

Cheers

    #region .!!

    PdfPTable tbl_A = new PdfPTable(1);
    tbl_A.WidthPercentage = 98f;
    //float[] colWidthUnderTaking1 = { 1300 };
    //tblUnderTaking1.SetWidths(colWidthUnderTaking1);

    #region For Page Space
    PdfPCell cell_A;
    cell_A = new PdfPCell(new Phrase("  ", Smallspace));
    cell_A.HorizontalAlignment = 1;
    cell_A.BorderWidth = 0;
    cell_A.Colspan = 2;
    tbl_A.AddCell(cell_A);

    cell_A = new PdfPCell(new Phrase("  ", Smallspace));
    cell_A.HorizontalAlignment = 1;
    cell_A.BorderWidth = 0;
    cell_A.Colspan = 2;
    tbl_A.AddCell(cell_A);


    #endregion

    Chunk cMem = new Chunk("The Member   ", TableFontmini_ARBold8Nor);
    Chunk cName = new Chunk(dt.Rows[0]["EmpName"].ToString(), TableFontmini_ARBold10);
    Chunk cjoin = new Chunk("  Has joined On  ", TableFontmini_ARBold8Nor);
    Chunk cDOJ = new Chunk("   " + dt.Rows[0]["DOJPF"].ToString(), TableFontmini_ARBold10);
    Chunk chas = new Chunk("and has been alloted PF Member ID   ", TableFontmini_ARBold8Nor);
    Chunk cPF = new Chunk("   " + dt.Rows[0]["PFNo"].ToString(), TableFontmini_ARBold10);
    Phrase paira = new Phrase();
    paira.Add(cMem);
    paira.Add(cName);
    paira.Add(cjoin);
    paira.Add(cDOJ);
    paira.Add(chas);
    paira.Add(cPF);
    Paragraph pName = new Paragraph();
    pName.Add(paira);


    PdfPCell cell_A2 = new PdfPCell(pName);
    cell_A2.HorizontalAlignment = 0;/**Left=0,Centre=1,Right=2**/
    cell_A2.BorderWidth = 0;
    cell_A2.Colspan = 2;
    tbl_A.AddCell(cell_A2);
    doc.Add(tbl_A);
    #endregion
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top