iTextSharp PdfPCell中的多行在另一行下(iTextSharp multiple lines in PdfPCell one under another)

编程入门 行业动态 更新时间:2024-10-19 09:32:22
iTextSharp PdfPCell中的多行在另一行下(iTextSharp multiple lines in PdfPCell one under another)

我正在使用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

我尝试了几种方法,用段落,大块,短语,在网上做了研究,但仍然无法得到这个结果。 请帮忙。 另外,如何让列动态调整宽度以适应内容? (不包装)谢谢

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

最满意答案

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

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

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

更多推荐

本文发布于:2023-08-06 14:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1450329.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PdfPCell   iTextSharp   lines   multiple

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!