Format for text in cell

Hello support,

If I set some formatting for segments within a text object, or for the whole text object, and then I add the text to the paragraphs within current section, then the text formatting is preserved (font family, color etc). If I add the same text object to the paragraphs within a cell, the text formatting is lost.

Please advice.
Thank you,
Milan

Hello Milan,

In order for the text object to retain the text formatting information when its added to the cell object, please try using the Row.cells.Add(String,TextInfo) overloaded method. In the second parameter you can specify the TextInfo object that contains the formatting information either for Segment or text object. Please try the following code snippet in order to have the understanding. In the following code text formatting for the Segment is set.

Pdf pdf1 = new Pdf();
Aspose.Pdf.Section sec1 = pdf1.Sections.Add();
sec1.Paragraphs.Add(new Aspose.Pdf.Text(sec1, "paragraph 1 "));

Text t3 = new Aspose.Pdf.Text(sec1);
Segment seg1 = new Segment(t3);
seg1.Content = "paragraph 2 segment 1";
seg1.TextInfo.Color = new Aspose.Pdf.Color("Red");
seg1.TextInfo.FontName = "Courier New";
t3.Segments.Add(seg1);
sec1.Paragraphs.Add(t3);

Table tab1 = new Table();
sec1.Paragraphs.Add(tab1);
tab1.DefaultCellBorder = new BorderInfo((int)BorderSide.All, 0.1F);
Row row1 = tab1.Rows.Add();
row1.Cells.Add("Col1");
row1.Cells.Add("col2",seg1.TextInfo); //add the column using the same formatting information as Seg1
row1.Cells.Add("col3");

pdf1.Save("c:\\Formated.pdf");

Please take a review, if it does not answer your query, please feel free to share.

Thank you for your reply.

What I really want is specific formatting for particular text in the cell. Something like : "this is cell text". Your example allows formatting for whole text in cell, and the text can be added just once, when you create the cell. "Codewarior" suggested using html tags for the text within cell, related with setting the text object property "IsHtmlTagSupported" to true. This is supported in C#, but I don't think that is supported in java. (I'm using java).

Regards,
Milan

Hello milan,

The Segment object in Text can has its own style(such as bold, underline etc). I think you can add different style segments in Text and then add this Text object in Cell.

The demonstrative code is as follows:
Pdf pdf = new Pdf();

//add a section
Section sec1 = pdf.getSections().add();

Table table = new Table(sec1);
table.setColumnWidths(“100 100”);

//add a row
Row row1 = new Row(table);
table.getRows().add(row1);

Cell cell11 = new Cell(row1);
row1.getCells().add(cell11);

Text text1 = new Text(sec1);

Segment seg1 = new Segment(text1, “this”);
seg1.getTextInfo().setTrueTypeBold(true);
text1.getSegments().add(seg1);

Segment seg2 = new Segment(text1, " is");
seg2.getTextInfo().setTrueTypeItalic(true);
seg2.getTextInfo().setUnderLine(true);
text1.getSegments().add(seg2);

Segment seg3 = new Segment(text1, " Cell");
seg3.getTextInfo().setFontSize(15);
text1.getSegments().add(seg3);

Segment seg4 = new Segment(text1, " text");
seg4.getTextInfo().setTrueTypeItalic(true);
text1.getSegments().add(seg4);

cell11.getParagraphs().add(text1);


Cell cell12 = new Cell(table,“some more text”);
row1.getCells().add(cell12);

//add another row
Row row2 = new Row(table);
table.getRows().add(row2);

Cell cell21 = new Cell(table,“another some text”);
row2.getCells().add(cell21);

Cell cell22 = new Cell(table,“another more text”);
row2.getCells().add(cell22);

//add table to sec1
sec1.getParagraphs().add(table);

FileOutputStream out=new FileOutputStream(new File(“output/TestTable.pdf”));
pdf.save(out);

Thank you for using our product!

Hi,

Did you test your example ? Because I don't get the desired output on my machine. Anyway, your code is similar with the one that caused my first entry in this case. If you add this line in your code : "sec1.getParagraphs().add(text1);" after the line "cell11.getParagraphs().add(text1);", you'll note what my first entry was about : the formatting is as expected in the section's paragraphs, but is lost within the cell's paragraphs.

Regards,
Milan

Hello <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />Milan.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

I have tested the code and have been able to produce the desired result. I have also added your suggested code line sec1.getParagraphs().add(text1);" and have seen the same formatting behavior in both paragraph and cell. Make sure you are using the latest version Aspose.Pdf for Java 2.3.1.0 Hot Fix . The output file that I have generated is in attachment. Please take a review and incase of any further query, please feel free to share.

Hi codewarior,

Yes, the problem was my jar, the 2.3.0.0 version. The text format in cell is ok now.

Thank you,
Milan