iTextSharp PdfPCell中的多行一个在另一个下面

时间:2014-02-21 16:04:17

标签: c# asp.net-mvc-4 itextsharp

我正在使用iTextSharp在PDF文档中创建表格。我需要在表格单元格中的几行显示在另一个之下,如下所示:

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

有时会出现这样的额外行:

First line text

   Second Line Text
   Third Line Text
Fourth line text

我尝试了几种方法,包括Paragraphs,Chunks,Phrases,在网上做过研究,但仍然无法得到这个结果。请帮忙。 另外,如何使列动态调整宽度到内容? (不包装) 谢谢

4 个答案:

答案 0 :(得分:16)

如果您需要在文本级别对齐,则需要切换到固定宽度的字体。但是如果你只想缩进,你可以在段落中的新行中添加空格:

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

答案 1 :(得分:6)

虽然相当繁琐,但对于设置字体,以下似乎有效:

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

答案 2 :(得分:1)

您也可以通过以下方式进行..

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

我的文本会注意到新的行返回代码,并在两个单独的行上呈现您的短语。

你的第一行

你的第二行

干杯

答案 3 :(得分:0)

    #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