Itext7 在iText 7中,如何在一行中加粗一个单词?

Itext7 在iText 7中,如何在一行中加粗一个单词?,itext7,typography,Itext7,Typography,我可以使用iText 7设置文本粗体,如下所示: parExecSummHeader2.Add(new Text(subj).SetBold()); …但当我尝试将“普通”(非粗体)文本位与粗体部分组合时,它不起作用。我有这个,它打印的行都是“常规”(没有粗体): …但希望将计算值设置为粗体。我尝试了这两种方法: parExecSummHeader2.Add("Average words per sentence (the general average is 15 - 20): &

我可以使用iText 7设置文本粗体,如下所示:

parExecSummHeader2.Add(new Text(subj).SetBold());
…但当我尝试将“普通”(非粗体)文本位与粗体部分组合时,它不起作用。我有这个,它打印的行都是“常规”(没有粗体):

…但希望将计算值设置为粗体。我尝试了这两种方法:

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));
你有一些更详细的例子

对于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);
从这里开始,您可以尝试将其组合使用。

对于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));
你有一些更详细的例子

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

较短的选项,由于使用粗体模拟而不是正确的粗体字体,因此可能导致文本呈现质量不理想:

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

BaseColor无法识别。BaseColor无法识别。感谢您的赏金!你当然不必这么做,但这仍然很愉快:)@AlexeySubach:不用担心;我喜欢慷慨大方地施舍,部分原因是我会因此获得声誉,人们会更愿意回答。谢谢你的施舍!你当然不必这么做,但这仍然很愉快:)@AlexeySubach:不用担心;我喜欢慷慨大方地施舍,这在一定程度上是为了让我获得这样的名声,人们会更愿意回答。