如何在iText中制作文本的一部分Bold

时间:2018-03-06 06:21:42

标签: android itext

我需要在我的应用中获得一部分粗体文字。所以基本上我想要实现的是这样的:地址: 123街。目前我在大胆的地方:地址:123 Street

这是我正在使用的代码:

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;

希望有人可以帮忙吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

您可以尝试使用Chunk,如下所示:

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

答案 1 :(得分:0)

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