We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

String with multiple font using TextFragment

Hi Team,


i want to display multi colored string in a single cell of table. Example string as show below.

“hi Aspose!!” Please suggest how this can be achived.

i am using com.aspose.pdf package classes.

Thanks in advance.

Hi Ramesh,

Thanks for contacting support.

In order to accomplish your requirements, please try using following code snippet. For your reference, I have also attached the resultant file generated over my end.
[Java]
com.aspose.pdf.Document pdfDocument1 = new com.aspose.pdf.Document();

pdfDocument1.getPages().add();

// create table instance

com.aspose.pdf.Table table1 = new com.aspose.pdf.Table();

// set width of table columns

table1.setColumnWidths("100 100");

// add row to table instance

com.aspose.pdf.Row row1 = table1.getRows().add();

// create cell instance

com.aspose.pdf.Cell cell1 = row1.getCells().add();

// create TextFragment for cell1 instance

com.aspose.pdf.TextFragment textfragment = new com.aspose.pdf.TextFragment();

// create first segment instance

com.aspose.pdf.TextSegment seg1 = new com.aspose.pdf.TextSegment("hi ");

// set foreground color for first segment

seg1.getTextState().setForegroundColor(com.aspose.pdf.Color.getRed());

// add first segment to TextFragment instance

textfragment.getSegments().add(seg1);

//create first segment instance

com.aspose.pdf.TextSegment seg2 = new com.aspose.pdf.TextSegment("Aspose!!");

//set foreground color for first segment

seg2.getTextState().setForegroundColor(com.aspose.pdf.Color.getGreen());

//add first segment to TextFragment instance

textfragment.getSegments().add(seg2);

cell1.getParagraphs().add(textfragment);

// add table to paragraphs collection of first page of document

pdfDocument1.getPages().get_Item(1).getParagraphs().add(table1);

// save PDF file

pdfDocument1.save("c:/pdftest/TableWithTwo_Fragments.pdf");