如何在单个String中使用常规和粗体? [英] How can I use regular and bold in a single String?

查看:276
本文介绍了如何在单个String中使用常规和粗体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 String ,它由一个常量部分和一个变量部分组成。
我希望变量在文本段落中使用常规字体进行格式化,而我希望常量部分是粗体。

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.

这是我的代码:

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

在这两种情况下,第一部分(NAME出生日期)应为粗体和变量部分( cc_cust_name cc_cust_dob )应该是常规的。

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.

推荐答案

现在你正在创建段落使用单一字体: fontsmallbold 。你想创建一个使用两种不同字体的段落

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

如您所见,我们创建一个段落内容NAME:使用字体粗体。然后我们将 Chunk 添加到段落,其中包含 CC_CUST_NAME font 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.

参见如何为itext中的单个字符串设置两种不同的颜色使用Itext将颜色应用于段落中的字符串,这是针对同一主题的两个问题。

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.

你也可以在 PdfPCell 的上下文中使用它,在这种情况下你创建一个短语使用两种字体:

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

这篇关于如何在单个String中使用常规和粗体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆