user6694876
user6694876

Reputation:

How to make one part of the text Bold in iText

I need to get a part of the text bold in my app. So basically what I am trying to achieve is have something like this: Address: 123 Street. Where at the moment i am getting all in bold: Address: 123 Street

Here is my code I am using:

PdfPTable table2 = new PdfPTable(3);
            table2.setWidthPercentage(100);
            table2.addCell(getCell("Address:  " + SaveString, PdfPCell.ALIGN_LEFT));
            table2.addCell(getCell("Name:  " + SaveStringA, PdfPCell.ALIGN_CENTER));
            table2.addCell(getCell("Last Name:  " + SaveStringB, PdfPCell.ALIGN_RIGHT));
            table2.setSpacingAfter(15);
            doc.add(table2);  

public PdfPCell getCell(String text, int alignment) {
    PdfPCell cell = new PdfPCell(new Phrase(text));
    Font bold = FontFactory.getFont(FontFactory.TIMES_BOLD, 12, Font.BOLD);
    Paragraph p1 = new Paragraph(text, bold);
    cell.setPadding(0);
    cell.addElement(p1);
    cell.setHorizontalAlignment(alignment);
    cell.setBorder(PdfPCell.NO_BORDER);

    return cell;

Hoping someone could help please?

Thanks!!

Upvotes: 1

Views: 3289

Answers (2)

ישו אוהב אותך
ישו אוהב אותך

Reputation: 29783

You can try using Chunk, something like this:

Font bold = FontFactory.getFont(FontFactory.TIMES_BOLD, 12, Font.BOLD);
Chunk chunkAddress= new Chunk("Address:  ", bold);
Chunk ChunkAddressDetails = new Chunk("123 Street.");

Phrase phrase = new Phrase();
phrase.add(chunkAddress);
phrase.add(ChunkAddressDetails);

Paragraph paragraph= = new Paragraph();
paragraph.add(phrase);

Upvotes: 3

Muhammed Saneef
Muhammed Saneef

Reputation: 72

SpannableStringBuilder str = new SpannableStringBuilder("Address: 123 Street");
str.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), 0, "Address:".length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
yourTextView.setText(str);

Upvotes: 0

Related Questions