iTextSharp PdfPCell中的多行一个在另一个下面 [英] iTextSharp multiple lines in PdfPCell one under another
问题描述
我正在使用iTextSharp在PDF文档中创建表格。我需要在表格单元格中出现一行,如下所示:
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
我尝试了几种方法,包括Paragraphs,Chunks,Phrases,在线研究但仍然可以没有得到这个结果。请帮忙。
另外,如何使列动态调整宽度到内容? (不包装)
谢谢
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
推荐答案
如果您需要在文本级别对齐,则需要切换到固定宽度的字体。但是如果你只想缩进,你可以在一个段落中为新行添加空格:
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);
这篇关于iTextSharp PdfPCell中的多行一个在另一个下面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!