The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

thibeact (3) [Avatar] Offline
#1
Hi.

Is there anyway to combine several chunks or phrases with different formatting into a paragraph?
blowagie (284) [Avatar] Offline
#2
Re: Changing font in middle of paragraph
My first thought would be to answer: yes.
But then I assume you define 'different formatting' the same way I do.
Did you try something and found out it didn't work as you expected?
(Because formatting is a very broad concept.)
thibeact (3) [Avatar] Offline
#3
Re: Changing font in middle of paragraph
In a table cell, I am trying to have some text in bold Times Roman font, followed by some more text in normal helvetica.

I tried creating 2 chunks, adding them into a phrase, then add the phrase into the cell. However, resulting text is all helvetica.

Font times_bold = new Font(Font.TIMES_ROMAN, 12, Font.BOLD);
Font helvetica = new Font(Font.HELVETICA, 12, Font.NORMAL);

Chunk location_label = new Chunk("Location:", times_bold);
Chunk location_text = new Chunk("Some text", helvetica);
Phrase location = new Phrase(location_label + " " + location_text);

... then in my table
PdfPCell cell_location = new PdfPCell(location);
cell_location.setBorderWidthTop(0f);
cell_location.setBorderWidthRight(0.2f);
cell_location.setBorderWidthLeft(0f);
cell_location.setBorderWidthBottom(1f);
table.addCell(cell_location);
blowagie (284) [Avatar] Offline
#4
Re: Changing font in middle of paragraph
Please read page 167-168.
You are working in text mode, whereas you are expecting composite mode.
thibeact (3) [Avatar] Offline
#5
Re: Changing font in middle of paragraph
Thanks,
Found the problem was in construction my paragraphs.
Was trying to add 2 chunks into 1 cell with:
Chunk chunk1 = new Chunk("Hello ", FONT9);
Chunk chunk2 = new Chunk("World", FONT24B);

PdfPCell cell2 = new PdfPCell();
cell2.addElement(chunk1);
cell2.addElement(chunk2);
table.addCell(cell2);

Which gave correct font size, but added a carridge return after the first line.

When I should of used:
Chunk chunk1 = new Chunk("Hello ", FONT9);
Chunk chunk2 = new Chunk("World", FONT24B);

Paragraph paragraph = new Paragraph();
paragraph.add(chunk1);
paragraph.add(chunk2);
document.add(paragraph);


PdfPCell cell3 = new PdfPCell();
cell3.addElement(paragraph);
table.addCell(cell3);

Thanks!, Great book. Page 126-127 helped alot.