Problem with textsegments not keeping colors

I am using the "segments" feature of a Text fragment. I create a textfragment and add several segments. The text appear as expected in the table cell - but not the properties. They appear as regular text (not bold, no colors). What am I doing wrong?

Code is below:

Dim t As Aspose.Pdf.Text.TextFragment = New Aspose.Pdf.Text.TextFragment

Dim d1 As Aspose.Pdf.Text.TextSegment = New Aspose.Pdf.Text.TextSegment("MyText1")
t.Segments.Add(d1)

Dim d2 As Aspose.Pdf.Text.TextSegment = New Aspose.Pdf.Text.TextSegment("MyText2")
d2.TextState.ForegroundColor = Aspose.Pdf.Color.White
d2.TextState.BackgroundColor = Aspose.Pdf.Color.Blue
d2.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold
t.Segments.Add(d2)

Row.Cells(1).Paragraphs.Add(t)

Hello There,


Thanks for using our API’s.

I have tested the scenario with your code, after little modification in your code and I have managed to get the expected output. I have attached the output PDF document and sample code for your reference.

<span class=“kwrd” style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>Dim<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”> doc <span class=“kwrd” style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>As<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”> <span class=“kwrd” style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>New<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”> Document()
Dim page As Page = doc.Pages.Add()
Dim table As New Table()

page.Paragraphs.Add(table)

Dim row As Row = table.Rows.Add()
Dim cell As Cell = row.Cells.Add()

Dim t As New Aspose.Pdf.Text.TextFragment()
Dim d1 As New Aspose.Pdf.Text.TextSegment(“MyText1”)

t.Segments.Add(d1)

Dim d2 As New Aspose.Pdf.Text.TextSegment(“MyText2”)
d2.TextState.ForegroundColor = Aspose.Pdf.Color.White
d2.TextState.BackgroundColor = Aspose.Pdf.Color.Blue
d2.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold

t.Segments.Add(d2)

cell.Paragraphs.Add(t)
doc.Save(dataDir + “outputdoc.pdf”)

If you still face any issue, please feel free to contact us for further assistance.

Best Regards,

Hi Fahad,


Thank you for the response. Unfortunately it still didn’t work for me. My code is the same as far as I can tell - but with one exception … I have set default cell styles for my table. So perhaps they are overriding the segment properties.

In any case - I did come up with an alternative method that does work that I wanted to share in case anyone else has the same issue.

I ended up using. Instead of segments I used multiple textfragments and “inline” paragraphs as follows.

Dim t1 As New Aspose.Pdf.Text.TextFragment(“MyText1”)
t1.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold
Cell.Paragraphs.Add(t1)

Dim t2 As New Aspose.Pdf.Text.TextFragment(“MyText2”)
t2.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold
t2.TextState.BackgroundColor = Aspose.Pdf.Color.Blue
t2.IsInLineParagraph = true
Cell.Paragraphs.Add(t2)

Thanks again for looking into this.

Hello There,


Thanks for using our API’s.

Its good to know that you found the solution. However, our solution should also work for you as well, I will appreciate if you share your sample project. It will help us to reproduce the issue at our end and address it accordingly.

Best Regards,
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; -webkit-text-stroke: #000000} span.s1 {font-kerning: none}