I want to write this.

Last night i dreamt of San Pedro.


How can I write this with 1 PdfPCell

Dani AI

Generated

Brief answer: build a single inline element (a Phrase or a Paragraph made of inline pieces) and put that in one PdfPCell. Paragraph objects are block-level: adding separate Paragraphs creates new lines. For a single line with mixed styles use Chunk/Text pieces (iText 2.x/5) or Text (iText 7) so only the portions you want bold are changed.

As pointed to font/style docs, use different Font instances for the bold/regular parts and compose them into one Phrase. Example for older iText (com.lowagie / iText 2.x style):

Font bold = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD);
Font normal = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL);

Phrase p = new Phrase();
p.add(new Chunk("Last", bold));
p.add(new Chunk(" night i dreamt of ", normal));
p.add(new Chunk("San Pedro", bold));

PdfPCell cell = new PdfPCell(p);

If using iText 7, the equivalent is to use Paragraph + Text and set bold on the Text objects:

Paragraph p = new Paragraph()
    .add(new Text("Last").setBold())
    .add(new Text(" night i dreamt of "))
    .add(new Text("San Pedro").setBold());

Cell cell = new Cell().add(p);
table.addCell(cell);

Notes and tips: adding Paragraph instances to a Paragraph will create line breaks, so avoid that if you want a single line. Use Chunk.NEWLINE (or Text.setMultipliedLeading / add("\n")) when you do want an explicit break. Ensure the fonts you choose provide the bold/italic faces (embed custom fonts if needed). If you need images plus inline text in the same cell (like ’s multi-element cell), use addElement and nested tables; otherwise a single Phrase/Paragraph with Chunks/Text is the simplest and most robust solution for mixed inline styling.

Recommended Answers

All 3 Replies


Thank you. That helped me a lot. Can't we do this by creating an array of Parapraph objects?

Paragraph myParagraph = new Paragraph(3)

myParagraph.add(new Paragraph(......));
myParagraph.add(new Paragraph(......));
myParagraph.add(new Paragraph(......));

Now will there be new lines if i print out myParagraph???

Or can I also use Paragraph to print out

Last night I dreamt of San Pedro

com.lowagie.text.pdf.PdfPTable table = new PdfPTable(columns);
        PdfPTable local = new PdfPTable(1);
        PdfPCell ind = new PdfPCell(new Phrase("" + index + "."));
        ind.setBorderColor(frameColor);
        ind.setHorizontalAlignment(indexAlign);
        PdfPCell img = new PdfPCell();
        img.addElement(image);
        img.setHorizontalAlignment(Element.ALIGN_CENTER);
        img.setBorderColor(frameColor);
        PdfPCell txt = new PdfPCell(new Paragraph(text, font));
        PdfPCell txt1 = new PdfPCell(new Paragraph(...));
        PdfPCell txt2 = new PdfPCell(new Paragraph(...));
        PdfPCell txt3 = new PdfPCell(new Paragraph(...));

        txt.setBorderColor(frameColor);

        local.addCell(ind);
        local.addCell(img);
        local.addCell(txt);
        local.addCell(txt1);
        local.addCell(txt2);
        local.addCell(txt3);
 table.addCell(local);
//
        Paragraph myParagraph = new Paragraph(3);
        myParagraph.add(new Paragraph("aaaaaaaaa", font));
        myParagraph.add(new Paragraph("bbbbbbbbb", font));
        myParagraph.add(new Paragraph("cccccccccccc", font));
        PdfPCell myCell = new PdfPCell(myParagraph);
        myCell.setBorderColor(frameColor);
        local.addCell(myCell);
        //
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.