2012-06-15 8 views
10

मैं निम्नलिखित कोड के साथ iTextSharp.dll उपयोग कर रहा हूँ:iTextSharp - क्या एक ही सेल और पंक्ति के लिए एक अलग फ़ॉन्ट रंग सेट करना संभव है?

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

हम शीर्षक और विवरण के लिए अलग फ़ॉन्ट रंग सेट कर सकता हूँ, लेकिन केवल एकल कक्ष (यानी एक नया टेबल बनाए बिना) का उपयोग कर?

इस मामले में किसी भी मदद की सराहना की जाएगी।

उत्तर

16

आप क्या करना चाहते हैं 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

+0

समाधान ऊपर अच्छा लग रहा है, लेकिन काम नहीं करता:

यहाँ एक उदाहरण है जो मेरे लिए काम करता है। इस उदाहरण में, दोनों फोंट एक ही बाहर आए: var titleChunk = new Chunk ("UPRN:", _fntHeading9); var विवरणChunk = नया खंड (keyPropertyId.ToString(), _fntNormal9); var वाक्यांश = नया वाक्यांश (शीर्षकचंक); वाक्यांश। जोड़ें (विवरणChunk); तालिका। एडकेल (नया पीडीएफपीसेल (वाक्यांश)); –

+0

कोड निश्चित रूप से काम करता है, शायद फ़ॉन्ट ऑब्जेक्ट्स एक ही फ़ॉन्ट पर इंगित करते हैं? या खेल में एक और मुद्दा है। – TimS

+4

यह निश्चित रूप से काम नहीं किया। फ़ॉन्ट ऑब्जेक्ट्स अलग हैं। एक बोल्ड है, दूसरा सामान्य है। यह कोड काम करता है: सार्वजनिक स्थैतिक फ़ॉन्ट _fntNormal9 = FontFactory.GetFont ("एरियल", 9, फ़ॉन्ट.NORMAL); सार्वजनिक स्थैतिक फ़ॉन्ट _fntHeading9 = FontFactory.GetFont ("एरियल", 9, फ़ॉन्ट.BOLD); var वाक्यांश = नया वाक्यांश(); वाक्यांश। जोड़ें (नया वाक्यांश ("यूपीआरएन:", _fntHeading9)); वाक्यांश। जोड़ें (नया वाक्यांश (keyPropertyId.ToString(), _fntNormal9)); तालिका। एडकेल (नया पीडीएफपीसेल (वाक्यांश)); –

3

पर एक नज़र डालें एक 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); 
0

VB.net उपयोग के लिए निम्नलिखित समारोह

Public Function CreateFont(size As Integer, Optional style As Integer = iTextSharp.text.Font.BOLD) As iTextSharp.text.Font 
     Dim FontColour = New BaseColor(193, 36, 67) 'Color code 
     Return New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, size, style, FontColour) 
End Function 


Dim p As New Paragraph 
p.Add(New Paragraph("Your Sentence Here", CreateFont(12, iTextSharp.text.Font.BOLDITALIC))) 
pdfDoc.Add(p) 
+0

अच्छा। धन्यवाद !! –

0

चाल विभिन्न फ़ॉन्ट्स के साथ वाक्यांश (नहीं भाग) बनाने और इन्हें एक pa में जोड़ने के लिए है किराया वाक्यांश। जहां तक ​​मैं बता सकता हूं कि क्या आप विभिन्न फोंट के साथ भाग बनाते हैं और इन्हें एक वाक्यांश में जोड़ते हैं, तो अंतिम वाक्यांश में सभी पाठ एक ही फ़ॉन्ट के साथ प्रदर्शित होते हैं।

// 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); 
संबंधित मुद्दे

 संबंधित मुद्दे