18

次のコードで 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 });

タイトルと説明に異なるフォントの色を設定できますが、単一のセルのみを使用します (つまり、新しいテーブルを作成しません)。

この問題で何か助けていただければ幸いです。

4

4 に答える 4

26

実行したいのは、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));

http://www.mikesdotnetting.com/Article/82/iTextSharp-Adding-Text-with-Chunks-Phrases-and-Paragraphsをご覧ください

于 2012-06-16T10:26:08.287 に答える
0

秘訣は、異なるフォントでフレーズ (チャンクではない) を作成し、これらを親フレーズに追加することです。異なるフォントでチャンクを作成し、これらをフレーズに追加すると、最終的なフレーズのすべてのテキストが同じフォントで表示されることがわかります。

これが私のために働く例です:

// create the font we'll use
var fNormal = FontFactory.GetFont("Helvetica", 10f);
fNormal.SetColor(0, 0, 0);

// add phrase containing link
var pFooter = new Phrase();

// add phrase to this containing text only
var footerStart = new Phrase("Visit ");
footerStart.Font = fNormal;
pFooter.Add(footerStart); 

// now create anchor and add with different font
string wolSiteUrl = "http://www.whateveryoulike.com";
Anchor wolWebSiteLink = new Anchor(wolSiteUrl, fNormal);
wolWebSiteLink.Reference = wolSiteUrl;
wolWebSiteLink.Font = fNormal;
wolWebSiteLink.Font.SetColor(242, 132, 0);
pFooter.Add(wolWebSiteLink);

// add text to go after this link
var footerEnd = new Phrase(" to view these results online.");
footerEnd.Font = fNormal;
pFooter.Add(footerEnd);
var paraFooter = new Paragraph(pFooter);

// add the phrase we've built up containing lots of little phrases to document
// (assume you have one of these ...)
doc.Add(paraFooter);
于 2017-10-18T13:10:55.653 に答える