我使用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 });
我们可以为标题和描述设置不同的字体颜色,但只能使用单个单元格(即不创建新表格)?
在这件事上的任何帮助将不胜感激.
解决方法
您要做的是创建2个Chunk对象,然后将它们组合成1个短语,您将添加到单元格.
var blackListTextFont = FontFactory.GetFont("Arial",28,Color.BLACK); var redListTextFont = FontFactory.GetFont("Arial",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));
看看http://www.mikesdotnetting.com/Article/82/iTextSharp-Adding-Text-with-Chunks-Phrases-and-Paragraphs