在Pdf列表中加粗一些文字

时间:2016-06-15 07:40:32

标签: c# asp.net pdf itextsharp

我在列表中显示一些内容,以便在pdf文件中显示。

每件事情都很好,但现在我想要一个列表项中的一些文字应该是大胆的。

例如:

这是 ListItem Bold 文字。

我该怎么做?

这是我的代码:

 List lst_note = new List(List.ORDERED);

 lst_note.IndentationLeft = 10f;
 lst_note.Add(new iTextSharp.text.ListItem("This single **word** should be Bold", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 10)));

 disclaimer.Add(lst_note);

修改

我试过这个:

    Font bold  = new Font(FontFactory.GetFont(FontFactory.TIMES_BOLD, 10, Font.BOLD));
   lst_terms.Add(new iTextSharp.text.ListItem("Some Text "+ new Chunk("this should bold", bold), FontFactory.GetFont(FontFactory.TIMES_ROMAN, 10)));

但这没效果

2 个答案:

答案 0 :(得分:2)

您可以使用ParagraphChunks执行此操作,如下所示:

Chunk c1 = new Chunk("This single", new Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL, BaseColor.BLACK)));
            Chunk c2 = new Chunk("word", new Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD, BaseColor.BLACK)));
            Chunk c3 = new Chunk("should be Bold", new Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL, BaseColor.BLACK)));

            Paragraph p2 = new Paragraph();
            p2.Add(c1);
            p2.Add(c2);
            p2.Add(c3);

 List lst_note = new List(List.ORDERED);

 lst_note.IndentationLeft = 10f;
 lst_note.Add(new iTextSharp.text.ListItem(p2);

 disclaimer.Add(lst_note);

答案 1 :(得分:1)

请看一下这个问题的答案:How can I use regular and bold in a single String?

答案是关于Paragraph,但它也适用于ListItem,因为ListItemParagraph的子类:

Font regular = new Font(FontFamily.HELVETICA, 12);
Font bold = Font font = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
ListItem li = new ListItem("NAME: ", bold);
li.Add(new Chunk("regular", regular));

您可以根据需要使用尽可能多的不同字体添加尽可能多的Chunk个对象。

相关问题