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

后端 未结 1 1052
执念已碎
执念已碎 2021-01-19 03:34

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, wherea

相关标签:
1条回答
  • 2021-01-19 03:45

    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);
    
    0 讨论(0)
提交回复
热议问题