Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use regular and bold in a single String?

Tags:

itext

itextpdf

I have a String that consists of a constant part and a variable part. I want the variable to be formatted using a regular font within the text paragraph, whereas I want the constant part to be bold.

This is my code:

String cc_cust_name = request.getParameter("CC_CUST_NAME");    
document.add(new Paragraph(" NAME  " + cc_cust_name, fontsmallbold));

My code for a cell in a table looks like this:

cell1 = new PdfPCell(new Phrase("Date of Birth" + cc_cust_dob ,fontsmallbold));

In both cases, the first part (" NAME " and "Date of Birth") should be bold and the variable part (cc_cust_name and cc_cust_dob) should be regular.

like image 764
nitin Avatar asked Aug 11 '15 12:08

nitin


People also ask

How do I make text bold in Itextsharp?

Font bold = Font font = new Font(FontFamily. HELVETICA, 12, Font. BOLD); Paragraph p = new Paragraph("NAME: ", bold);

How do I make part of a string bold in C#?

How do I make only stringA bold in C# code behind? var result = this. formaatedstring(Final, stringA); Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM.

How do you bold part of a string?

The bold() method creates a string that embeds a string in a <b> element ( <b>str</b> ), which causes a string to be displayed as bold.

How do I make part of a string bold in HTML?

To bold the text in HTML, use either the strong tag or the b (bold) tag. Browsers will bold the text inside both of these tags the same, but the strong tag indicates that the text is of particular importance or urgency. You can also bold text with the CSS font-weight property set to “bold.”


1 Answers

Right now you are creating a Paragraph using a single font: fontsmallbold. You want to create a Paragraph that uses two different fonts:

Font regular = new Font(FontFamily.HELVETICA, 12);
Font bold = Font font = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
Paragraph p = new Paragraph("NAME: ", bold);
p.add(new Chunk(CC_CUST_NAME, regular));

As you can see, we create a Paragraph with content "NAME: " that uses font bold. Then we add a Chunk to the Paragraph with CC_CUST_NAME in font regular.

See also How to set two different colors for a single string in itext and Applying color to Strings in Paragraph using Itext which are two questions that address the same topic.

You can also use this in the context of a PdfPCell in which case you create a Phrase that uses two fonts:

Font regular = new Font(FontFamily.HELVETICA, 12);
Font bold = Font font = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
Phrase p = new Phrase("NAME: ", bold);
p.add(new Chunk(CC_CUST_NAME, regular));
PdfPCell cell = new PdfPCell(p);
like image 68
Bruno Lowagie Avatar answered Oct 05 '22 19:10

Bruno Lowagie