Free Support Forum - aspose.com

How to find out if a table cell contains a particular string text

Hi,

Can you please let me know how to find out if a pdf.cell contains a particular string text?

I would like to loop thru a PDF table and see if a row should display the data in bold. This can be determined if the first cell in that row contains the string "Total". How do I compare the data in a cell if it contains the string "Total"?

Here is my pseudo code:

int idx = 0;

foreach (Aspose.Pdf.Row aRow in tab1.Rows)

{

if (aRow.Rows[idx][cell0].ToString() contains "Total")

{

//display make the cell data bold

}

}

Hi,

Thank you for considering Aspose.

Yes, it can be done. Just loop through the cells and it's segment. Check the contents of the Segment in the cell if the contents contains the Searched String then set it's Segment contents to bold. Please find the below code in bold.

foreach (Aspose.Pdf.Row row in table.Rows)

{

foreach (Aspose.Pdf.Cell cell in row.Cells)

{

foreach (Text text in cell.Paragraphs)

{

Aspose.Pdf.TextInfo txtInfo = new Aspose.Pdf.TextInfo();

txtInfo.FontSize = 10;

txtInfo.FontName = "Arial";

txtInfo.IsTrueTypeFontBold = true;

txtInfo.IsTrueTypeFontItalic = true;

foreach (Segment seg in text.Segments)

{

if (seg.Content.Contains("left"))

{

seg.TextInfo = txtInfo;

}

}

}

}

}

Hope it helps.

Thanks.

Is there anyway you can bold the entire row? Your code only bolds the cell which the string is found. I need to bold the entire row for which the string is found. The Search string is always in the first cell of a row.

Thanks again for your assitance.

Hi,

Please try the following:

bool makeBold;

Aspose.Pdf.TextInfo txtInfo = new Aspose.Pdf.TextInfo();

txtInfo.FontSize = 10;

txtInfo.FontName = "Arial";

txtInfo.IsTrueTypeFontBold = true;

txtInfo.IsTrueTypeFontItalic = true;

foreach (Aspose.Pdf.Row row in table.Rows)

{

makeBold = false;

foreach (Aspose.Pdf.Cell cell in row.Cells)

{

foreach (Text text in cell.Paragraphs)

{

foreach (Segment seg in text.Segments)

{

if (seg.Content.Contains("left"))

{

makeBold = true;

}

if(makeBold)

{

seg.TextInfo = txtInfo;

}

}

}

}

}

Thanks.