Detecting bold text in Cell

Hello again)
I have html table like this:

<table style="border-collapse: collapse; width: 100.011%; height: 100.656px;" border="1" data-mce-selected="1">
	<colgroup>
		<col style="width: 49.8807%;">
			<col style="width: 49.8807%;">
			</colgroup>
			<tbody>
				<tr style="height: 21px;">
					<td>
						<br>
						</td>
						<td>
							<br>
							</td>
						</tr>
						<tr style="height: 79.6562px;">
							<td>
								<br>
								</td>
								<td class="rotate-text" style="transform: rotate(180deg); writing-mode: vertical-rl;">
									<strong>VERT_BOLD</strong>
								</td>
							</tr>
						</tbody>
					</table>

How to detect that text in one of the cell in Bold? It tagged via VERT_BOLD

cell.getFirstParagraph().getParagraphFormat().getStyle().getFont() not working, it always shows false

@cagecrew

To detect bold text in a cell of an HTML table using Aspose.Words, you can check the formatting of the text within the cell. Since the text is wrapped in a <strong> tag, you can identify it as bold. Here’s how you can achieve this:

  1. Load the Document: First, load your HTML document into an Aspose.Words Document object.
  2. Access the Table and Cell: Navigate to the specific table and cell where you expect the bold text.
  3. Check for Bold Formatting: Iterate through the runs in the cell’s paragraphs and check if any run has its bold property set to true.

Here’s a sample code snippet demonstrating this:

import com.aspose.words.*;

public class DetectBoldText {
    public static void main(String[] args) throws Exception {
        // Load the HTML document
        Document doc = new Document("path/to/your/document.html");

        // Access the first table in the document
        Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
        
        // Access the specific cell (row 1, column 1 in this case)
        Cell cell = table.getRows().get(1).getCells().get(1);
        
        // Check for bold text in the cell
        boolean hasBoldText = false;
        for (Paragraph paragraph : cell.getParagraphs()) {
            for (Run run : paragraph.getRuns()) {
                if (run.getFont().getBold()) {
                    hasBoldText = true;
                    break;
                }
            }
            if (hasBoldText) {
                break;
            }
        }

        // Output the result
        if (hasBoldText) {
            System.out.println("The cell contains bold text.");
        } else {
            System.out.println("The cell does not contain bold text.");
        }
    }
}

In this code:

  • We load the HTML document and access the first table.
  • We then access the specific cell and iterate through its paragraphs and runs to check if any run has the bold property set to true.
  • Finally, we print whether the cell contains bold text.

This approach allows you to effectively detect bold text within a specific cell of an HTML table using Aspose.Words.

1 Like