C# 使用 iTextSharp 将句子中的单个单词加粗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10213231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:54:59  来源:igfitidea点击:

Bold a single word within a sentence with iTextSharp

c#itextsharp

提问by Neeraj

Is it possible to bold a single word within a sentence with iTextSharp? I am trying to bold several individual words without having to break the string into individual phrases.

是否可以使用 iTextSharp 将句子中的单个单词加粗?我正在尝试将几个单独的单词加粗,而不必将字符串分成单独的短语。

I want to this type of out put

我想要这种类型的输出

Eg:REASON(S) FOR CANCELLATION:See Statutory reason(s) designated by Code No(s) 1 on the reverse side hereof.

例如:取消的原因请参阅本背面代码 1 指定的法定原因。

My actual output is below

我的实际输出如下

Eg:REASON(S) FOR CANCELLATION: See Statutory reason(s) designated by Code No(s) 1 on the reverse side hereof.

例如:取消的原因:请参阅本协议背面代码 1 指定的法定原因。

Code

代码

    pdftb4 = new PdfPTable(1);
    pdftb4.WidthPercentage = 100;
    width = new float[1];
    width[0] = 0.7F;
    pdftb4.SetWidths(width);

  pdfcel4 = new PdfPCell(new Phrase("\n REASON(S) FOR CANCELLATION: See Statutoryreason(s) designated by Code No(s) 1 on the reverse side hereof", docBlackFont10));
    pdfcel4.Border = 0;
    pdfcel4.HorizontalAlignment = Element.ALIGN_LEFT;
    pdftb4.AddCell(pdfcel4);
   objDocument.Add(pdftb4);

somebody please help me

有人请帮助我

采纳答案by Chris Haas

The way to accomplish what you are trying is with Chunks. A simple example is:

完成您正在尝试的方法的方法是使用Chunks。一个简单的例子是:

var normalFont = FontFactory.GetFont(FontFactory.HELVETICA, 12);
var boldFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12);

var phrase = new Phrase();
phrase.Add(new Chunk("REASON(S) FOR CANCELLATION:", boldFont));
phrase.Add(new Chunk(" See Statutoryreason(s) designated by Code No(s) 1 on the reverse side hereof", normalFont));

回答by richk

Maybe this link Bolding with Rich Text Values in iTextSharpwill help?

也许这个链接在 iTextSharp 中使用富文本值加粗会有所帮助?

Not sure if it fits your scenario completely but might get you where you need to go.

不确定它是否完全适合您的场景,但可能会让您到达需要去的地方。

回答by ChrisJL

Can also create font like

也可以创建字体

Font verdanaBold = FontFactory.GetFont("Verdana", 7f, Font.BOLD);