如何在 iText 7 的一行中加粗一个单词?
How can I Bold one Word in a Line in iText 7?
我可以像这样使用 iText 7 将文本设置为粗体:
parExecSummHeader2.Add(new Text(subj).SetBold());
...但是当我尝试将“正常”(非粗体)文本位与粗体部分组合时,它不起作用。我有这个,打印所有“常规”行(无粗体):
parExecSummHeader2.Add("Average words per sentence (the general average is 15 - 20): " + Math.Round(WordsPerSentenceInDoc, 2).ToString());
...但想将计算值设为粗体。这两个我都试过了:
parExecSummHeader2.Add("Average words per sentence (the general average is 15 - 20): ");
parExecSummHeader2.Add(new Text(Math.Round(WordsPerSentenceInDoc, 2).ToString().SetBold()));
...还有这个:
parExecSummHeader2.Add("Average words per sentence (the general average is 15 - 20): ");
string mathval = Math.Round(WordsPerSentenceInDoc, 2).ToString();
parExecSummHeader2.Add(new Text(mathval.SetBold()));
...但他们都不会编译,抱怨,“错误 CS1061 'string' 不包含 'SetBold' 的定义并且没有可访问的扩展方法 'SetBold' 接受第一个可以找到 'string' 类型的参数
对于 iText 7:
public static final Font HELVETICA_BOLD =
new Font(FontFamily.HELVETICA, 12, Font.BOLD, BaseColor.BLUE);
new Text("MyText").setFontColor(Color.BLUE)
.setFont(PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD));
你有一些更详细的例子here
对于 iText 5:
public const string DEFAULT_FONT_FAMILY = "Arial";
public static Font SmallFontBold
{
get
{
BaseColor black = new BaseColor(0, 0, 0);
Font font = FontFactory.GetFont(DEFAULT_FONT_FAMILY, 10, Font.BOLD, black);
return font;
}//get
}//SmallFontBold
...
Phrase aPh = new Phrase("My Bold", SmallFontBold);
从这里开始,您可以尝试结合使用它。
较短的选项可能会导致文本呈现质量不完美,因为使用粗体模拟而不是正确的粗体字体:
Paragraph parExecSummHeader2 = new Paragraph();
parExecSummHeader2.Add("Average words per sentence (the general average is 15 - 20): ");
parExecSummHeader2.Add(new Text("123").SetBold());
具有更多代码但输出质量更好的选项,因为使用了正确的粗体字体:
PdfFont boldFont = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD);
Paragraph parExecSummHeader2 = new Paragraph();
parExecSummHeader2.Add("Average words per sentence (the general average is 15 - 20): ");
parExecSummHeader2.Add(new Text("123").SetFont(boldFont));
我可以像这样使用 iText 7 将文本设置为粗体:
parExecSummHeader2.Add(new Text(subj).SetBold());
...但是当我尝试将“正常”(非粗体)文本位与粗体部分组合时,它不起作用。我有这个,打印所有“常规”行(无粗体):
parExecSummHeader2.Add("Average words per sentence (the general average is 15 - 20): " + Math.Round(WordsPerSentenceInDoc, 2).ToString());
...但想将计算值设为粗体。这两个我都试过了:
parExecSummHeader2.Add("Average words per sentence (the general average is 15 - 20): ");
parExecSummHeader2.Add(new Text(Math.Round(WordsPerSentenceInDoc, 2).ToString().SetBold()));
...还有这个:
parExecSummHeader2.Add("Average words per sentence (the general average is 15 - 20): ");
string mathval = Math.Round(WordsPerSentenceInDoc, 2).ToString();
parExecSummHeader2.Add(new Text(mathval.SetBold()));
...但他们都不会编译,抱怨,“错误 CS1061 'string' 不包含 'SetBold' 的定义并且没有可访问的扩展方法 'SetBold' 接受第一个可以找到 'string' 类型的参数
对于 iText 7:
public static final Font HELVETICA_BOLD =
new Font(FontFamily.HELVETICA, 12, Font.BOLD, BaseColor.BLUE);
new Text("MyText").setFontColor(Color.BLUE)
.setFont(PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD));
你有一些更详细的例子here
对于 iText 5:
public const string DEFAULT_FONT_FAMILY = "Arial";
public static Font SmallFontBold
{
get
{
BaseColor black = new BaseColor(0, 0, 0);
Font font = FontFactory.GetFont(DEFAULT_FONT_FAMILY, 10, Font.BOLD, black);
return font;
}//get
}//SmallFontBold
...
Phrase aPh = new Phrase("My Bold", SmallFontBold);
从这里开始,您可以尝试结合使用它。
较短的选项可能会导致文本呈现质量不完美,因为使用粗体模拟而不是正确的粗体字体:
Paragraph parExecSummHeader2 = new Paragraph();
parExecSummHeader2.Add("Average words per sentence (the general average is 15 - 20): ");
parExecSummHeader2.Add(new Text("123").SetBold());
具有更多代码但输出质量更好的选项,因为使用了正确的粗体字体:
PdfFont boldFont = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD);
Paragraph parExecSummHeader2 = new Paragraph();
parExecSummHeader2.Add("Average words per sentence (the general average is 15 - 20): ");
parExecSummHeader2.Add(new Text("123").SetFont(boldFont));