Attach an HTML Tag to the HEAD of an HTML Doc in Java

Cloudmersive
2 min readJul 13, 2021

If you frequently work with HTML documents, ensuring you have all the critical components can become tedious and the margin for error increases. In this brief tutorial, we will demonstrate how you can use an API to automatically append an HTML tag to the HEAD of an HTML document in Java, avoiding the possibility of missing this important element.

To install with Maven, we will add a reference to the repository in pom.xml:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Then, we will add a reference to the dependency:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.90</version>
</dependency>
</dependencies>

Now, we are ready to add the imports to the top of our controller and call the function with the following code:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.EditHtmlApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
EditHtmlApi apiInstance = new EditHtmlApi();
String htmlTag = "htmlTag_example"; // String | The HTML tag to append.
File inputFile = new File("/path/to/inputfile"); // File | Optional: Input file to perform the operation on.
String inputFileUrl = "inputFileUrl_example"; // String | Optional: URL of a file to operate on as input.
try {
byte[] result = apiInstance.editHtmlHtmlAppendHeaderTag(htmlTag, inputFile, inputFileUrl);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling EditHtmlApi#editHtmlHtmlAppendHeaderTag");
e.printStackTrace();
}

That’s it! Quick and simple.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.