iTextSharp - 是否可以为同一个单元格和行设置不同的字体颜色?

Nic*_*k K 15 c# pdf asp.net itextsharp

我使用iTextSharp.dll与以下代码:

var Title = "This is title";
var Description = "This is description";

Innertable.AddCell(new PdfPCell(new Phrase(string.Format("{0} {1}", Title, Description.Trim()), listTextFont)) { BackgroundColor = new BaseColor(233, 244, 249), BorderWidth = 0, PaddingTop = 4, PaddingLeft = -240, PaddingBottom = 5, HorizontalAlignment = Element.ALIGN_LEFT });
Run Code Online (Sandbox Code Playgroud)

我们可以为标题和描述设置不同的字体颜色,但只使用单个单元格(即不创建新表格)?

任何有关此事的帮助将不胜感激.

Tim*_*imS 23

你想要做的是创建2个Chunk对象,然后将它们组合成1 Phrase,你将添加到单元格中.

var blackListTextFont = FontFactory.GetFont("Arial", 28, Color.BLACK);
var redListTextFont = FontFactory.GetFont("Arial", 28, Color.RED);

var titleChunk = new Chunk("Title", blackListTextFont);
var descriptionChunk = new Chunk("Description", redListTextFont);

var phrase = new Phrase(titleChunk);
phrase.Add(descriptionChunk);

table.AddCell(new PdfPCell(phrase));
Run Code Online (Sandbox Code Playgroud)

看看http://www.mikesdotnetting.com/Article/82/iTextSharp-Adding-Text-with-Chunks-Phrases-and-Paragraphs


Nal*_*ran 5

尝试这样在 pdf 单元格中设置不同的前景色:

var FontColour = new BaseColor(35, 31, 32);
var Calibri8 = FontFactory.GetFont("Calibri", 8, FontColour);

PdfPCell R3C2 = new PdfPCell(new Paragraph("Hello", Calibri8));
R3C2.BorderWidth = 0f;
R3C2.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
table.AddCell(R3C2);
Run Code Online (Sandbox Code Playgroud)