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

Kar*_*lon 7 c# itextsharp asp.net-mvc-4

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

First line text
   Second Line Text
   Third Line Text
Fourth line text
Run Code Online (Sandbox Code Playgroud)

有时像这样额外的线:

First line text

   Second Line Text
   Third Line Text
Fourth line text
Run Code Online (Sandbox Code Playgroud)

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

Chr*_*aas 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);
Run Code Online (Sandbox Code Playgroud)

如果您需要更多控制,您也可能会变得复杂并使用子表:

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);
Run Code Online (Sandbox Code Playgroud)


Kar*_*lon 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);
Run Code Online (Sandbox Code Playgroud)