HTML is a markup language used to structure web pages using different elements, tags, etc. HTML documents are usually assisted by formatting technologies like CSS. In some use cases, you may need to modify the text formatting in an HTML file. In accordance with such scenarios, this article covers how to change text color in an HTML file programmatically in Java.
- HTML Text Color Formatting – Java API Installation
- Change Text Color in HTML using Inline CSS using Java
- Change Text Color in HTML using Internal CSS in Java
HTML Text Color Formatting – Java API Installation
Aspose.HTML for Java API supports manipulating or converting different file formats including MD, XPS, SVG, etc. You can easily access the API by configuring its JAR file from the Downloads page, or pasting the following configurations in the pom.xml file of your project in order to install it from the Aspose Repository:
<repositories> <repository> <id>snapshots</id> <name>repo</name> <url>http://repository.aspose.com/repo/</url> </repository> </repositories>
<dependencies> <dependency> <groupId>com.aspose</groupId> <artifactId>aspose-html</artifactId> <version>22.9</version> <classifier>jdk18</classifier> </dependency> </dependencies>
Change Text Color in HTML using Inline CSS using Java
The steps below elaborate on how to change text color in an HTML file using inline CSS:
- Load the source HTML using the HTMLDocument class.
- Find the first paragraph element to set a style attribute.
- Set the style attribute with the color property.
- Save the HTML document.
The following code snippet demonstrates how to change text color in HTML using inline CSS programmatically using Java:
Change Text Color in HTML using Internal CSS using Java
The following steps explain how to change text color in HTML using internal CSS using Java:
- Create an object of HTMLDocument class.
- Make a style element and set the text color value for all paragraph elements.
- Append style after finding the head element.
- Save the updated HTML file.
The following sample code elaborates on how to change text color in an HTML file using internal CSS programmatically in Java:
Explore Aspose.HTML for Java
You may visit the [documentation] section to learn many features included in the API.
In conclusion, you have learned how to change text color in an HTML document programmatically using Java. Furthermore, please feel free to reach out to us at forum in case of any queries.