Bold some text in Pdf List

2020-03-29 09:38发布

问题:

I am displaying some content in a List to show it in a pdf file.

Every thing is working fine , but Now I want some text in a List Item Should be Bold.

For Example :

This is ListItem Bold Text.

How can I do this ?

Here is my code :

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

EDIT

I've tried this :

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

But this didn't worked

回答1:

Please take a look at the answer of this question: How can I use regular and bold in a single String?

The answer talks about a Paragraph, but it also works for a ListItem as ListItem is a subclass of Paragraph:

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

You can add as many Chunk objects using as many different fonts as you want.



回答2:

You can do this by using Paragraph and Chunks like below:

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